How To Install and Use Docker Compose on CentOS 8 Tutorial

How To Install and Use Docker Compose on CentOS 8 (Step by Step Tutorial). In this tutorial we will introduce Docker Compose, it’s use cases with it’s main advantages then move onto installation phase. We will also explain some basic Docker Compose commands that are used to manage the Docker containers.

Docker Compose is very powerful tool that helps developers to define and share multi container applications in a single YAML file. Docker Compose is software used for defining and running multi container Docker applications. With Docker Compose, you don’t need to manually create a container for each application. You can define and run all services using a single command docker-compose up or break it up using docker-compose down.

What Is Docker Compose?

The application you use constitutes several containers running distinctive services. However, starting and managing these containers is a very difficult process. That is why Docker developed a tool called Docker Compose that speeds up this process.

This container tool is widely used by the developers and operations teams to create and automate deploying applications in lightweight containers. It ensures that these applications work in different environments appropriately.

In a nutshell, Docker Compose can handle several containers concurrently in different environments, like production, staging, development, testing, and CI.

Let’s understand the working of the tool with the help of the example.

Myntra, a fashion eCommerce brand is a very similar website to Amazon. When you visit the website via your web browser, you have to go through several activities, like logging into your account, looking at a catalog, checking the product and so on. Behind these activities contains products such as account databases, product databases, cart databases, etc.

They are known as microservices. When you build more of these microservices into your environment, the more value it provides to the services in the containers. However, being a developer, it is your responsibility to jump from one container to another. It is performed by using Docker Compose as it connects different containers as a single service.

In Docker Compose, every service runs in isolation. However, they can interact with each other whenever required.

In this blog post about how To Install and Use Docker Compose on CentOS 8 (Step by Step Tutorial) is to see the use cases for Docker Compose. 

Use Cases Of Docker Compose

Docker includes some of the common use cases that are given below:

Development Environments– docker runs an application in an isolated environment compose command line tool.

Automated Testing Environments – Docker Compose supports automated testing, an essential part of CI/CD. It is capable of creating and destroying the required testing environment. It also enables developers to define and configure the environment required for running automated end to end testing using the most appropriate file of Docker Compose.

Development Environments – With the help of Docker Compose, you can start any project quickly and efficiently. It enables the developers to immediately sign up for new isolated development environments. Its software document configures all the service dependencies of an application. This way, you can start with one or multiple containers for each dependency using a single command.

Single Host Deployment – The containers in Docker Compose are developed to run on a single host. It is because they are traditionally focused on the development and testing workflows.

Benefits Of Docker Compose

The benefits of Docker Compose are as follows:

  • Single Host DeploymentIn Docker Compose, you can run everything on a single piece of hardware.
  • Straightforward Configuration – It uses a YAML script, that makes the process of configuration quick and effortless.
  • Increased Productivity – Organizations using Docker Compose tend to experience an increase in their productivity due to a reduction in the time taken to perform every task.
  • Makes Internal Communication Secure – It enables you to share all the services in a network they have created. It also creates an extra security layer for the app since you cannot access services externally.
  • Highly Portable And Supports CI/CD – Since Docker Compose constitutes all the services in its file, you can easily access and share the configuration. When you pull a YAML file and source code, it also enables you to launch the environment within a minute. It contributes to setting up and enabling an efficient CI/CD pipeline.
  • Efficient Use Of Users – In Docker Compose, you can host multiple isolated environments in a single host. This way, you tend to save a lot of resources. It also provides features for caching a configuration and reusing existing containers, thereby contributing to resource efficiency.
  • Preserves volume data when creating containers

Next in this post is to go through steps of how to install Docker Compose on CentOS 8.

How to Install Docker Compose CentOS 8

Prerequisites

  • A root user or a user with sudo privileges.

Install Docker Engine

Before installing Docker Compose, you will need to have Docker Engine installed on your server. If not installed, you can install it with the following command:

				
					dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
dnf install docker-ce -y
				
			

Once the Docker Engine is installed, start the Docker service and enable it to start at system reboot with the following command:

				
					systemctl start docker
systemctl enable docker
				
			

Install Docker Compose

First, install the curl command line utility using the following command:

				
					dnf install -y curl
				
			

Next, visit the Docker Compose Git Hub release page and download the latest version of Docker Compose binary using the following command:

				
					curl -L "https://github.com/docker/compose/releases/download/v2.6.0/docker-compose-linux-x86_64" -o /usr/local/bin/docker-compose
				
			

Once the download is completed, set the executable permission on the downloaded binary file using the following command:

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

Verify Docker version

Next, verify the Docker version with the following command:

				
					docker-compose --version
				
			

You should see the following output:

				
					Docker Compose version v2.6.0
				
			

