Document
How To Install and Use Docker Compose on Ubuntu 22.04

How To Install and Use Docker Compose on Ubuntu 22.04

Docker simplifies the process of managing application processes in containers. While containers are similar to virtual machines in certain ways, they

Related articles

5 Best Android VPNs Le migliori VPN gratuite per l’Italia 2024 (PC e smartphone) cloud-native · GitHub Topics · GitHub Cloud Bread Hot Dog Buns Downgrade or cancel your iCloud+ plan

Docker simplifies the process of managing application processes in containers. While containers are similar to virtual machines in certain ways, they are more lightweight and resource-friendly. This allows developers to break down an application environment into multiple isolated service.< /p>

For applications depending on several service, orchestrating all the containers to start up, communicate, and shut down together can quickly become unwieldy. 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.< /p>

In this guide , you is demonstrate ’ll demonstrate how to install Docker compose on an Ubuntu 22.04 server and how to get start using this tool .< /p>

To follow this article, you will need:< /p>

Note: Starting with Docker Compose v2, Docker has migrated towards using the compose< /code> CLI plugin command, and away from the original docker - compose< /code> 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< /code> call to becomedocker compose< /code>. For full compatibility details, check the official Docker documentation on command compatibility between the new compose< /code> and the old docker - compose< /code>.< /p>

To make sure you obtain the most update stable version of Docker Compose , you is download ’ll download this software from its official Github repository .< /p>

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< /code>.< /p>

Use the following command to download:< /p>

