Play docker automatic orchestration tool-docker compose
Foreword
The last two articles are replay Dockerfile, mainly based on Dockerfile to build various service images. This article will introduce and demonstrate the concept and use of docker's automatic orchestration tool-docker compose.
What is docker compose?
When we used Dockerfile to build the image, after writing the Dockerfile and other necessary files, we need to use the docker build, docker run and other commands to operate {create, start, stop, etc.} the container. However, in a microservice architecture system, an application system generally contains several servers, and each microservice usually deploys multiple instances. If each service has to be manually started and stopped, this is too inefficient and maintenance is very inconvenient.
Therefore, with docker compose, it can easily and efficiently manage containers and is an application tool for defining and running multi-container docker.
Let's use the following docker compose tool in combination with practical examples. After that, we will give an introduction to the fields commonly used in the orchestration file and related commands for docker-compose.
Build nginx container service based on docker compose tool
First, we need to create a directory, which needs to contain a nginx directory to build the Dockerfile, the home page test directory and the docker-compose.yml file
Therefore, on the one hand, we need the docker installation environment and the docker compose tool;
Furthermore, it is still necessary to write the Dockerfile and running script of nginx service, combined with the specified page directory to test and verify;
Next, write the docker-compose.yml file in yml format and execute the corresponding command.
The following is the directory structure of this case
[root@localhost opt]# tree compose_nginx/
compose_nginx/
├── docker-compose.yml
├── nginx
│?? ├── Dockerfile
│?? └── nginx.sh
└── wwwroot
└── index.html
2 directories, 4 files
There is no need to change the Dockerfile and the corresponding file, all we need to do is write a home page
[root@localhost opt]# cat compose_nginx/wwwroot/index.html
<h1>this is test web</h1>
Then write the docke-compose.yml file
[root@localhost opt]# cat compose_nginx/docker-compose.yml
version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 1216:80
- 1217:443
networks:
- cluster
volumes:
- ./wwwroot:/usr/local/nginx/html
networks:
cluster:
Explanation of docker-compose.yml file
version:声明版本,目前是3
service:具体的服务
nginx:服务名称
hostname:容器主机名称
build:创建
context:提供资源和素材提供的目录,用来创建容器的路径
dockerfile:Dockerfile文件
ports:提供的端口,相当于执行docker run -p的设置
networks:网络名称,如果是群集那么这个名称设置需要一致(同一个网络环境)
volumes:数据卷,本次案例是提供首页页面
networks:services的子选项针对外公开的网络名称
Start running
[root@localhost opt]# docker-compose -f compose_nginx/docker-compose.yml up -d
#-f——指定文件 -d——后台 up——启动
....//第一次执行过程比较久,显示信息多,这里就省略了。
The results of the operation and some explanations are as follows
If the above command is executed repeatedly, the following result will appear, so this WARNING is a friendly reminder!
[root@localhost opt]# docker-compose -f compose_nginx/docker-compose.yml up -d
compose_nginx_nginx_1 is up-to-date
View images and containers
[root@localhost opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
compose_nginx_nginx latest 67f9a64cc32d 5 minutes ago 587MB
centos 7 5e35e350aded 5 months ago 203MB
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8f3d48d7c765 compose_nginx_nginx "/nginx.sh" 5 minutes ago Up 5 minutes 0.0.0.0:1216->80/tcp, 0.0.0.0:1217->443/tcp compose_nginx_nginx_1
Test verification: