Article Directory
definition
Docker Compose is a container orchestration tool used to define and run applications composed of multiple containers. Using compose,
each service of the application can be defined through the yaml file, and the creation and startup of the application can be completed by a single command. Allows users to define a group of associated containers in a template (yaml format), which will sort the startup priority according to parameters such as -link.
One: Docker Compos container orchestration
YUML is a very intuitive data serialization format for markup language
File format and preparation notes
Tab key indentation is not supported, and space indentation is required
Usually indent two spaces at the beginning
Indent 1 space after the character, such as colon, comma, bar
Comment with # sign
Use single quotes if it contains special characters
Boolean values must be enclosed in quotes
1.1: Docker Compose configuration common fields
1.2: Docker Compose commonly used commands
docker-compose -h # 查看帮助
docker-compose up # 创建并运行所有容器
docker-compose up -d # 创建并后台运行所有容器
docker-compose ps # 显示所用容器信息
docker-compose -f docker-compose.yml up -d # 指定模板
docker-compose down # 停止并删除容器、网络、卷、镜像。
docker-compose rm # 删除容器
docker-compose logs # 查看容器输出日志
docker-compose pull # 拉取依赖镜像
dokcer-compose config # 检查配置
dokcer-compose config -q # 验证yaml配置文件是否正确,有问题才有输出
docker-compose pause # 暂停容器
docker-compose unpause # 恢复暂停
docker-compose restart # 重启服务
docker-compose start # 启动服务
docker-compose stop : # 停止服务
1.3: Compose command description
- Basic usage format
docker-compose [options][COMMAND][ARGS...]
- docker-compose option
--verbose 输出更多调试信息
--version 打印版本并退出
-f,--file FILE使用特定的compose模板文件,默认为docker-compose.yml
-p,--project-name NAME指定项目名称,默认使用目录名称
2: Arrangement examples
2.1: Download docker-compose
[root@localhost ~]# rz -E
rz waiting to receive.
#给下载好的docker-compose赋予权限
[root@localhost ~]# chmod +x docker-compose
#将下载好的docker-compose复制到/usr/local/bin目录下
[root@localhost ~]# cp -p docker-compose /usr/local/bin/
2.2: Create a working directory
(Copy the nginx software package to the nginx directory)
[root@localhost compose_nginx]# mkdir nginx
[root@localhost compose_nginx]# cd nginx/
[root@localhost nginx]# vim Dockerfile
FROM centos:7
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src
WORKDIR /usr/local/src
WORKDIR nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
'//编写启动脚本'
[root@localhost nginx]# vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
#把nginx包复制到nginx目录
[root@localhost nginx]# ls
Dockfile nginx-1.12.0.tar.gz run.sh
2.3: Create and edit yml files
[root@localhost nginx]# cd ..
[root@localhost compose_nginx]# vim 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:
2.4: execute open, tree view structure
#执行开启
[root@localhost compose_nginx]# docker-compose -f docker-compose.yml up -d
[root@localhost compose_nginx]# ls
docker-compose.yml nginx wwwroot
#在站点目录下创建一个网页,用浏览器访问是否可以正常显示
[root@localhost compose_nginx]# cd wwwroot/
'//编写站点信息'
[root@localhost wwwroot]# vim index.html
<h1>this is shuai web</h1>
#下载tree查看组织结构
[root@localhost compose_nginx]# yum install tree -y
[root@localhost compose_nginx]# tree ./
./
├── docker-compose.yml
├── nginx
│ ├── Dockerfile
│ ├── nginx-1.12.0.tar.gz
│ └── run.sh
└── wwwroot
└── index.html
- Check whether the image and container are both creatively successful:
[root@localhost compose_nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
compose_nginx_nginx latest 178d32644ae3 13 minutes ago 480MB
centos 7 7e6257c9f8d8 6 weeks ago 203MB
#查看容器
[root@localhost compose_nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0874ae4fe802 compose_nginx_nginx "/run.sh" 13 minutes ago Up 13 minutes 0.0.0.0:1216->80/tcp, 0.0.0.0:1217->443/tcp compose_nginx_nginx_1
8fb37f271c31 centos:7 "/bin/bash" 2 hours ago Up 2 hours niu
2.5: Browser access (the IP address of the machine is): 20.0.0.42:1216
Thank you for watching this experiment.