添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Report this

What is the reason for this report?

How To Install and Use Docker Compose on Ubuntu 22.04

Published on April 26, 2022
Tony Tran Erika Heidi
How To Install and Use Docker Compose on Ubuntu 22.04

Introduction

Docker Compose is a tool that allows you to run multi-container application environments based on definitions set in a YAML file. It uses service definitions to build fully customizable environments with multiple containers that can share networks and data volumes.

In this guide, you’ll demonstrate how to install Docker Compose on an Ubuntu 22.04 server and how to get started using this tool.

Prerequisites

  • Initial Server Setup Guide for Ubuntu 22.04 .
  • Docker installed on your server or local machine, following Steps 1 and 2 of How To Install and Use Docker on Ubuntu 22.04 .
  • Note: Starting with Docker Compose v2, Docker has migrated towards using the compose CLI plugin command, and away from the original docker-compose as documented in our previous Ubuntu 20.04 version of this tutorial . While the installation differs, in general the actual usage involves dropping the hyphen from docker-compose calls to become docker compose . For full compatibility details, check the official Docker documentation on command compatibility between the new compose and the old docker-compose .

    Step 1 — Installing Docker Compose

    official Github repository .

    First, confirm the latest version available in their releases page . At the time of this writing, the most current stable version is 2.3.3 .

    Use the following command to download:

    1. mkdir -p ~/.docker/cli-plugins/
    2. curl -SL https://github.com/docker/compose/releases/download/v2.3.3/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose

      Next, set the correct permissions so that the docker compose command is executable:

      1. chmod +x ~/.docker/cli-plugins/docker-compose

        To verify that the installation was successful, you can run:

        1. docker compose version

          You’ll see output similar to this:

          Output
          Docker Compose version v2.3.3

          Docker Compose is now successfully installed on your system. In the next section, you’ll see how to set up a docker-compose.yml file and get a containerized environment up and running with this tool.

          Step 2 — Setting Up a docker-compose.yml File

          Nginx image from Docker Hub, the public Docker registry. This containerized environment will serve a single static HTML file.

          Start off by creating a new directory in your home folder, and then moving into it:

          1. mkdir ~/compose-demo
          2. cd ~/compose-demo

            In this directory, set up an application folder to serve as the document root for your Nginx environment:

            1. mkdir app

              Using your preferred text editor, create a new index.html file within the app folder:

              1. nano app/index.html

                Place the following content into this file:

                ~/compose-demo/app/index.html
                <!doctype html>
                <html lang="en">
                    <meta charset="utf-8">
                    <title>Docker Compose Demo</title>
                    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
                </head>
                    <h1>This is a Docker Compose Demo Page.</h1>
                    <p>This content is being served by an Nginx container.</p>
                </body>
                </html>
                

                Save and close the file when you’re done. If you are using nano, you can do that by typing CTRL+X, then Y and ENTER to confirm.

                Next, create the docker-compose.yml file:

                1. nano docker-compose.yml

                  Insert the following content in your docker-compose.yml file:

                  docker-compose.yml
                  version: '3.7'
                  services:
                    web:
                      image: nginx:alpine
                      ports:
                        - "8000:80"
                      volumes:
                        - ./app:/usr/share/nginx/html
                  

                  The docker-compose.yml file typically starts off with the version definition. This will tell Docker Compose which configuration version you’re using.

                  You then have the services block, where you set up the services that are part of this environment. In your case, you have a single service called web. This service uses the nginx:alpine image and sets up a port redirection with the ports directive. All requests on port 8000 of the host machine (the system from where you’re running Docker Compose) will be redirected to the web container on port 80, where Nginx will be running.

                  The volumes directive will create a shared volume between the host machine and the container. This will share the local app folder with the container, and the volume will be located at /usr/share/nginx/html inside the container, which will then overwrite the default document root for Nginx.

                  Save and close the file.

                  You have set up a demo page and a docker-compose.yml file to create a containerized web server environment that will serve it. In the next step, you’ll bring this environment up with Docker Compose.

                  Step 3 — Running Docker Compose

                  1. Step 2 of How To Install and Use Docker on Ubuntu 22.04. Going back and completing that step will enable permissions to run docker commands without sudo.

                    Your environment is now up and running in the background. To verify that the container is active, you can run:

                    1. docker compose ps

                      This command will show you information about the running containers and their state, as well as any port redirections currently in place:

                      Output
                      Name Command State Ports ---------------------------------------------------------------------------------- compose-demo_web_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:8000->80/tcp

                      You can now access the demo application by pointing your browser to either localhost:8000 if you are running this demo on your local machine, or your_server_domain_or_IP:8000 if you are running this demo on a remote server.

                      You’ll see a page like this:

                      The shared volume you’ve set up within the docker-compose.yml file keeps your app folder files in sync with the container’s document root. If you make any changes to the index.html file, they will be automatically picked up by the container and thus reflected on your browser when you reload the page.

                      In the next step, you’ll see how to manage your containerized environment with Docker Compose commands.

                      Step 4 — Getting Familiar with Docker Compose Commands

                                1. How to Install and Use Docker for a more detailed reference on Docker commands.

                                  Conclusion

                                  official documentation.

    Tutorial Series: Getting Started With Cloud Computing

    This curriculum introduces open-source cloud computing to a general audience along with the skills necessary to deploy applications and websites securely to the cloud.

    About the author(s)

    Erika Heidi
    Erika Heidi
    Author
    Developer Advocate
    See author profile

    Dev/Ops passionate about open source, PHP, and Linux. Former Senior Technical Writer at DigitalOcean. Areas of expertise include LAMP Stack, Ubuntu, Debian 11, Linux, Ansible, and more.

    Still looking for an answer?

    Was this helpful?
    

    This textbox defaults to using Markdown to format your answer.

    You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

    Wonderful tutorial! I followed this tutorial for installing. Then I found this one that has a good tip on using bash aliases.

    Is there any difference in installing docker compose in .docker/cli-plugins/ vs installing it in /usr/local/bin/ ? The latter is what the linked guide says to do. This is what your older guide (20.04) and the one linked above is asking me to do.

    Thx for the tutorial! I am constantly running into the issue that I cant mount any directory into the container which is outside of the current folder structure of the home directory of the user who installed docker compose. The container directory is empty but should contain the files in the host directory. E.g.

    volumes:
     - ./www:/var/www/html