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

Solution to the routing highlighting problem of Vue components

Preface Before, I used cache to highlight the rou...

How to use nginx to access local static resources on Linux server

1. Check whether port 80 is occupied. Generally, ...

Several methods to clear floating (recommended)

1. Add an empty element of the same type, and the...

Summary of WEBAPP development skills (notes for mobile website development)

1. To develop web responsively, the page must ada...

How to display percentage and the first few percent in MySQL

Table of contents Require Implementation Code dat...

Tutorial on using Webpack in JavaScript

Table of contents 0. What is Webpack 1. Use of We...

When the interviewer asked the difference between char and varchar in mysql

Table of contents Difference between char and var...

Detailed examples of using JavaScript event delegation (proxy)

Table of contents Introduction Example: Event del...

vue3 custom directive details

Table of contents 1. Registering custom instructi...

Set the width of the table to be fixed so that it does not change with the text

After setting the table width in the page to width...

Detailed tutorial on installing Docker and docker-compose suite on Windows

Table of contents Introduction Download and insta...

Detailed tutorial on installing CentOS, JDK and Hadoop on VirtualBox

Table of contents 1. Prerequisites 1.1 Supported ...

Index in MySQL

Preface Let's get straight to the point. The ...

MySQL Packet for query is too large problem and solution

Problem description: Error message: Caused by: co...

Solution to MySQL root password error number 1045

Stop MySQL Service Windows can right-click My Com...