How to Install Docker Compose on CentOS 7

 

In this tutorial, we will cover the steps needed for installing Docker Compose on a CentOS 7 VPS, as well as go over a few commands you can do with Docker Compose.

Docker Compose is a tool designed to run multiple Docker applications at the same time. Compose can be used to run a stand-alone application as well as communicate with the other containers present on the same host. With Compose you use a YAML file to configure all of your Docker containers and configurations all in one place.

Table of Contents

Prerequisites

  • For the purposes of this tutorial, we will use a CentOS 7 VPS.
  • Full SSH root access or a user with sudo privileges is also required.
  • Have Docker already installed

Let’s begin with the installation.

Step 1: Connect via SSH and Update

Connect to your server via SSH as the root user using the following command:

ssh [email protected]IP_ADDRESS -p PORT_NUMBER

Remember to replace “IP_ADDRESS” and “PORT_NUMBER” with your server’s respective IP address and SSH port number.

Before starting with the installation, you will need to update your system packages to their latest versions. It’s easy to do, and it won’t take more than a few minutes.

You can do this by running the following command:

sudo yum -y update

Once the updates are completed, we can move on to the next step.

Step 2: Install Docker Compose on CentOS 7

We will get the latest version of Docker Compose by installing it from the official Docker GitHub repository. To install Docker Compose on CentOS 7, please follow these steps:

We will start downloading the Docker Compose binary into the /usr/local/bin directory using the ‘curl’ command:

sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Once the download is completed, we will set the permissions for the Compose binary:

sudo chmod +x /usr/local/bin/docker-compose

We can then verify the installation by checking the Compose version with the following command:

docker-compose --version

The output should look to the following:

docker-compose version 1.24.0, build h16727c

We have installed Docker Compose successfully. Now we will show you some useful Docker Compose commands.

Step 3: Using the Docker Compose Command

In this part, we will show you how to use the Docker Compose command to create a container with Docker Compose.

You can see the options available for a specific command by executing the following command:

docker-compose docker-subcommand --help

If you execute the following command:

docker-compose up --help

The output should look similar to the following:

Usage: up [options] [--scale SERVICE=NUM...] [SERVICE...]

Options:
-d, --detach                                  Detached mode: Run containers in the background, print new container names. Incompatible with
                                              --abort-on-container-exit.
--no-color                                    Produce monochrome output.
--quiet-pull                                  Pull without printing progress information
--no-deps                                     Don't start linked services.
--force-recreate                              Recreate containers even if their configuration
                                              and image haven't changed.
--always-recreate-deps                        Recreate dependent containers.
                                              Incompatible with --no-recreate.
--no-recreate                                 If containers already exist, don't recreate
                                              them. Incompatible with --force-recreate and -V.
--no-build                                    Don't build an image, even if it's missing.
--no-start                                    Don't start the services after creating them.
--build                                       Build images before starting containers.
--abort-on-container-exit                     Stops all containers if any container was
                                              stopped. Incompatible with -d.
-t, --timeout TIMEOUT                         Use this timeout in seconds for container
                                              shutdown when attached or when containers are
                                              already running. (default: 10)
-V, --renew-anon-volumes                      Recreate anonymous volumes instead of retrieving
                                              data from the previous containers.
--remove-orphans                              Remove containers for services not defined
                                              in the Compose file.
--exit-code-from SERVICE                      Return the exit code of the selected service
                                              container. Implies --abort-on-container-exit.
--scale                                       SERVICE=NUM Scale SERVICE to NUM instances. Overrides the
                                              `scale` setting in the Compose file if present.

Step 4: Running a Container with Docker Compose

The Docker Hub is the largest library and community for container images. It is a public Docker registry in which Docker users can create, test, store and distribute container images. We will use a Hello World image for testing purposes.

First, we will create a directory for the YAML file with the following commands:

mkdir hello_world
cd hello_world

Now we will create the YAML file by opening it with our text editor of choice, ‘nano’. You can use your preferred text editor instead if you like:

nano docker-compose.yml

We will put the following content into the file. Once that is done, save and close the file.

test:
   image: hello-world

The first line is showing the container name and the second line specifies which image to use.

If you want to manually see which images you can use on your system, use the following command:

docker images

If there are no local images it will show only the headings.

REPOSITORY               TAG               IMAGE                ID               CREATED SIZE

Next, we will run the Hello World image by executing the following command:

docker-compose up

If there is no local image for Hello World, Compose will pull it from the Docker Hub repository.

Pulling test (hello-world:)...
latest: Pulling from library/hello-world
1bgdsg30624: Pull complete
Creating hello_test_1_837gdsagc86e3 ... done
Attaching to hello_test_1_gjhkla48cd535e

Docker Compose will then create a container and run the hello-world program.

You can see the container information using the following command:

docker ps -a

The output should be similar to this:

CONTAINER ID IMAGE        COMMAND CREATED            STATUS                        PORTS      NAMES
751100d37d87 hello-world "/hello" About a minute ago Exited (0)About a minute ago             hello_test_1_gjhkla48cd535e

If you need to remove the container for some reason, you can run the following command:

docker rm 751100d37d87

Essentially removing the container with the ID that you specified. Once the container has been removed, you can remove the image with the following command:

docker rmi hello-world

That’s all – in this tutorial, we learned how to install Docker Compose on CentOS 7 as well as how to run a simple hello-world program. The possibilities of the program span much further than we can show in one tutorial, so make sure to look online for more uses.