谈到微服务的话题,技术上我们往往会涉及到多服务、多容器的部署与管理。
清原网站建设公司成都创新互联公司,清原网站设计制作,有大型网站制作公司丰富经验。已为清原成百上千提供企业网站建设服务。企业网站搭建\成都外贸网站建设要多少钱,请找那个售后服务好的清原做网站的公司定做!
Docker 有三个主要的作用:Build, Ship和Run。使用docker compose我们可以在Run的层面解决很多实际问题,如:通过创建compose(基于YUML语法)文件,在这个文件上面描述应用的架构,如使用什么镜像、数据卷、网络、绑定服务端口等等,然后再用一条命令就可以管理所有的服务(如启动、停止、重启、日志监控等等)。
本文通过发布3个APP(App1,App2,App3),来演示Docker在服务发布、网络、共享分区以及信息隔离与连通方面的工作:
1. 安装Compose:
- [root@docker ~]# curl -L "https://github.com/docker/compose/releases/download/1.9.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- [root@docker ~]# docker-compose version
- docker-compose version 1.9.0, build 2585387
- docker-py version: 1.10.6
- CPython version: 2.7.9
- OpenSSL version: OpenSSL 1.0.1t 3 May 2016
2. 服务物理框架
说明:
3. docker-compose.yml 内容
- [root@docker compose]# more docker-compose.yml
- version: '2'
- services:
- App1:
- image: nginx
- ports:
- - "8080:80"
- networks:
- - "netName1"
- volumes:
- - /opt/conf/:/mnt
- App2:
- image: nginx
- ports:
- - "8081:80"
- networks:
- - "netName1"
- volumes:
- - /opt/conf/:/mnt
- App3:
- image: nginx
- ports:
- - "8082:80"
- networks:
- - "netName2"
- networks:
- netName1:
- driver: bridge
- netName2:
- driver: bridge
4. 运行
- [root@docker compose]# docker-compose up
- Creating network "compose_netName1" with driver "bridge"
- Creating network "compose_netName2" with driver "bridge"
- Creating volume "compose_vol1" with local driver
- Creating compose_App2_1
- Creating compose_App1_1
- Creating compose_App3_1
在后台运行:[root@docker compose]# docker-compose up -d
5. 查看容器运行状态
- [root@docker compose]# docker-compose ps
- Name Command State Ports
- -----------------------------------------------------------------------------
- compose_App1_1 nginx -g daemon off; Up 443/tcp, 0.0.0.0:8080->80/tcp
- compose_App2_1 nginx -g daemon off; Up 443/tcp, 0.0.0.0:8081->80/tcp
- compose_App3_1 nginx -g daemon off; Up 443/tcp, 0.0.0.0:8082->80/tcp
其他命令:
- [root@docker compose]# docker-compose restart #重启所有容器
- [root@docker compose]# docker-compose restart App1 #重启App1
- [root@docker compose]# docker-compose stop #停止所有容器
- [root@docker compose]# docker-compose stop App1 #停止App1
6. 验证网络隔离:
a.到App1 ping App2
- [root@docker compose]# docker-compose exec App1 bash
- root@dd01fa7315ae:/# ping App2
- PING App2 (172.18.0.3): 56 data bytes
- 64 bytes from 172.18.0.3: icmp_seq=0 ttl=64 time=0.059 ms
- 64 bytes from 172.18.0.3: icmp_seq=1 ttl=64 time=0.108 ms
- 64 bytes from 172.18.0.3: icmp_seq=2 ttl=64 time=0.062 ms
- 64 bytes from 172.18.0.3: icmp_seq=3 ttl=64 time=0.062 ms
成功ping通。
b.到App2 ping App1和App3
- [root@docker compose]# docker-compose exec App2 bash
- root@1905b2a875e3:/# ping App1
- PING App1 (172.18.0.2): 56 data bytes
- 64 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.043 ms
- 64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.089 ms
- 64 bytes from 172.18.0.2: icmp_seq=2 ttl=64 time=0.085 ms
App2 to App3-成功。
- [root@docker compose]# docker-compose exec App3 bash
- root@595f744e8634:/# ping App1
- PING App1 (172.31.213.13): 56 data bytes
- ....#App3 to App1失败
- root@595f744e8634:/# ping App2
- PING App2 (172.31.213.13): 56 data bytes
- ....#App3 to App2失败
7. 验证共享卷
a. 在host /opt/conf/ 目录下新建nginx.conf 文件
- [root@docker conf]# pwd
- /opt/conf
- [root@docker conf]# touch nginx.conf
b. 登录到App1 和App2查看
- [root@docker compose]# docker-compose exec App1 bash
- root@49d702fc8606:/# cd /mmt
- root@49d702fc8606:/mnt# ls
- nginx.conf#成功挂载
- [root@docker compose]# docker-compose exec App2 bash
- root@1d161428d4dd:/# cd /mnt
- root@1d161428d4dd:/mnt# ls
- nginx.conf#成功挂载
c. 在App3中挂载文件到nginx
修改docker-compose.yml App3:
- App3:
- image: nginx
- volumes:
- - ./app/web/:/usr/share/nginx/html
- ports:
- - "8082:80"
- networks:
- - "netName2"
在host ./app/web 中添加 app3.html
- [root@docker web]# app3.html
- [root@docker web]# ls
- app3.html
- [root@docker web]# more app3.html
Welcome to leonyan's home, this is app3 - Welcome to leonyan's home, this is app3!
- [root@docker compose]# cd app/web/
访问App3 nginx:
【本文为专栏作者“王森丰”的原创稿件,转载请注明出处】
戳这里,看该作者更多好文
新闻名称:如何使用docker compose部署服务
当前路径:http://www.shufengxianlan.com/qtweb/news46/83846.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联