OSETC TECH

How to Install and Use Docker Compose on CentOS 7 or RHEL 8

This post will guide you how to install and use Docker Compose tool on CentOS Linux 7 or RHEL Linux 8. How do I Install the latest version of Docker Compose on your CentOS system. How to setup multiple containers with Docker Compose tool on CentOS or RHEL system.

What is Docker Compose?


Docker compose is a command line tool to define and set up multiple containers docker applications. You can use docker compose to create multiple containers and deploy application from a single docker compose command.

And you can define a YAML file to configure your application’s service, networks and volumes, so that you can use a Docker Compose command to create and start all services from your configuration.

You can use Docker Compose to orchestrate the processes of Docker containers, including starting up, or shutting down Docker Containers.
For example, if you want to deploy a wordpress site inside a Docker Container, and you can create two containers, and one is Web server container and another is MySQL or Mariadb Container. The below method will explain that how to install Docker Compose on existing Docker host.

Install Docker Compose with PIP Tool


Assuming that you have install Docker tool on your CentOS system. And then you can start to install the Docker Compose. You can use the Pip tool to install Docker Compose on your system. Just do the following steps:

#1 Install the EPEL repository and then install python-pip package to install pip tool. Type the following command:

$ yum install epel-release –y
$ yum install python-pip –y
$ pip install --upgrade pip

Outputs:

[root@devops ~]# yum install epel-release
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
epel-release noarch 7-11 extras 15 k

Transaction Summary
================================================================================
Install 1 Package

Total download size: 15 k
Installed size: 24 k
Is this ok [y/d/N]: y
……

[root@devops ~]# yum install python-pip
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package python2-pip.noarch 0:8.1.2-8.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================
Package Arch Version Repository Size
=========================================================================================================================
Installing:
python2-pip noarch 8.1.2-8.el7 epel 1.7 M

Transaction Summary
=========================================================================================================================
Install 1 Package

Total download size: 1.7 M
Installed size: 7.2 M
Is this ok [y/d/N]:
……

[root@devops ~]# pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df8e8583ea85385d51125dceff/pip-19.0.3-py2.py3-none-any.whl (1.4MB)
100% |████████████████████████████████| 1.4MB 23kB/s
Installing collected packages: pip
Found existing installation: pip 8.1.2
Uninstalling pip-8.1.2:
Successfully uninstalled pip-8.1.2
Successfully installed pip-19.0.3

#2 install Docker Compose Package using Pip Tool, type the following command:

$ pip install docker-compose

Outputs:

[root@devops ~]# pip install docker-compose
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting docker-compose
Downloading https://files.pythonhosted.org/packages/51/56/5745e66b33
…….
Downloading https://files.pythonhosted.org/packages/68/9e/49196946aee219aead1290e00d1e7fdeab8567783e83e1b9ab5585e6206a/pycparser-2.19.tar.gz (158kB)
100% |████████████████████████████████| 163kB 51kB/s
Installing collected packages: six, websocket-client, docker-pycreds, idna, chardet, urllib3, certifi, requests, enum34, asn1crypto, pycparser, cffi, cryptography, pynacl, pyasn1, bcrypt, paramiko, docker, texttable, dockerpty, docopt, functools32, jsonschema, cached-property, docker-compose
Found existing installation: chardet 2.2.1
Uninstalling chardet-2.2.1:
Successfully uninstalled chardet-2.2.1
Running setup.py install for pycparser ... done
Running setup.py install for texttable ... done
Running setup.py install for dockerpty ... done
Running setup.py install for docopt ... done
Running setup.py install for functools32 ... done
Successfully installed asn1crypto-0.24.0 bcrypt-3.1.6 cached-property-1.5.1 certifi-2019.3.9 cffi-1.12.2 chardet-3.0.4 cryptography-2.6.1 docker-3.7.2 docker-compose-1.24.0 docker-pycreds-0.4.0 dockerpty-0.4.1 docopt-0.6.2 enum34-1.1.6 functools32-3.2.3.post2 idna-2.7 jsonschema-2.6.0 paramiko-2.4.2 pyasn1-0.4.5 pycparser-2.19 pynacl-1.3.0 requests-2.20.1 six-1.12.0 texttable-0.9.1 urllib3-1.24.1 websocket-client-0.56.0

#3 you can check the installation by verifiying the Docker’s version with the following command:

$ docker-compose –version

Outputs:

[root@devops ~]# docker-compose --version
docker-compose version 1.24.0, build 0aa5906

I just check the Docker’s GitHub, the latest stable version of Docker Compose is version 1.24.0. So the pip tool will download the latest Docker Compose build to install it on your CentOS Linux server.

Install Docker Compose from Docker’s GitHub Repository


If you want to install the latest version of Docker Compose on your CentOS system, you can also download the Docker Compose binary file from the Docker’s GitHub repository. Just run the following command to install Docker Compose on CentOS 7 or RHEL 8 Linux.

$ curl -L https://github.com/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

Outputs:

[root@devops ~]# sudo curl -L https://github.com/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 76 0 --:--:-- 0:00:08 --:--:-- 144
100 15.4M 100 15.4M 0 0 184k 0 0:01:25 0:01:25 --:--:-- 424k