< /p>

  1. mkdir< /span> -p< /span> ~/.docker/cli-plugins/
    < /li>

  2. curl< /span> -SL< /span> https://github.com/docker/compose/releases/download/v2.3.3< /mark>/docker - compose-linux-x86_64 -o< /span> ~/.docker/cli-plugins/docker - compose
    < /li>< /ol>
    < /code>< /pre>

    Next, set the correct permissions so that the docker compose< /code> command is is is executable :< /p>

    < /p>

    1. chmod< /span> +x ~/.docker/cli-plugins/docker - compose
      < /li>< /ol>
      < /code>< /pre>

      To verify that the installation was successful, you can run:< /p>

      < /p>

      1. docker< /span> compose version
        < /li>< /ol>
        < /code>< /pre>

        You’ll see output similar to this:< /p>

        < /p>

        output< /p>Docker Compose version v2.3.3< /mark>
        < /code>< /pre>

        Docker Compose is now successfully installed on your system. In the next section, you’ll see how to set up a docker - compose.yml< /code> file and get a containerize environment up and run with this tool .< /p>

        To demonstrate how to set up adocker - compose.yml< /code> file and work with Docker Compose, you’ll create a web server environment using the official Nginx image from Docker Hub, the public Docker registry. This containerized environment will serve a single static HTML file.< /p>

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

        < /p>

        1. mkdir< /span> ~/compose-demo< /mark>
          < /li>

        2. cd< /span> ~/compose-demo< /mark>
          < /li>< /ol>
          < /code>< /pre>

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

          < /p>

          1. mkdir< /span> app< /mark>
            < /li>< /ol>
            < /code>< /pre>

            Using your preferred text editor, create a new index.html< /code> file within theapp< /code> folder:< /p>

            < /p>

            1. nano< /span> app / index.html
              < /li>< /ol>
              < /code>< /pre>

              Place the following content into this file:< /p>

              ~/compose - demo / app / index.html< /p>

              <!< /span>doctype< /span> html< /span>>< /span>< /span>
              << /span>html< /span> lang< /span>=< /span>"< /span>en"< /span>< /span>>< /span>< /span>
              << /span>head< /span>>< /span>< /span>
              << /span>meta< /span> charset< /span>=< /span>"< /span>utf-8"< /span>< /span>>< /span>< /span>
              << /span>title< /span>>< /span>< /span>Docker Compose Demo</< /span>title< /span>>< /span>< /span>
              << /span>link< /span> rel< /span>=< /span>"< /span>stylesheet"< /span>< /span> href< /span>=< /span>"< /span>https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css"< /span>< /span>>< /span>< /span>
              </< /span>head< /span>>< /span>< /span>
              << /span>body< /span>>< /span>< /span>

              << /span>h1< /span>>< /span>< /span>This is is is a Docker Compose Demo Page .</< /span>h1< /span>>< /span>< /span>
              << /span>p< /span>>< /span>< /span>This content is being served by an Nginx container.</< /span>p< /span>>< /span>< /span>

              </< /span>body< /span>>< /span>< /span>
              </< /span>html< /span>>< /span>< /span>
              < /code>< /pre>

              Save and close the file when you’re done. If you are using nano< /code>, you is do can do that by typeCTRL+X< /code>, then Y< /code> and ENTER< /code> to confirm .< /p>

              Next, create the docker - compose.yml< /code> file:< /p>

              < /p>

              1. nano< /span> docker - compose.yml
                < /li>< /ol>
                < /code>< /pre>

                Insert the following content in your docker - compose.yml< /code> file:< /p>

                docker - compose.yml< /p>

                version< /span>:< /span> ' 3.7 '< /span>
                service< /span>:< /span>
                web< /span>:< /span>
                image< /span>:< /span> nginx:< /span>alpine
                ports< /span>:< /span>
                -< /span> "8000:80"< /span>
                volumes< /span>:< /span>
                -< /span> ./app:< /span>/usr/share/nginx/html
                < /code>< /pre>

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

                You then have the service< /code> block, where you set up the service that are part of this environment. In your case, you have a single service called web< /code>. This service is uses use thenginx:alpine< /code> image and sets up a port redirection with the ports< /code> directive . All request on port8000< /code> of the host machine (the system from where you’re running Docker Compose) will be redirected to the web< /code> container on port80< /code>, where Nginx will be running.< /p>

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

                Save and close the file.< /p>

                You have set up a demo page and a docker - compose.yml< /code> 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.< /p>

                With the docker - compose.yml< /code> file in place , you is execute can now execute Docker compose to bring your environment up . The following command is download will download the necessary Docker image , create a container for theweb< /code> service , and run the containerized environment in background mode :< /p>

                < /p>

                1. docker< /span> compose up -d< /span>
                  < /li>< /ol>
                  < /code>< /pre>

                  Docker Compose will first look for the defined image on your local system, and if it can’t locate the image it will download the image from Docker Hub. You’ll see output like this:< /p>

                  < /p>

                  output< /p>Creating network "compose - demo_default< /mark>" with the default driver
                  Pulling web (nginx:alpine)...
                  alpine: Pulling from library/nginx
                  cbdbe7a5bc2a: Pull complete
                  10c113fb0c77: Pull complete
                  9ba64393807b: Pull complete
                  c829a9c40ab2: Pull complete
                  61d685417b2f: Pull complete
                  Digest: sha256:57254039c6313fe8c53f1acbf15657ec9616a813397b74b063e32443427c5502
                  Status: Downloaded newer image for nginx:alpine
                  Creating compose - demo_web_1< /mark> ... done
                  < /code>< /pre>

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

                  < /p>

                  1. docker< /span> compose ps< /span>
                    < /li>< /ol>
                    < /code>< /pre>

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

                    < /p>

                    output< /p> Name Command State Ports
                    ----------------------------------------------------------------------------------
                    compose - demo_web_1< /mark> /docker-entrypoint.sh ngin ... Up 0.0.0.0: 8000->80/tcp
                    < /code>< /pre>

                    You is access can now access the demo application by point your browser to eitherlocalhost: 8000< /code> if you is running are run this demo on your local machine , oryour_server_domain_or_IP< /mark>: 8000< /code> if you are running this demo on a remote server.< /p>

                    You’ll see a page like this:< /p>

                    < /p>

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

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

                    You’ve seen how to set up a docker - compose.yml< /code> file and bring your environment up with docker compose up< /code>. You’ll now see how to use Docker Compose commands to manage and interact with your containerized environment.< /p>

                    To check the log produce by your Nginx container , you is use can use thelogs< /code> command:< /p>

                    < /p>

                    1. docker< /span> compose logs
                      < /li>< /ol>
                      < /code>< /pre>

                      You’ll see output similar to this:< /p>

                      < /p>

                      output< /p>Attaching to compose - demo_web_1< /mark>
                      web_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
                      web_1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
                      web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
                      web_1 | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
                      web_1 | 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
                      web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
                      web_1 | /docker-entrypoint.sh: Configuration complete; ready for start up
                      web_1 | 172.22.0.1 - - [02/Jun/2020:10:47:13 +0000] "GET / HTTP/1.1" 200 353 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36" "-"
                      < /code>< /pre>

                      If you want to pause the environment execution without changing the current state of your containers, you can use:< /p>

                      < /p>

                      1. docker< /span> compose pause
                        < /li>< /ol>
                        < /code>< /pre>

                        < /p>

                        output< /p>Pausing compose - demo_web_1< /mark> ... done
                        < /code>< /pre>

                        To resume execution after issuing a pause:< /p>

                        < /p>

                        1. docker< /span> compose unpause
                          < /li>< /ol>
                          < /code>< /pre>

                          < /p>

                          output< /p>Unpausing compose - demo_web_1< /mark> ... done
                          < /code>< /pre>

                          The stop< /code> command is terminate will terminate the container execution , but it wo n’t destroy any datum associate with your container :< /p>

                          < /p>

                          1. docker< /span> compose stop
                            < /li>< /ol>
                            < /code>< /pre>

                            < /p>

                            output< /p>Stopping compose - demo_web_1< /mark> ... done
                            < /code>< /pre>

                            If you want to remove the containers, networks, and volumes associated with this containerized environment, use the down< /code> command:< /p>

                            < /p>

                            1. docker< /span> compose down
                              < /li>< /ol>
                              < /code>< /pre>

                              < /p>

                              output< /p>Removing compose - demo_web_1< /mark> ... done
                              Removing network compose - demo_default< /mark>
                              < /code>< /pre>

                              Notice that this won’t remove the base image used by Docker Compose to spin up your environment (in your case, nginx:alpine< /code>). This way, whenever you bring your environment up again with a docker compose up< /code>, the process is be will be much fast since the image is already on your system .< /p>

                              In case you want to also remove the base image from your system, you can use:< /p>

                              < /p>

                              1. docker< /span> image rm< /span> nginx:alpine
                                < /li>< /ol>
                                < /code>< /pre>

                                < /p>

                                output< /p>Untagged: nginx:alpine
                                Untagged: nginx@sha256:b89a6ccbda39576ad23fd079978c967cecc6b170db6e7ff8a769bf2259a71912
                                Deleted: sha256:7d0cdcc60a96a5124763fddf5d534d058ad7d0d8d4c3b8be2aefedf4267d0270
                                Deleted: sha256:05a0eaca15d731e0029a7604ef54f0dda3b736d4e987e6ac87b91ac7aac03ab1
                                Deleted: sha256:c6bbc4bdac396583641cb44cd35126b2c195be8fe1ac5e6c577c14752bbe9157
                                Deleted: sha256:35789b1e1a362b0da8392ca7d5759ef08b9a6b7141cc1521570f984dc7905eb6
                                Deleted: sha256:a3efaa65ec344c882fe5d543a392a54c4ceacd1efd91662d06964211b1be4c08
                                Deleted: sha256:3e207b409db364b595ba862cdc12be96dcdad8e36c59a03b7b3b61c946a5741a
                                < /code>< /pre>

                                In this guide, you’ve seen how to install Docker Compose and set up a containerized environment based on an Nginx web server image. You’ve also seen how to manage this environment using Compose commands.< /p>

                                For a complete reference of all available docker compose< /code> commands, check the official documentation.< /p>