Docker is a great tool for automating the deployment of Linux applications inside software containers, but to take full advantage of its potential, each component of an application should run in its own individual container. For complex applications with a lot of components, orchestrating all the containers to start up, communicate, and shut down together can quickly become unwieldy.
The Docker community came up with a popular solution called Fig, which allowed you to use a single YAML file to orchestrate all your Docker containers and configurations. This became so popular that the Docker team decided to make Docker Compose based on the Fig source, which is now deprecated.
Docker Compose makes it easier for users to orchestrate the processes of Docker containers, including starting up, shutting down, and setting up intra-container linking and volumes. In this tutorial, you’ll install the latest version of Docker Compose to manage multi-container applications.
To follow this article, you will need an Ubuntu 18.04 server with the following:
Note: This tutorial will guide you through installing Docker Compose v1, which uses
docker-compose. Starting with Docker Compose v2, Docker has migrated towards using the
compose CLI plugin command as documented in our latest Ubuntu 22.04 version of this tutorial, and away from the original
docker-compose. 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
Although you can install Docker Compose from the official Ubuntu repositories, it is several minor versions behind the latest release, so you’ll install Docker Compose from Docker’s GitHub repository. The command below is slightly different than the one you’ll find on the Releases page. By using the
-o flag to specify the output file first rather than redirecting the output, this syntax avoids running into a permission denied error caused when using
Check the current release and if necessary, update it in the command below:
- sudo curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
Next set the permissions:
- sudo chmod +x /usr/local/bin/docker-compose
Then you’ll verify that the installation was successful by checking the version:
- docker-compose --version
This will print out the version you installed:
Outputdocker-compose version 1.29.2, build 5becea4c
Now that you have Docker Compose installed, you’re ready to run a “Hello World” example.
The public Docker registry, Docker Hub, includes a Hello World image for demonstration and testing. It illustrates the minimal configuration required to run a container using Docker Compose: a YAML file that calls a single image:
First, you’ll create a directory for the YAML file and move into it:
- mkdir hello-world
- cd hello-world
Then, you’ll create the YAML file:
- nano docker-compose.yml
Put the following contents into the file, save the file, and exit the text editor:
my-test: image: hello-world
The first line in the YAML file is used as part of the container name. The second line specifies which image to use to create the container. When you run the command
docker-compose up it will look for a local image by the name you specified,
hello-world. With this in place, you’ll save and exit the file.
You can look manually at images on your system with the
docker images command:
- docker images
When there are no local images at all, only the column headings display:
OutputREPOSITORY TAG IMAGE ID CREATED SIZE
Now, while still in the
~/hello-world directory, you’ll execute the following command:
- docker-compose up
The first time you run the command, if there’s no local image named
hello-world, Docker Compose will pull it from the Docker Hub public repository:
OutputPulling my-test (hello-world:latest)... latest: Pulling from library/hello-world c04b14da8d14: Downloading [==================================================>] c04b14da8d14: Extracting [==================================================>] c04b14da8d14: Extracting [==================================================>] c04b14da8d14: Pull complete Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9 Status: Downloaded newer image for hello-world:latest . . .
Note: If you run into a permission error regarding the Docker socket, this means you skipped Step 2 of How To Install and Use Docker on Ubuntu 18.04. Going back and completing that step will enable permissions to run docker commands without
After pulling the image,
docker-compose creates a container, attaches, and runs the hello program, which in turn confirms that the installation appears to be working:
Output. . . Creating helloworld_my-test_1... Attaching to helloworld_my-test_1 my-test_1 | my-test_1 | Hello from Docker. my-test_1 | This message shows that your installation appears to be working correctly. my-test_1 | . . .
Then it prints an explanation of what it did:
Output of docker-compose up. . . 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. . . .
Docker containers only run as long as the command is active, so once
hello finished running, the container stopped. Consequently, when you look at active processes, the column headers will appear, but the
hello-world container won’t be listed because it’s not running.
- docker ps
OutputCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
You can see the container information, which you’ll need in the next step, by using the
-a flag which shows all containers, not just the active ones:
- docker ps -a
OutputCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06069fd5ca23 hello-world "/hello" 35 minutes ago Exited (0) 35 minutes ago drunk_payne
This displays the information you’ll need to remove the container when you’re done with it.
To avoid using unnecessary disk space, you’ll remove the local image. To do so, you’ll need to delete all the containers that reference the image using the
docker rm command, followed by either the CONTAINER ID or the NAME. Below, you’re using the CONTAINER ID from the
docker ps -a command you just ran. Be sure to substitute the ID of your container:
- docker rm 06069fd5ca23
Once all containers that reference the image have been removed, you can remove the image:
- docker rmi hello-world
You’ve now installed Docker Compose, tested your installation by running a Hello World example, and removed the test image and container.
While the Hello World example confirmed your installation, the simple configuration does not show one of the main benefits of Docker Compose — being able to bring a group of Docker containers up and down all at the same time. To see the power of Docker Compose in action, you might like to check out this practical example, How To Configure a Continuous Integration Testing Environment with Docker and Docker Compose on Ubuntu 16.04 (note: this article is for Ubuntu 16.04 rather than 18.04)
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.Sign up
Click below to sign up and get $100 of credit to try our products over 60 days!