Deploy Multiple containers with Docker Compose Tool


Once the Docker Compose installed on your CentOS system, you can start to deploy multiple Docker Containers for your WordPress site. And you need to create two Docker Containers for you applications. And each application runs one image in a docker container. Just do the following steps to launch a multiple-container application.

#1 Create your project directory called mywordpress, and change the current directory to it.

$ mkdir mywordpress
$ cd mywordpress

#2 create a YAML file called docker-compose.yml under your project directory with a text editor, such as: vi or vim

$ vim docker-compose.yml

Then put the following lines into the file, and save and close the file.

version: '3.0'

services:
webserver:
image: wordpress
container_name: myweb
ports:
- 8080:80
links:
- dbserver:mysql
environment:
WORDPRESS_DB_PASSWORD: 123456
dbserver:
image: mysql:latest
container_name: mydb
environment:
MYSQL_ROOT_PASSWORD: 123456

#3 run the following command to start up those two Docker Containers, type:

$ docker-compose up

Outputs:

[root@devops mywordpress]# docker-compose up
Creating network "mywordpress_default" with the default driver
Pulling dbserver (mysql:latest)...
latest: Pulling from library/mysql
27833a3ba0a5: Pull complete
864c283b3c4b: Pull complete
cea281b2278b: Pull complete
8f856c14f5af: Pull complete
9c4f38c23b6f: Pull complete
1b810e1751b3: Pull complete
5479aaef3d30: Pull complete
ded8fa2e1614: Pull complete
636033ba4d2e: Pull complete
902e6010661d: Pull complete
dbe44d2bf055: Pull complete
e906385f419d: Pull complete
Digest: sha256:a7cf659a764732a27963429a87eccc8457e6d4af0ee9d5140a3b56e74986eed7
Status: Downloaded newer image for mysql:latest
Pulling webserver (wordpress:)...
latest: Pulling from library/wordpress
27833a3ba0a5: Already exists
2d79f6773a3c: Pull complete
f5dd9a448b82: Pull complete
95719e57e42b: Pull complete
cc75e951030f: Pull complete
78873f480bce: Pull complete
1b14116a29a2: Pull complete
887fc426d9b4: Pull complete
e8a2a7e68e47: Pull complete
44116bd4b499: Pull complete
5a7ed133cf7c: Pull complete
a0cc2e7ce3b9: Pull complete
3ea943f2a6e6: Pull complete
b7cbb4ae8469: Pull complete
f1ee59d1627e: Pull complete
480e816f1b42: Pull complete
9803a14680f4: Pull complete
a9d5149d7240: Pull complete
7a162cc00537: Pull complete
Digest: sha256:eb7fb32d7098153b586cb6c0c8c7fb3b9684c66ccff803734b0895e49210fe08
Status: Downloaded newer image for wordpress:latest
Creating mydb ... done
Creating myweb ... done
Attaching to mydb, myweb

Note: The Docker-compose command try to look up image files by the name you specified in your docker-compose.yml from your local disk. If it do not find images from local disk, then it will pull images from the Docker Github repository. Then it will create two or multiple containers to setup the corresponding applications.

If you want to check the image file from your local disk, you can use the docker image command, type:

$ docker image

Outputs:

[root@devops mywordpress]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wordpress latest 837092bc87de 5 days ago 421MB
mysql latest 7bb2586065cd 2 weeks ago 477MB
centos latest 9f38484d220f 4 weeks ago 202MB

#4 you can check the container information by docker ps command with –a option to show all existing containers, type:

$ docker ps –a

Outputs:

[root@devops mywordpress]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cd481a9a04d7 wordpress "docker-entrypoint.s…" 19 minutes ago Exited (1) 19 minutes ago myweb
c1a7aaa8d227 mysql:latest "docker-entrypoint.s…" 19 minutes ago Exited (0) About a minute ago mydb
b28fed735ec8 centos "/bin/bash" 2 weeks ago Exited (137) 2 weeks ago infallible_khorana

Uninstall Docker Compose Tool


If you want to uninstall Docker Compose from your ubuntu system, just delete the docker-compose binary file with the following command:

$ sudo rm /usr/local/bin/docker-compose
$ sudo rm /usr/bin/docker-compose

If you install the docker compose with the pip tool, you just execute the following command to remove it, type:

$ pip uninstall docker-compose

Outputs:

[root@devops mywordpress]# pip uninstall docker-compose
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Uninstalling docker-compose-1.24.0:
Would remove:
/usr/bin/docker-compose
/usr/lib/python2.7/site-packages/compose/*
/usr/lib/python2.7/site-packages/docker_compose-1.24.0.dist-info/*
Proceed (y/n)? y
Successfully uninstalled docker-compose-1.24.0

Conclusion


You should know that how to use and install Docker Compose tool on CentOS 7 or RHEL 8. And you also should know that how to create multiple containers with Docker-compose tool on CentOS system. If you want to see more information about Docker Compose, you can go to theofficial web page of Docker Composedirectly.

See Also: