- Kubernetes can be used with a wide range of container systems among which Docker is the most used. For this reason, we will be looking at how to install docker first. Later In this article, we will learn how to install Kubernetes on CentOS 8. Note: If you are using Ubuntu, then refer to our article How to install Kubernetes on Ubuntu 20.04.
- The command above installs and enable Docker CE repo on CentOS 8. Install Docker Engine (Docker CE) on CentOS 8. Once the repos are in place, proceed to install docker-ce and other tools including containerd.io, an open and reliable container runtime on CentOS 8. Next, install Docker CE and Docker CE CLI utility.
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.
Now, this version of containerd.io is not available in CentOS 8. To avoid this dependency cycle and battling them manually, you can use the -nobest option of the dnf command. It will check the latest version of docker-ce but when it finds the dependency issue, it checks the next available version of docker-ce.
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:
Install docker-compose using pip3
Use pip to install 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:
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:
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:
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.
Docker Compose On Centos 8
Create a directory named 'nginx-compose' and cd into it:
Create a file named 'docker-compose.yml' and add the following lines
Docker Ce On Centos 8
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 will look in the current directory for a file named 'docker-compose.yml', parse it, and deploy the services defined in it.
Docker On Centos 8
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.
Comments are closed.