Deploy Nginx with Docker Compose

Docker Compose allows you to create a YAML file to define all your container and start them easily with a just single command. In this section, we will show you how to install Nginx with Docker Compose.

First, create a project directory to hold the Docker Compose file:

				
					mkdir project
				
			

Next, navgate to the project directory and create a docker-compose.yaml file with the following command:

				
					cd project
nano docker-compose.yml
				
			

Add the following configuration:

				
					version: "3"

services:
    web:
        image: nginx
        ports:
            - 8000:80

				
			

Save and close the file when you are finished. Now, run the following command to start the Nginx container:

				
					docker-compose up -d
				
			

This command will download the Nginx image, start the Nginx container and expose it on port 8000:

				
					[+] Running 7/7
 ⠿ web Pulled                                                                                                                            6.0s
   ⠿ 42c077c10790 Pull complete                                                                                                          3.5s
   ⠿ 62c70f376f6a Pull complete                                                                                                          5.1s
   ⠿ 915cc9bd79c2 Pull complete                                                                                                          5.2s
   ⠿ 75a963e94de0 Pull complete                                                                                                          5.3s
   ⠿ 7b1fab684d70 Pull complete                                                                                                          5.4s
   ⠿ db24d06d5af4 Pull complete                                                                                                          5.5s
[+] Running 2/2
 ⠿ Network project_default  Created                                                                                                      0.1s
 ⠿ Container project-web-1  Started                                                                                                      1.3s

				
			

Access Nginx

Now, open your web browser and access your Nginx server using the URL http://your-server-ip:8000. You should see the Nginx test page on the following screen:

Docker Compose Basic Commands

In this section, we will show you how to use Docker Compose command to manage the Docker container.

If you want to check all running container, run the following command:

				
					docker-compose ps
				
			

You should see your Nginx container in the following output:

				
					NAME                COMMAND                  SERVICE             STATUS              PORTS
project-web-1       "/docker-entrypoint.…"   web                 running             0.0.0.0:8000->80/tcp, :::8000->80/tcp
				
			

To check the Docker container log, run the following command:

				
					docker container logs project-web-1
				
			

You will see the following output:

				
					/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/06/13 12:44:04 [notice] 1#1: using the "epoll" event method
2022/06/13 12:44:04 [notice] 1#1: nginx/1.21.6
2022/06/13 12:44:04 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/06/13 12:44:04 [notice] 1#1: OS: Linux 4.18.0-348.12.2.el8_5.x86_64
2022/06/13 12:44:04 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/06/13 12:44:04 [notice] 1#1: start worker processes
2022/06/13 12:44:04 [notice] 1#1: start worker process 31
2022/06/13 12:44:04 [notice] 1#1: start worker process 32

				
			

Stop a container

To stop the running container, run the following command:

				
					docker-compose stop
				
			

You should see the following output:

				
					[+] Running 1/1
 ⠿ Container project-web-1  Stopped                                                                                                     10.2s
				
			

Start Docker

To start the container again, run the following command:

				
					docker-compose start
				
			

Pause Docker

To pause the container, run the following command:

				
					docker-compose pause
				
			

Stop Remove Docker

To stop and remove all containers, run the following command:

				
					docker-compose down
				
			

You should see the following output:

				
					[+] Running 2/2
 ⠿ Container project-web-1  Removed                                                                                                      0.2s
 ⠿ Network project_default  Removed                                                                                                      0.1s

				
			

To get a list of all Docker Compose commands, run the following command:

				
					docker-compose --help
				
			

You should see the following output:

				
					Commands:
  build       Build or rebuild services
  convert     Converts the compose file to platform's canonical format
  cp          Copy files/folders between a service container and the local filesystem
  create      Creates containers for a service.
  down        Stop and remove containers, networks
  events      Receive real time events from containers.
  exec        Execute a command in a running container.
  images      List images used by the created containers
  kill        Force stop service containers.
  logs        View output from containers
  ls          List running compose projects
  pause       Pause services
  port        Print the public port for a port binding.
  ps          List containers
  pull        Pull service images
  push        Push service images
  restart     Restart containers
  rm          Removes stopped service containers
  run         Run a one-off command on a service.
  start       Start services
  stop        Stop services
  top         Display the running processes
  unpause     Unpause services
  up          Create and start containers
  version     Show the Docker Compose version information

				
			

 That’s it! Thanks for learning with us how To Install and Use Docker Compose on CentOS 8 (Step by Step Tutorial).

How To Install and Use Docker Compose on CentOS 8 Conclusion

In this post, we explained how to install Docker Compose on CentOS 8 server. We also explained how to use Docker Compose YAML file to start Nginx container. You can now use YAML file to define multiple services and start all of them with a single command.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x