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

Upgrade MySQL 5.1 to 5.5.36 in CentOS

This article records the process of upgrading MyS...

How to simulate network packet loss and delay in Linux

netem and tc: netem is a network simulation modul...

Keepalived implements Nginx load balancing and high availability sample code

Chapter 1: Introduction to keepalived The purpose...

Example code for implementing a pure CSS pop-up menu using transform

Preface When making a top menu, you will be requi...

Detailed explanation of MySQL combined index and leftmost matching principle

Preface I have seen many articles about the leftm...

Use viewport in meta tag to define screen css

<meta name="viewport" content="w...

SQL implementation of LeetCode (183. Customers who have never placed an order)

[LeetCode] 183.Customers Who Never Order Suppose ...

VSCode Development UNI-APP Configuration Tutorial and Plugin

Table of contents Written in front Precautions De...

WeChat applet learning notes: page configuration and routing

I have been studying and reviewing the developmen...

MySQL-group-replication configuration steps (recommended)

MySQL-Group-Replication is a new feature develope...

Should I use UTF-8 or GB2312 encoding when building a website?

Often when we open foreign websites, garbled char...

Solution to many line breaks and carriage returns in MySQL data

Table of contents Find the problem 1. How to remo...

A brief talk about cloning JavaScript

Table of contents 1. Shallow cloning 2. Deep clon...

Implementation of FIFO in Linux process communication

FIFO communication (first in first out) FIFO name...