Docker Compose: How To Install on CentOS 7

Docker Compose – CentOS7
linuxworlds.com

What is Docker Compose

Containerization is dominated by Docker. Docker Compose is a tool for running multi-container Docker applications. To configure an application’s services with Compose we use a configuration file, and then, execute a single command. It is possible to create and start all the services specified in the configuration.

Docker Compose can be useful for many projects, including:

Development: We develop with Compose on the command line using tools. We use CLI tools to create (and interact with) an isolated environment which will host the application being developed. By using the Compose file, developers document and configure all of the application’s service dependencies.

Automated Testing: This use case requires an environment for running tests. Compose provide a convenient way to manage isolated testing environments for a test suite. The full environment is defined in the Compose file.

In this tutorial we will see how to install Docker Compose on a CentOS 7 Server.

Install Docker

First of all, install Docker. The easiest way to install it is to download an installation script provided by the Docker project:

$ wget -q0- https://get.docker.com/ | sh

You need to correctly provision the Docker user. In particular, add the user to the docker group by executing the following command:

# usermod -aG docker $(whoami)

Log out and log in again to update the user groups list.

Next, enable Docker to start at boot time:

# systemctl enable docker

Start Docker:

# systemctl start docker

Install Docker Compose

Once Docker has been installed, install Docker Compose. First of all, install the EPEL repository by executing the command:

# yum install epel-release -y

Next, install python-pip:

# yum install python-pip -y

AT this point, it is possible to install Docker Compose by executing a pip command:

# pip install docker-compose

Upgrade all the Python packages on CentOS 7:

# yum upgrade python* -y

Check Docker Compose version with the following command:

$ docker-compose -v

The output should be something like this:

docker'compose version 1.16.1, build 320a2a3

Testing Docker Compose

The Docker Hub includes a Hello World image for demonstration purposes, illustrating the configuration required to run a container with Docker Compose

Create a new directory and change directory into it:

$ mkdir hello-world && cd hello-world

Create a new YAML file:

$ vi docker-compose.yml

In this file paste the following content:

linuxworlds-compose-test:
  image: hello-world
Note: the first line is used as part of the container name

Save and exit. Run the container

Next, execute the following command in the hello-world directory:

$ sudo docker-compose up

If everything is correct, this should be the output shown by Compose:

Pulling linuxworlds-compose-test (hello-world:latest)...
latest: Pulling from library/hello-world
a3dk92dbu23b: Pull complete
Digest: sha256:f3b3b28sdo2830982u9a098208828519430e9e6d6ffc09d72261b0d2625fff
Status: Downloaded newer image for hello-world:latest
Creating helloworld_linuxworlds-compose-test_1 ... 
Creating helloworld_linuxworlds-compose-test_1 ... done
Attaching to helloworld_linuxworlds-compose-test_1
linuxworlds-compose-test_1 | 
linuxworlds-compose-test_1 | Hello from Docker!
linuxworlds-compose-test_1 | This message shows that your installation appears to be working correctly.
linuxworlds-compose-test_1 | 
linuxworlds-compose-test_1 | To generate this message, Docker took the following steps:
linuxworlds-compose-test_1 | 1. The Docker client contacted the Docker daemon.
linuxworlds-compose-test_1 | 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
linuxworlds-compose-test_1 | 3. The Docker daemon created a new container from that image which runs the
linuxworlds-compose-test_1 | executable that produces the output you are currently reading.
linuxworlds-compose-test_1 | 4. The Docker daemon streamed that output to the Docker client, which sent it
linuxworlds-compose-test_1 | to your terminal.
linuxworlds-compose-test_1 | 
linuxworlds-compose-test_1 | To try something more ambitious, you can run an Ubuntu container with:
linuxworlds-compose-test_1 | $ docker run -it ubuntu bash
linuxworlds-compose-test_1 | 
linuxworlds-compose-test_1 | Share images, automate workflows, and more with a free Docker ID:
linuxworlds-compose-test_1 | https://cloud.docker.com/
linuxworlds-compose-test_1 | 
linuxworlds-compose-test_1 | For more examples and ideas, visit:
linuxworlds-compose-test_1 | https://docs.docker.com/engine/userguide/
linuxworlds-compose-test_1 | 
helloworld_linuxworlds-compose-test_1 exited with code 0

Docker containers only run as long as the command is active, so the container will stop when the test finish running.

Conclusion

In this tutorial, we have seen how to install and test Docker Compose on a CentOS 7 server, and used the Compose file in the YAML format.