Docker is an application that simplifies the process of managing application in containers. A great tool for deployment of application inside containers. To take full advantage of its potential, each component of an application should run in its own individual container.
The Docker community came up with 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. 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.
- One Ubuntu 18.04 server set up, including a sudo non-root user and a firewall.
- Docker installed in the system.
Docker Compose Installation
Check the current release and if necessary, update it in the command below:
sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
We’ll set the permissions:
sudo chmod +x /usr/local/bin/docker-compose
We’ll verify that the installation was successful by checking the version:
This will print out the version we installed:
docker-compose version 1.21.2, build a133471
Now that we have Docker Compose installed, we’re ready to run a “Hello World” example.
Running a Container with Docker Compose
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:
we’ll create a directory for the YAML file and move into it:
mkdir hello-world cd hello-world
Then, we’ll create the YAML file:
Put the following contents into the file, save the file, and exit the text editor:
version: '3' services: my-test: image: hello-world:latest
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 we run the command
docker-compose up it will look for a local image by the name we specified,
hello-world. With this in place, we’ll save and exit the file.
We can look manually at images on our system with the
docker images command:
Now, while still in the
~/hello-world directory, we’ll execute the following command:
The first time we run the command, if there’s no local image named
hello-world, Docker Compose will pull it from the Docker Hub public repository:
Pulling 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 . . .
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:
. . . 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.
We can see the container information, which we’ll need in the next step, by using the
-a flag which shows all containers, not just the active ones:
docker ps -a
Hurray! We’ve now installed Docker Compose, tested our installation by running a Hello World example.