docker-compose编排web集群
启动两个nginx容器
编写docker-compose.yml
如果要编排的容器较多,建议使用目录将docker-compose.yml分隔开来
[root@localhost ~]# mkdir test
[root@localhost ~]# cd test/
[root@localhost test]# vim docker-compose.yml
nginx:
container_name: nginx91
image: nginx:latest
ports:
- "8091:80"
volumes:
- /www/wwwroot/8091:/usr/share/nginx/html
hostname: nginx.test.com
#
nginx-php:
container_name: nginx92
image: nginx:latest
ports:
- "8092:80"
volumes:
- /www/wwwroot/8092/:/usr/share/nginx/html
hostname: nginx-php.test.com
创建文件中volumes所制定的目录位置,并写入两个简单的页面
[root@localhost test]# mkdir /www/wwwroot/{8091,8092} -p
[root@localhost test]# echo "8091" >> /www/wwwroot/8091/index.html
[root@localhost test]# echo "8092" >> /www/wwwroot/8092/index.html
运行docker-compose文件
这些命令必须在存在docker-compose.yml文件的目录中执行,以下均是
[root@localhost test]# docker-compose up -d
Creating nginx91 ... done
Creating nginx92 ... done
文件中指定的镜像如果没有,会直接下载使用
查看启动的容器
[root@localhost test]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------
nginx91 nginx -g daemon off; Up 0.0.0.0:8091->80/tcp
nginx92 nginx -g daemon off; Up 0.0.0.0:8092->80/tcp
停止容器
[root@localhost test]# docker-compose stop
Stopping nginx92 ... done
Stopping nginx91 ... done
启动容器
[root@localhost test]# docker-compose start
Starting nginx ... done
Starting nginx-php ... done
删除容器
[root@localhost test]# docker-compose rm
Going to remove nginx92, nginx91
Are you sure? [yN] y
Removing nginx92 ... done
Removing nginx91 ... done
验证
可以访问映射的两个端口访问页面
[root@localhost test]# curl 192.168.1.14:8091
8091
[root@localost test]# curl 192.168.1.14:8092
8092