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, executing a single command, it is possible to create and start all the services specified in the configuration.
Docker Compose can be useful for many different projects, including:
Development: with the Compose command line tools we 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 in. Compose provides a convenient way to manage isolated testing environments for a test suite. The full environment is defined in the Compose file.
Docker Compose was made on the Fig source code, a community project now unused.
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 -qO- https://get.docker.com/ | sh
One required step is to configure correctly the user for Docker. 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
Next, install python-pip:
# yum install -y python-pip
At this point, it is possible to install Docker Compose by executing a pip command:
# pip install docker-compose
Upgrade also all the Python packages on CentOS 7:
# yum upgrade python*
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 6d1ac219
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 move into it:
$ mkdir hello-world $ cd hello-world
Create a new YAML file:
$ $EDITOR docker-compose.yml
In this file paste the following content:
unixmen-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 unixmen-compose-test (hello-world:latest)... latest: Pulling from library/hello-world b04784fba78d: Pull complete Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f Status: Downloaded newer image for hello-world:latest Creating helloworld_unixmen-compose-test_1 ... Creating helloworld_unixmen-compose-test_1 ... done Attaching to helloworld_unixmen-compose-test_1 unixmen-compose-test_1 | unixmen-compose-test_1 | Hello from Docker! unixmen-compose-test_1 | This message shows that your installation appears to be working correctly. unixmen-compose-test_1 | unixmen-compose-test_1 | To generate this message, Docker took the following steps: unixmen-compose-test_1 | 1. The Docker client contacted the Docker daemon. unixmen-compose-test_1 | 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. unixmen-compose-test_1 | 3. The Docker daemon created a new container from that image which runs the unixmen-compose-test_1 | executable that produces the output you are currently reading. unixmen-compose-test_1 | 4. The Docker daemon streamed that output to the Docker client, which sent it unixmen-compose-test_1 | to your terminal. unixmen-compose-test_1 | unixmen-compose-test_1 | To try something more ambitious, you can run an Ubuntu container with: unixmen-compose-test_1 | $ docker run -it ubuntu bash unixmen-compose-test_1 | unixmen-compose-test_1 | Share images, automate workflows, and more with a free Docker ID: unixmen-compose-test_1 | https://cloud.docker.com/ unixmen-compose-test_1 | unixmen-compose-test_1 | For more examples and ideas, visit: unixmen-compose-test_1 | https://docs.docker.com/engine/userguide/ unixmen-compose-test_1 | helloworld_unixmen-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 finishes 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.
The post Docker Compose: How To Install on CentOS 7 appeared first on Unixmen.