Docker Compose is a simple Python program that helps in easy deployments of multiple docker containers on a server.
It uses a simple YAML file for service description.
Instead of the long
docker run . . . lines, you can just use
docker-compose up -dto deploy multiple services at once.
In this tutorial, I'll show you how to install Docker Compose on CentOS server. I'll also show you a quick example of using Docker Compose for deploying an NGINX server with specific configuration.
Installing docker-compose on CentOS
You need Docker installed beforehand. Make sure to install Docker on your CentOS system before installing Docker Compose.
docker-compose isn't packaged on either CentOS's official repositories or docker's rpm repository.
There are two ways to install Docker Compose on Linux:
- Download the Docker Compose Linux installer files and manually set it up
- Use PIP to install Docker Compose easily
Personally, I prefer using PIP to install Docker Compose. Since Docker Compose is basically a Python application, it makes sense to use PIP for installing it.
Let's see how to do it.
Install pip3 using the following command:
sudo dnf install python3-pip
Install docker-compose using pip3
Use pip to install docker-compose
pip3 install --user docker-compose
pip installs docker-compose in
~/.local/bin directory. You'll have to add it to the PATH environment variable so that you can run it from anywhere:
echo 'PATH="$PATH:$HOME/.local/bin"' >> ~/.bashrc
If you're using
Once you're done, either use source command
source ~/.bashrc or reopen the terminal (log out and log back in if it's a server).
Confirm docker-compose is installed using 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 test it out? Follow this simple tutorial and learn how to use Docker Compose.
Deploy a sample nginx server using docker-compose
Nginx is one of the most popular web servers, it's easily deployable via docker.
Let's compare the compose way, and the non-compose way
Deploying Nginx without Docker Compose
You can use
run command to easily deploy an 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've deployed a nginx server, with a container name "server", using an 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 go through the issues here:
You'll have to create the networks and volumes beforehand
Docker won't automatically create the network and volume. You'll have to create them beforehand.
Too many options to write each and every time
There are too many options to write. If it's a more complicated container like a database container or reverse proxy container or a nextcloud container, these options will just grow.
What if you're testing your build and don't want to preserve the volume after stopping the container? You'll have to manually remove the volume and network afterwards.
Deploying Nginx with Docker Compose
First, you'll have to create a compose file.
Create a directory named "nginx-compose" and cd into 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'll deploy a nginx server with exactly the same configurations as we did in the non-compose way. Deploy it using:
docker-compose up -d
docker-compose will look in the current directory for a file named "docker-compose.yml", parse it, and deploy the services defined in it.
Let's go over the issues of non-compose method now with compose method:
- No need to create the networks and volumes beforehand, docker-compose does that for you. You can add external volumes by setting external to true in each volume/network.
- Once you've written the .yaml file, no need to write down massive commands anymore. You can copy this file over to any other server that has a recent version of docker-compose installed and it'll run as expected with ease.
- You can use
docker-compose down -vcommand to stop and remove not just the containers, but also all the networks and volumes that docker-compose created. This helps in clean up in a development/test environment.
There are many other benefits to using docker-compose over
docker run, especially in production, which is out of the scope for today's article.
If you want to see something like that do let me know in the comment section below. Keep an eye out for our docker series for more guides like these.