Solve the problem of managing containers with Docker Compose

Solve the problem of managing containers with Docker Compose

In Docker's design, a container runs only one application. However, most of the current application systems cannot be composed of just one application. Although the methods of connecting and exchanging data between containers have been mentioned before, these methods can indeed be used to build a container group for a complete application system. However, many commands need to be executed and the relationships between many applications and containers need to be considered. Docker Compose is designed to solve these complex operations.

Solving container management problems

Take the simplest example, if we want to prepare a MySQL container and a Redis container for our application container, then at each startup, we must first start the MySQL container and the Redis container, and then run the application container. Don't forget to connect the container network to the MySQL container and the Redis container when creating the application container so that the application can connect to them and exchange data.

This is not enough. If we have configured the container in various ways, we'd better save the commands for creating and configuring the container so that we can use them directly next time.

If we want this system to switch seamlessly like docker run and docker rm, it will be even more troublesome. We may need to write some scripts to avoid being trapped in the thread ball of commands.

In fact, the core is still lacking something to manage container combinations.

Docker Compose

Docker Compose: Define and run software in multiple containers. In Docker Compose, all applications and corresponding containers related to the application system are configured according to a configuration file, and then started according to the commands provided by Docker Compose, which can solve the complex problems between multiple containers mentioned above. Docker Compose can be understood as solidifying the operation mode and configuration of multiple containers, similar to the role of Dockerfile for images.

Install Docker Compose

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

#Set permissions chmod +x /usr/local/bin/docker-compose

#View the installed information docker-compose version

Basic use of Docker Compose

The core of Docker Compose is its configuration file, which is a file based on the YAML format. Just as Dockerfile uses the name Dockerfile as the default file name for the image build definition, the Docker Compose configuration file also has a default file name docker-compose.yml .
A simple configuration content

version: '3'
services:
	webapp:
		build: ./image/webapp
		ports:
			- "5000:5000"
		volumes:
			- ./code:/code
			-logvolume:/var/log
		links:
			-mysql
			- redis
	redis:
		image: redis:3.2
	mysql:
		image:mysql:5.7
		environment:
			-MYSQL_ROOT_PASSWORD=my-secret-pw
volumes:
	logvolume: {}

A Docker Compose configuration file can contain a lot of content, from detailed control of each container to the definition of networks, data volumes, etc.

version is the version number of Docker Compose, the latest one is 3. services are the core of the configuration and define the details of the container. Each service represents the configuration of an application cluster.

Start and Stop

start up
docker-compose up will create all the containers, networks, data volumes, etc. configured according to the configuration file and start them. Similar to the docker run command, both are started in the foreground. If you want to start it as a daemon, you also need to add -d

docker-compose up -d

docker-compose up will recognize the docker-compose.yml file in the directory where the current console is located by default. If you want to specify the directory, you can use the -f command, and if you want to specify the project name, you can use the -t command.

docker-compose -f ./compose/docker-compose.yml -p myapp up -d

stop
docker-compose down command is used to stop all containers and delete them, while also deleting configurations such as the network. This means that almost all effects of this Docker Compose project are removed from Docker.

Container Commands

These commands look similar to the commands used to operate a single container in Docker Engine. Let's look at a few common ones.
In Docker Engine, if we want to view the output of the main process in the container, we can use the docker logs command. Since the services running under Docker Compose are named automatically by Docker Compose, if we use docker logs directly, we need to find the name of the container first, which is obviously a bit troublesome. We can directly use the docker-compose logs command to complete this task.

docker-compose logs nginx

Similarly, there are several similar commands in Docker Compose that can control one or more services individually.
Through docker-compose create , docker-compose start and docker-compose stop we can achieve similar effects to docker create , docker start and docker stop , except that the objects of the operation are changed from containers in Docker Engine to services in Docker Compose.

This is the end of this article about using Docker Compose to manage containers. For more information about Docker Compose managing containers, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed process of building nfs server using Docker's NFS-Ganesha image
  • Detailed explanation of the process of using GPU in Docker

<<:  Detailed explanation of mysql filtering replication ideas

>>:  Vue advanced usage tutorial dynamic components

Recommend

MySQL uses events to complete scheduled tasks

Events can specify the execution of SQL code once...

Implementation of CSS3 3D cool cube transformation animation

I love coding, it makes me happy! Hello everyone,...

MySQL Interview Questions: How to Set Up Hash Indexes

In addition to B-Tree indexes, MySQL also provide...

Detailed explanation of Docker Secret management and use

1. What is Docker Secret 1. Scenario display We k...

MySQL scheduled backup solution (using Linux crontab)

Preface Although some love in this world has a pr...

How to use the jquery editor plugin tinyMCE

Modify the simplified file size and download the ...

MySQL 8.0.11 MSI version installation and configuration graphic tutorial

This article shares the installation and configur...

How to use Docker Swarm to build WordPress

cause I once set up WordPress on Vultr, but for w...

Detailed explanation of virtual DOM and diff algorithm in react

The role of virtual DOM First of all, we need to ...

Meta viewport makes the web page full screen display control on iPhone

In desperation, I suddenly thought, how is the Sin...

Implementation of automatic completion of Docker commands

Preface I don't know how long this friend has...

CSS implements six adaptive two-column layout methods

HTML structure <body> <div class="w...

JDBC-idea import mysql to connect java jar package (mac)

Preface 1. This article uses MySQL 8.0 version Co...

Summary of practical experience of HTML knowledge points

1. The table tag is table, tr is row, td is cell, ...