1. What is container orchestration?
- By executing a file, multiple containers defined in the file are started in sequence, which is the container arrangement
- This file is the yaml file
2. Tools used to implement container orchestration
- docker-compose
3. Install docker-compose
[root@hya ~]# yum -y install docker-compose
4. Use of docker-compose command
-
Format: docker-compose [option]
-
Options
-
up: start the container defined in the yaml file
-
down: close the container defined in the yaml file
-
-f x.yaml: Specify the yaml file to be read by docker-compose
-
-d: perform operations in the background
-
ps: View the status of containers started based on docker-compose
-
logs: Check the log information in the process of starting the container for troubleshooting
-
5. Case: Write the docker-compose.yaml file to start two containers: nginx and redis, requiring the nginx container to communicate directly with the redis container
1) Create a yaml file
[root@hya ~]# vim docker-compose.yaml
version: '3'
services:
web:
image: nginx:1.17.10
ports:
- "80:80"
links:
- redis
redis:
image: redis:latest
2) Start the container
[root@hya ~]# docker-compose up -d
Creating root_redis_1 ... done
Creating root_redis_1 ...
Creating root_web_1 ... done
3) View the status of the container started based on the yaml file
[root@hya ~]# docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------
root_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
root_web_1 nginx -g daemon off; Up 0.0.0.0:80->80/tcp
4) View the log information in the process of starting the container
[root@hya ~]# docker-compose logs
Attaching to root_web_1, root_redis_1
web_1 | 192.168.253.1 - - [29/Jun/2020:07:41:12 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36" "-"
web_1 | 2020/06/29 07:41:12 [error] 8#8: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.253.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.253.110", referrer: "http://192.168.253.110/"
web_1 | 192.168.253.1 - - [29/Jun/2020:07:41:12 +0000] "GET /favicon.ico HTTP/1.1" 404 556 "http://192.168.253.110/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36" "-"
redis_1 | 1:C 29 Jun 2020 07:39:45.271 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5) Close the container started based on the yaml file
[root@hya ~]# docker-compose down
Stopping root_web_1 ... done
Stopping root_redis_1 ... done
Removing root_web_1 ... done
Removing root_redis_1 ... done
Removing network root_default
to sum up
1.Docker compose的使用非常类似于docker命令的使用,但是需要注意的是大部分的compose命令都需要到docker-compose.yml文件所在的目录下才能执行。
2.compose以守护进程模式运行加-d选项
$ docker-compose up -d
3.查看有哪些服务,使用docker-compose ps命令,非常类似于 docker 的ps命令
4.查看compose日志
$ docker-compose logs web(跟的是容器名)
5.停止compose服务
$ docker-compose stop
6.重启compose服务
$ docker-compose restart
7.kill compose服务
$ docker-compose kill
8.删除compose服务
$ docker-compose rm
9.如果你想单独启动一个服务
docker-compose up -d 指定服务名称
10.登录到某个容器中
例:docker-compose exec nginx bash 登录到nginx容器中
11.删除所有的容器镜像
docker-compose down 删除所有容器,镜像
12.构建镜像
例:docker-compose build nginx 构建镜像