由此能夠看出, 持續集成有一個關鍵步驟就是 RUN BVT automation, 而這一步驟的實現須要有一個完整的,乾淨的bvt 環境來支撐。 沒有一個好的bvt環境, run bvt automation 就沒有意義。 如何生成一個完整,乾淨的bvt 環境呢,這裏咱們能夠引入Docker compose. Docker compose 是 Docker 容器進行編排的工具,定義和運行多容器的應用,能夠一條命令啓動多個容器。使用docker compose咱們能夠在Run的層面解決不少實際問題,如:經過建立compose(基於YUML語法)文件,在這個文件上面描述應用的架構,如使用什麼鏡像、數據卷、網絡、綁定服務端口等等,而後再用一條命令就能夠管理全部的服務(如啓動、中止、重啓、日誌監控等等)。html
使用Compose 基本上分爲三步:nginx
[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.服務物理框架
git
說明:
a.Container1和2共在一個network namespace(netName1)內,Container3獨立network namespace(netName2)
b.Container1掛載了一個系統卷(/opt/conf)到/mnt
c.Container3掛載了一個系統卷(./app/web)到/usr/share/nginx/html
c.Container1,2,3 nginx監聽端口分別爲:8080,8081,8082github
3.docker-compose.yml 內容web
[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.運行docker
[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.查看容器運行狀態bash
[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和App3app
[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 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Welcome to leonyan's home, this is app3</title> </head> <body> Welcome to leonyan's home, this is app3! </body> </html> [root@docker compose]# cd app/web/
訪問App3 nginx: