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
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
- 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
- Grant execution rights
After downloading, you need to grant execution permission to the downloaded file: sudo chmod +x /usr/local/bin/docker-compose
- 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:
- Create project folder:
mkdir docker-project
cd docker-project
- Create docker-compose.yml file: nano docker-compose.yml
- Add content to docker-compose.yml file:
version: '3.9'
services:
hello-world:
image: hello-world:latest
- 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
You should read it
- Docker best practices you need to know
- Docker Hub is used by hackers to spread Cryptojacking malware
- 10 Best Docker Alternatives 2022
- 6 reasons to use Docker virtualization software
- How to run Docker on Raspberry Pi
- 5 useful tips to learn Docker in 2018
- How to safely check desktop applications with Docker
- Containerize Go App with Docker
May be interested
- What is Golang? Why should you use Golang language?golang is an open source, compiled and designed programming language by google. it is built for simplicity, high performance, readability and efficiency.
- What is Codeigniter? Advantages and Disadvantages of Codeigniter Frameworkcodeigniter is an open source php framework designed for rapid web application development.
- Top best and most effective XML file reading software on computerthis article will introduce the best xml file reading software, helping you easily edit, add or delete content in xml files effectively.
- What is Vim? Detailed installation and usage instructionsvim stands for 'vi improved', is a text editor designed to optimize the user experience in compiling and editing source code.
- What is CSS3? What's new and different from traditional CSS?css3 is the latest version of the cascading style sheets (css) styling language, with many new features and improvements over previous versions, especially css2.
- Easy Photoshop Skin Beautification Tutorial for Beginnersskin retouching in photoshop is an important technique to make your photos more perfect. if you are new to this software, just follow these steps and you will easily beautify your skin professionally.