建 docker-compose 文件
volumes中挂载的目录当宿主机不存在时,会自动创建
vi docker-compose.yml
version: "2" # 使用Version 2
services: # 包含需要操作的容器
web1: # 容器的名称
image: nginx # 指定基于哪个镜像
ports: # 指定映射的端口
- "8080:80"
networks: # 指定使用哪个网络模式 - "net1"
volumes: # 指定挂载的的目录 - /data/www:/usr/share/nginx/html
web2:
image: nginx
ports: - "8081:80"
networks: - "net2"
volumes: - /data/www1:/usr/share/nginx/html
networks:
net1:
driver: bridge
net2:
driver: bridge
docker-compose文件内容区域
- services : 服务,定义应用需要的一些服务,每个服务都有自己的名字、使用的镜像、挂载的数据卷、所属的网络、依赖哪些其他服务等等
- networks : 网络,定义应用的名字、使用的网络类型等等
- volumes : 数据卷,定义的数据卷(名字等等),然后挂载到不同的服务下去使用
使用docker-compose 开始构建容器
[[email protected] opt]# docker-compose up -d
Creating network "opt_net2" with driver "bridge"
Creating network "opt_net1" with driver "bridge"
Creating opt_web2_1 ... done
Creating opt_web1_1 ... done
[[email protected] opt]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f959c27eb8b0 nginx "nginx -g ‘daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:8081->80/tcp opt_web2_1
60e8f1e1ba90 nginx "nginx -g ‘daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:8080->80/tcp
向web挂载目录添加内容
echo "webweb" > /data/www/index.html
echo "web11111" > /data/www1/index.html
查看容器状态
[[email protected] opt]# docker-compose ps
Name Command State Ports
opt_web1_1 nginx -g daemon off; Up 0.0.0.0:8080->80/tcp
opt_web2_1 nginx -g daemon off; Up 0.0.0.0:8081->80/tcp
停止已有的容器:
[[email protected] ~]# docker-compose stop
Stopping root_app1_1 ... done
Stopping root_app2_1 ... done
[[email protected] ~]#
启动已有的容器:
[[email protected] ~]# docker-compose start
Starting app2 ... done
Starting app1 ... done
[[email protected] ~]#
查看容器的状态:
[[email protected] ~]# docker-compose ps
Name Command State Ports
root_app1_1 /bin/sh -c /usr/local/ngin ... Exit 137
root_app2_1 tail -f /etc/passwd Exit 137
[[email protected] ~]#
删除容器:
[[email protected] ~]# docker-compose rm -f
Going to remove root_app1_1, root_app2_1
Removing root_app1_1 ... done
Removing root_app2_1 ... done
[[email protected] ~]#
停止并删除运行中的容器:
[[email protected] ~]# docker-compose down
Stopping root_app1_1 ... done
Stopping root_app2_1 ... done
Removing root_app1_1 ... done
Removing root_app2_1 ... done
Removing network root_net2
Removing network root_net1
[[email protected] ~]# docker-compose ps
Name Command State Ports
[[email protected] ~]#
原文地址:http://blog.51cto.com/10158955/2152512
时间: 2024-10-29 16:44:35