What is Docker Compose? Summary of knowledge about Docker compose

To make application development easier and more convenient, many supporting technologies have been born. Docker in general and Docker Compose in particular are among those technologies. Today, TipsMake will learn specifically and in detail all the things you need to know about Docker Compose.

About Docker

First, to start we will learn what docker is.

Docker is a platform for developers and system administrators that provides them with the tools to build, run, and share programs using containers. Using containers to deploy programs is called containerization.

A normal program running on a server will have three layers from top to bottom: program (Application), Operating System and Physical Server.

The problem arises when we run many programs on the server, the impact from one program to another will occur (for example, sharing resources, libraries, .) leading to the consequence that the performance of the entire server is not high. Therefore, some methods have been proposed to solve the problem.

The first way is to use virtualization technology .

Instead of sharing a single OS, virtualization divides the system's physical resources into parts and then runs the operating system on those parts. Programs will run on the virtual operating systems that share those resources (called Guest OS). At this time, the OS running the virtualization system (called Host OS) only runs the program that manages the virtual machines (or hypervisor).

Some common virtualization support tools are VirtualBox, Vmware or Hyper-V.

The second way is to use containerization technology.

In this way, programs will run on containers and share the same OS. The container will contain all the components to create an environment that allows the application to run on it. For that reason, containers have some advantages such as: diversity, lightness, portability, easy to upgrade, replace, and scalability.

So Docker was created to provide and manage container application packages for the above containerization technology.

Docker was launched on the market in 2013 and up to now the technology has received cooperation and support from many corporations such as Red Hat, Microsoft, IBM, .

The program used to host containers is called Docker Engine .

One way to run a Docker Container is to use a file called a Dockerfile. This file contains the steps to build a Container. This build must be done on a computer that has the Docker Engine installed.

In addition, there are some pre-built component packages called Docker Images. An Image includes the operating system and the environments for the program that have been pre-packaged. We can download Images from Docker hub or shared images on the network. We can create a new Container by running an Image.

What is Docker Compose?

Docker Compose  is a tool for defining and running Docker programs using multiple containers. With Docker Compose, we use a YAML file to set up the services needed for the program. Finally, with a single command, we will create and start all the services from those settings.

Using Compose typically involves three steps:

1. Declare the program's environments in Dockerfile.

2. Declare the services needed for the program in the docker-compose.yml file so that the services can run together in one environment.

3. Run the docker-compose up command to start Compose and run the program.

Compose has commands that allow you to manage the lifecycle of your program:

Start, Stop and Rebuild the service.

View the status of running services.

View log output of running service.

Run a one-off command in a service.

Benefits of using Compose

Create multiple isolated environments on one host : Compose isolates the environments of projects to ensure they don't conflict with each other, and makes it easy to create copies of a particular environment.

Only recreate containers that have changed : Compose will recognize services that have not changed and reuse containers corresponding to those services.

Adjusting variables used for environments : Compose uses variables in the Compose file for environments. So for different environments or users, it is possible to adjust variables when using Compose to set up services.

Basic commands in Docker Compose

docker-compose up: Starts the containers defined in the docker-compose.yml file. If the image does not exist, it will be automatically downloaded or rebuilt from the Dockerfile.

docker-compose down: Stops and deletes all containers, networks, and volumes created by the up command.

docker-compose ps: Show the current state of containers.

docker-compose build: Create an image from a Dockerfile for each service defined in the configuration file.

docker-compose restart: Restart running containers.

docker-compose stop: Stop containers without deleting them.

docker-compose rm: Remove unused containers.

docker-compose logs: Displays logs of containers to track activity and errors.

docker-compose config: Shows the current Docker Compose configuration, helps to check if the configuration is valid.

docker-compose exec: Execute a command on a specific running container.

docker-compose port : Shows the port the service is using on the host.

docker-compose top: Shows a list of processes running in containers.

Install Docker Compose

Step 1: Install Docker

Before installing Docker Compose, you need to make sure that Docker is installed on your system. If not, you can install Docker using the following command:

sudo apt-get update

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

sudo apt-get update

sudo apt-get install docker-ce

Once installed, you can check the Docker version with the command: docker --version

Step 2: Install Docker Compose

  1. Download and install

You need to download Docker Compose from the official Docker repository. Here is the command to download the latest version (at the time of writing, it is 2.5.0):

sudo curl -L "https://github.com/docker/compose/releases/download/v2.5.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

  1. Grant execution rights

After downloading, you need to grant execution permission to the downloaded file: sudo chmod +x /usr/local/bin/docker-compose

  1. Check settings

To confirm that Docker Compose was successfully installed, run the following command:

docker-compose --version

You will get a message showing the version of Docker Compose.

Some common cases when using Compose

Compose can be used for many cases. Below will be two cases of using Compose in program development.

Development environment

When developing a program, it is essential to run a program in an isolated and interactive environment. Compose allows to set up and run all the services needed for the program. With just one docker-compose up command, those services will be run with the corresponding containers.

Environment for automated testing

With automated testing, creating an environment for using test packages with compose becomes very simple. Create an environment with Compose, run the test packages, then destroy that environment with just three lines of command:

$ docker-compose up

$ ./run_tests (Run automated test package)

How to use Docker Compose

Once installed, you can start using Docker Compose to manage containers. To do this, you need to create a docker-compose.yml file in your project directory. For example:

  1. Create project folder:

mkdir docker-project

cd docker-project

  1. Create docker-compose.yml file: nano docker-compose.yml
  2. Add content to docker-compose.yml file:

version: '3.9'

services:

  hello-world:

    image: hello-world:latest

  1. Run containers using the command: docker-compose up

In addition to the basic issues presented in the article, there are still many other interesting issues to learn about Docker and the technologies that support application development and construction. Let's follow more good articles on this topic on TipsMake.

According to TipsMake share

5 ★ | 1 Vote

May be interested