Docker Compose is a simple Python program that helps you easily deploy multiple Docker containers to a server.
A simple YAML file is used to describe the service.
Instead of long docker runs. … … you can just use docker-compose up -d to deploy multiple services at the same time.
This tutorial will show you how to install Docker Compose on a CentOS server. We’ll also show you a quick example of using Docker Compose to deploy an NGINX server with a specific configuration.
Installing docker-compose on CentOS
You need to install Docker beforehand. Be sure to install Docker on your CentOS system before installing Docker Compose.
docker-compose is not included in the official CentOS repositories or the Docker rpm repository.
There are two ways to install Docker Compose on Linux:
- Download the Docker Compose Linux installation files and manually configure them
- Use PIP to Install Docker Compose Easily
Personally, we prefer to use PIP to install Docker Compose. Since Docker Compose is basically a Python application, it makes sense to use PIP to install it.
Let’s see how to do it.
Install pip3 using the following command:
sudo dnf install python3-pip
Install docker-compose using pip3
Using pip to install docker-compose
pip3 install --user docker-compose
pip installs docker-compose into ~ / .local / bin directory. You will need to add it to your PATH environment variable in order to run it from anywhere:
echo 'PATH="$PATH:$HOME/.local/bin"' >> ~/.bashrc
If you are using zsh, change .bashrc to .zshrc.
Once you’re done, either use the original command, source ~ / .bashrc, or open a terminal again (log out and log back in if it’s a server).
Make sure docker-compose is installed with the following command:
[email protected]:~$ docker-compose --version docker-compose version 1.26.2, build unknown
Congratulations! You now have docker-compose installed on your CentOS 8 server.
Want to check it out? Follow this simple tutorial and learn how to use Docker Compose.
Deploy an example nginx server with docker-compose
Nginx is one of the most popular web servers and is easy to deploy via docker.
Let’s compare the two ways.
Deploying Nginx without Docker Compose
You can use the run command to easily deploy the nginx server like this:
docker run --name server --network net -v html:/usr/share/nginx/html -v $PWD/custom-config.conf:/etc/nginx/nginx.conf -p 80:80 --restart on-failure -d nginx:latest
Here you have deployed an nginx server named “server” using the external network “net”, mounted a volume named “html” using a custom config file and listening on port 80 on the host, which will automatically restart on failure.
Let’s look at the questions here:
You will have to create networks and volumes in advance.
Docker will not automatically create network and volume. You will have to create them beforehand.
Too many options to write every time
There are too many options to write. If it is a more complex container, such as a database container, a reverse proxy container, or a nextcloud container, these options will only expand.
What if you are testing your build and you don’t want to keep the volume after the container stops? You will have to manually delete the volume and network after that.
Deploying Nginx with Docker Compose
First, you need to create a composition file.
Create a directory named “nginx-compose” and change to it:
mkdir nginx-compose && cd nginx-compose
Create a file named “docker-compose.yml” and add the following lines
version: "3.3" services: server: image : "nginx:latest" container_name: "server" volumes: - "html:/usr/share/nginx/html" - "./custom-config.conf:/etc/nginx/nginx.conf" networks: - "net" ports: - "80:80" restart: "on-failure" volumes: html: networks: net:
This compose file describes a service named server that will deploy an nginx server with exactly the same configurations as we did in easy mode. Expand it using:
docker-compose up -d
docker-compose will find a file named “docker-compose.yml” in the current directory, parse it, and deploy the services defined in it.
Let’s now look at the problems of the non-compositional method with the compose method:
- You don’t need to create networks and volumes in advance, Docker-compose does it. You can add external volumes by setting each volume / network for the external volume to true.
- Once you’ve written the .yaml file, you no longer need to write massive commands. You can copy this file to any other server that has the latest docker-compose installed and it will work as expected with ease.
- You can use the docker-compose down -v command to stop and remove not only containers, but all networks and volumes created by docker-compose. This helps in cleaning up in a development / test environment.
There are many other benefits of using docker-compose docker run, especially in a production environment, that are beyond the scope of this article.