Preface
So the blogger here has some columns full of useful information!
The first is a summary of the blogger’s high-quality blogs. The blogs in this column are all written by the blogger with the most care. They are full of useful information. I hope it will be helpful to everyone.
Then there is the column that the blogger spends the most time on recently, "Docker from Realization to Practice to Underlying Principles". I hope everyone will pay more attention to it!
Docker Compose
1. Overview
Reference: Bit Employment Class
1.1 What is Docker Compose?
docker-compose is Docker's official open source project, written in python, and implemented by calling the API of the Docker service for container management and orchestration. Its official definition is an application that defines and runs multiple Docker containers.
There are two very important concepts in docker-compose:
- Service: An application container can actually include several container instances running the same image.
- Project: A complete business unit consisting of a set of associated application containers,
docker-compose.yml
defined in a file, anddocker-compose.yml
defining a project as a whole.
Compose
The default management object is the project, and subcommands are used to conveniently manage the life cycle of a group of containers in the project. compose
Multiple services can be managed conveniently .
1.2 Why Docker Compose
Docker is a lightweight application. Docker officially recommends that only one process be run in each Docker container.
If an application needs to involve MySQL, nginx and other environments, then we need to create separate docker containers for the application, database and nginx respectively, and then start the containers respectively. Imagine that after we build Docker, every time we start the application, we need to run docker at least three times, or write some scripts to implement it, which will be cumbersome.
In addition, these docker containers are scattered and independent, which makes image management inconvenient. Since these docker containers are for the same application service, we should put them together, which leads to docker-compose to solve this type of problem.
2. Installation of Docker Compose
We have already installed docker when we installed it. You can read the previous chapter (Chapter 1).
Check if it is installed.
root@ALiCentos7:~$ docker compose version
Docker Compose version v2.20.2
You have new mail in /var/spool/mail/root
root@ALiCentos7:~$
3. Introduction to the functions of Docker Compose
3.1 Usage steps
Steps to use Compose:
- Use
docker-compose.yml
definitions to form the services of your application so that they can run together in an isolated environment. - Finally, execute the docker compose up command to get the entire application up and running.
3.2 Core functions
Compose has commands to manage the entire life cycle of an application:
-
Start, stop and rebuild services
-
View the status of running services
-
Streaming the log output of a running service
-
Run a one-time command on the service
4. Docker Compose 文件(docker-compose.yml)
4.1 File syntax version
Currently, there are three official versions supported, namely Version 1, Version 2 and Version 3, of which Version 1 has been abandoned. The latest version is 3.8, and the Docker Engine version it supports must not be lower than 19.03.0.
Official documentation:
- https://docs.docker.com/compose/compose-file/compose-versioning/
4.2 Basic structure of files
version: "3.8" # 定义版本, 表示当前使用的 docker-compose 语法的版本
services: # 服务,可以存在多个
servicename: # 服务名字,它也是内部bridge 网络可以使用的DNS name,如果不是集群模式相当于 docker run的时候指定的一个名称
#集群(Swarm)模式是多个容器的逻辑抽象
image: # 必选,镜像的名字
command: # 可选,如果设置,则会覆盖默认镜像里的 CMD 命令
environment: # 可选,等价于 docker container run 里的 --env 选项设置环境变量
volumes: # 可选,等价于docker container run 里的 -v 选项 绑定数据卷
networks: # 可选,等价于 docker container run 里的 --network 选项指定网络
ports: # 可选,等价于 docker container run 里的 -p 选项指定端口映射
expose: # 可选,指定容器暴露的端口
build: #构建目录
depends_on: #服务依赖配置
env_file: #环境变量文件
servicename2:
image:
command:
networks:
ports:
servicename3:
#...
volumes: # 可选,等价于 docker volume create
networks: # 可选,等价于 docker network create
4.3 Common field format syntax
4.3.1 image
image: redis
image: redis:5
image:
redis@sha256:0ed5d5928d4737458944eb604cc8509e245c3e19d02ad83935398
bc4b991aac7
image: library/redis
image: docker.io/library/redis
image: my_private.registry:5000/redis
Prepare the directory first.
Write the configuration file.
Start everything in the configuration file.
A bridge network will be created by default, and the project-related containers will be added to the project's network, separate from the docker default network.
Stop and delete.
4.3.2 command
Override the default command for container startup.
command: ["bundle", "exec", "thin", "-p", "3000"]
command: bundle exec thin -p 3000
4.3.3 entrypoint
Override the container's default entrypoint.
Two formats.
entrypoint: /code/entrypoint.sh
entrypoint:
- php
- -d
- zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so
- -d
- memory_limit=-1
- vendor/bin/phpunit
4.3.4 environment
Add environment variables. You can use an array or dictionary, any boolean value, the boolean value needs to be enclosed in quotes to ensure that the YML parser does not convert it to True or False.
# map语法
environment:
RACK_ENV: development
SHOW: "true"
USER_INPUT:
# 数组语法
environment:
- RACK_ENV=development
- SHOW=true
- USER_INPUT
4.3.5 networks
Specify the network on which the container runs.
It's also easy to adjust the network's information. As shown in the example below.
services:
frontend:
image: awesome/webapp
networks:
front-tier:
ipv4_address: 172.16.238.10
networks:
front-tier:
ipam:
driver: default
config:
- subnet: "172.16.238.0/24"
4.3.6 volumes
Mount the host's data volume or file into the container.
#短语法 对应 -v 的精简的配置
services:
db:
image: postgres:latest
volumes:
- "/localhost/postgres.sock:/var/run/postgres/postgres.sock"
- "/localhost/data:/var/lib/postgresql/data"
#完整语法 对应 --mount 的详细的配置
services:
backend:
image: awesome/backend
volumes:
- type: volume
#命名卷
source: db-data
target: /data
volume:
nocopy: true
#绑定卷
- type: bind
source: /var/run/postgres/postgres.sock
target: /var/run/postgres/postgres.sock
volumes:
db-data:
4.3.7 ports
Port mapping is actually an option in the command -p
.
#完整语法
ports:
- target: 80
host_ip: 127.0.0.1 # 设置成 0.0.0.0 就是所有机器都能来访问
published: 8080
protocol: tcp
mode: host
- target: 80
host_ip: 127.0.0.1
published: 8000-9000
protocol: tcp
mode: host
#短语法
ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"
- "6060:6060/udp"
4.3.8 expose
The port is exposed but not mapped to the host and is only accessible by the connected service.
expose:
- "3000"
- "8000"
4.3.9 build
We can understand this parameter after learning dockerFile, so we won’t talk about it now.
4.3.10 depends_on
Set dependencies.
-
docker compose up: Start services in dependency order. In the following example, db and redis are started first, and then the web is started.
-
docker compose up SERVICE: Automatically include SERVICE dependencies. In the following example, docker compose up web will also create and start db and redis.
-
docker compose stop: Stop services in dependency order. In the following example, web is stopped before db and redis.
This parameter is more complicated, let’s take a look at an example.
version: "3.7"
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
Conditions can be specified. Healthy needs to be configured with healthcheck to complete.
services:
web:
build: .
depends_on:
db:
condition: service_healthy
redis:
condition: service_started
redis:
image: redis
db:
image: postgres
healthcheck case.
version: "3.8"
services:
web:
image: nginx:1.24.0
environment:
TEST: 1
depends_on:
mysql:
condition: service_healthy
mysql:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: "bit@123"
volumes:
- /data/maxhou/mysqldata/varlib/:/var/lib/mysql
healthcheck:
test: mysql --user=root --password='bit@123' -e "SELECT 1;"
interval: 10s
timeout: 5s
retries: 10
4.3.11 env_file
Add environment variables from file. Can be a single value or a list of multiple values.
env_file: .env
env_file:
- ./common.env
- ./apps/web.env
- /opt/secrets.env
5. Docker Compose command
5.1 Command list
Order | Function |
---|---|
docker compose build |
Build service |
docker compose config |
Canonical format to display service configuration |
docker compose cp |
Copy files directly to the local system and service container |
docker compose create |
Create a service container |
docker compose down |
Stop all containers and delete containers |
docker compose events |
Get real time from server |
docker compose exec |
Execute command in container |
docker compose images |
List the images used by all containers |
docker compose kill |
Container that is forced to stop service |
docker compose logs |
show log |
docker compose ls |
Show all items |
docker compose pause |
Service Unavailable |
docker compose port |
List all port mappings |
docker compose ps |
This command can list all containers currently in the project |
docker compose pull |
Pull service image |
docker compose push |
Push service image |
docker compose restart |
Restart or restart a service |
docker compose rm |
Delete a container with a stopped service |
docker compose run |
Execute related commands on the specified service container |
docker compose start |
Start a currently stopped container |
docker compose stop |
Stop a currently running container |
docker compose top |
Show running processes |
docker compose unpause |
Restoring service |
docker compose up |
The up command will build, (re)create, start, and link a service-related container. By default if containers already exist, it will stop and try to recreate them. and use the previously mounted volume. The –no-recreate parameter prevents the container from being stopped or re-created, and -d indicates running in the background. |
docker compose version |
View version |
5.2 Command format
For Compose, the object of most commands can be either the project itself or specified as a service or container in the project. Unless otherwise specified, the command object will be the project, which means that all services in the project will be affected by the command. The basic usage format of the docker-compose command is as follows.
docker compose [OPTIONS] COMMAND [ARGS...]
5.3 Common options
-
-f, --file
Specify the Compose template file to use. The default isdocker-compose.yml
. It can be specified multiple times. -
-p, --project-name
Specify the project name. By default, the name of the directory will be used as the project name.
5.4 Common commands
5.4.1 up
This command is very powerful. It will try to automatically complete a series of operations including building images, (re)creating services, starting services, and associating service-related containers. You can start a project directly through this command.
docker compose up [options] [SERVICE...]
parameter.
-d 在后台运行服务容器, 推荐在生产环境下使用该选项
--force-recreate 强制重新创建容器,不能与 --no-recreate 同时使用
--no-recreate 如果容器已经存在了,则不重新创建,不能与 --force-recreate 同时使用
5.4.2 down
Stop all containers and delete containers and networks.
docker compose down [options] [SERVICE...]
parameter.
-v, --volumes 删除容器同时删除目录映射
5.4.3 run
This command can execute related commands on the specified service container.
# 例如:启动一个 ubuntu 服务容器,并执行 ping docker.com 命令
# docker compose run ubuntu ping docker.com
docker compose run [options] SERVICE [COMMAND] [ARGS...]
parameter.
-d 后台运行容器
--name NAME 为容器指定一个名字
--entrypoint CMD 覆盖默认的容器启动指令
-e KEY=VAL 设置环境变量值,可多次使用选项来设置多个环境变量
-u, --user="" 指定运行容器的用户名或者 uid
--rm 运行命令后自动删除容器
-p, --publish=[] 映射容器端口到本地主机
6. Comprehensive cases
6.1 Comprehensive case one
- nginx reverse proxy to our web service to provide external browsing services
- Query mysql database user information and return user data
- Create user library and write user data
6.1.1 Write data initialization script
The design data is a very simple table information. Two pieces of data are written. The file isinit.sql
drop database if exists test;
CREATE DATABASE `test` DEFAULT CHARACTER SET utf8mb4 ;
use `test`;
CREATE TABLE `users` (
`sno` int(11) DEFAULT NULL,
`sname` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO users (sno,sname) VALUES (1,'pony'), (2,'maxhou');
This can be done manually. But we hope that every time the service is started, these will be run directly and my data will be initialized. So these things need to be written sql
to a file.
This is called a data initialization script!
6.1.2 Create a springboot application and configure the maven project
6.1.3 Add startup class
6.1.4 Configure database information
Note that during local testing, the red part is adjusted to the IP address, while our actual service uses a service name of mysql.
6.1.5 Configuring User Controller
Simple configuration, no longer involving mapper, the operations are completed directly through jdbc.
6.1.6 Compilation and packaging
6.1.7 Local testing
6.1.8 Writing docker-compose.yml
6.1.9 Create project directory and volume directory
6.1.10 Put nginx’s reverse proxy configuration bit.conf into ./nginx/conf.d
6.1.11 Place the database initialization file init.sql into the ./mysql/init directory
6.1.12 Put the application jar package into the ./app directory
6.1.13 Starting our project
6.2 Comprehensive Case 2
6.2.1 What is WordPress
WordPress is a blogging platform developed using PHP language. Users can set up their own website on a server that supports PHP and MySQL databases. You can also use WordPress as a content management system (CMS). WordPress is a personal blogging system that has gradually evolved into a content management system software. It is developed using the PHP language and MySQL database. Users can use their own blogs on servers that support PHP and MySQL databases.
WordPress has many free templates developed by third parties that are easy to install and use. However, to make your own template, you need to have certain professional knowledge. For example, you must at least understand an application's HTML code, CSS, PHP and other related knowledge under standard universal markup language. WordPress officially supports the Chinese version, and there are also third-party Chinese language packages developed by enthusiasts, such as the wopus Chinese language package. WordPress has thousands of various plugins and countless theme template styles.
6.2.2 Writing docker compose
version: "3.8"
services:
wordpress:
image: wordpress
restart: always
depends_on:
db:
condition: service_healthy
ports:
- 8080:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: mywordpressuser
WORDPRESS_DB_PASSWORD: mywordpresspass
WORDPRESS_DB_NAME: wordpress
volumes:
- ./wordpress/:/var/www/html
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: wordpress
MYSQL_USER: mywordpressuser
MYSQL_PASSWORD: mywordpresspass
volumes:
- ./mysqlvarlib/:/var/lib/mysql
healthcheck:
test: mysql -u root -proot -e "select 1;"
interval: 10s
timeout: 5s
retries: 10
Use this command to check whether there is any problem with the syntax of this file.
docker compose config
6.2.3 Start service + install blog system
docker compose up -d
Access directly using port 8080.
Enter something and then install it.
In fact, everything we have exists here.