docker compose
使用Docker Compose管理多個容器
Docker Compose是一個用來定義和運行復雜應用的Docker工具。使用Compose,你可以在一個文件中定義一個多容器應用,然後使用一條命令來啟動你的應用,完成一切準備工作。
- github.com/docker/compose
一個使用Docker容器的應用,通常由多個容器組成。使用Docker Compose,不再需要使用shell腳本來啟動容器。在配置文件中,所有的容器通過services
來定義,然後使用docker-compose
腳本來啟動,停止和重啟應用,和應用中的服務以及所有依賴服務的容器。完整的命令列表如下:
build
構建或重建服務
help
命令幫助
kill
殺掉容器
logs
顯示容器的輸出內容
port
打印綁定的開放端口
ps
顯示容器
pull
拉取服務鏡像
restart
重啟服務
rm
刪除停止的容器
run
運行一個一次性命令
scale
設置服務的容器數目
start
開啟服務
stop
停止服務
up
創建並啟動容器
參考 https://docs.docker.com/compose/install/ 。你能運行Compose在OSX和64位Linux。當前不支持Windows操作系統。
8.1. 安裝Docker Compose
curl -L https://github.com/docker/compose/releases/download/1.4.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose chmod x /usr/local/bin/docker-compose
8.2. 配置文件
1.Compose的配置文件是docker-compose.yml
。讓我們看看下面這個文件:
mysqldb: image: [classroom.example.com:5000/]mysql environment: MYSQL_DATABASE: sample MYSQL_USER: mysql MYSQL_PASSWORD: mysql MYSQL_ROOT_PASSWORD: supersecret mywildfly: image: [classroom.example.com:5000|arungupta]/wildfly-mysql-javaee7 links: - mysqldb:db ports: - 8080
這個文件在 https://raw.githubusercontent. ... dees/ ,它表明:
a. 定義了兩個服務分別叫做mysqldb
和mywildfy
b. 使用image
定義每個服務的鏡像名
c. MySQL容器的環境變量定義在environment
d. MySQL容器使用links
和WildFly容器鏈接
e. 使用ports
實現端口轉發
8.3. 啟動服務
1.如果你從互聯網運行,將docker-compose-internet.yml
保存為docker-compose.yml
。
2.如果你使用教師給的鏡像,將docker-compose-instructor.yml
保存為docker-compose.yml
。
3.使用下面的命令,所有的服務將使用後臺模式被啟動
docker-compose up -d
顯示的輸出如下:
Creating attendees_mysqldb_1...
Creating attendees_mywildfly_1...
使用-f
指定代替的compose文件。
使用-p
指定代替compose文件所在的目錄。
4.驗證啟動的服務
docker-compose ps Name Command State Ports attendees_mysqldb_1 /entrypoint.sh mysqld Up 3306/tcp attendees_mywildfly_1 /opt/jboss/wildfly/customi ... Up 0.0.0.0:32773->8080/tcp
這裏提供了一個整合的列表顯示所有啟動的服務和容器。
同時,通常使用docker ps
命令來驗證應用的容器,和在Docker主機上運行的其他容器。
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3598e545bd2f arungupta/wildfly-mysql-javaee7:latest "/opt/jboss/wildfly/ 59 seconds ago Up 58 seconds 0.0.0.0:32773->8080/tcp attendees_mywildfly_1
b8cf6a3d518b mysql:latest "/entrypoint.sh mysq 2 minutes ago Up 2 minutes 3306/tcp attendees_mysqldb_1
5.查詢服務日誌
taching to attendees_mywildfly_1, attendees_mysqldb_1 mywildfly_1 | => Starting WildFly server mywildfly_1 | => Waiting for the server to boot mywildfly_1 | =========================================================== mywildfly_1 | mywildfly_1 | JBoss Bootstrap Environment mywildfly_1 | mywildfly_1 | JBOSS_HOME: /opt/jboss/wildfly mywildfly_1 | mywildfly_1 | JAVA: /usr/lib/jvm/java/bin/java mywildfly_1 | mywildfly_1 | JAVA_OPTS: -server -Xms64m -Xmx512m -XX:MaxPermSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true mywildfly_1 | . . . mywildfly_1 | 15:40:20,866 INFO [org.jboss.resteasy.spi.ResteasyDeployment] (MSC service thread 1-2) Deploying javax.ws.rs.core.Application: class org.javaee7.samples.employees.MyApplication mywildfly_1 | 15:40:20,914 INFO [org.wildfly.extension.undertow] (MSC service thread 1-2) JBAS017534: Registered web context: /employees mywildfly_1 | 15:40:21,032 INFO [org.jboss.as.server] (ServerService Thread Pool -- 28) JBAS018559: Deployed "employees.war" (runtime-name : "employees.war") mywildfly_1 | 15:40:21,077 INFO [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management mywildfly_1 | 15:40:21,077 INFO [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990 mywildfly_1 | 15:40:21,077 INFO [org.jboss.as] (Controller Boot Thread) JBAS015874: WildFly 8.2.0.Final "Tweek" started in 9572ms - Started 280 of 334 services (92 services are lazy, passive or on-demand) mysqldb_1 | Running mysql_install_db mysqldb_1 | 2015-06-05 15:38:31 0 [Note] /usr/sbin/mysqld (mysqld 5.6.25) starting as process 27 ... mysqldb_1 | 2015-06-05 15:38:31 27 [Note] InnoDB: Using atomics to ref count buffer pool pages . . . mysqldb_1 | 2015-06-05 15:38:40 1 [Note] Event Scheduler: Loaded 0 events mysqldb_1 | 2015-06-05 15:38:40 1 [Note] mysqld: ready for connections. mysqldb_1 | Version: ‘5.6.25‘ socket: ‘/var/run/mysqld/mysqld.sock‘ port: 3306 MySQL Community Server (GPL) mysqldb_1 | 2015-06-05 15:40:18 1 [Warning] IP address ‘172.17.0.24‘ could not be resolved: Name or service not known
8.4. 驗證應用
1.通過 http://dockerhost:32773/employ ... yees/ 訪問應用。在瀏覽器裏顯示如下:
使用docker-compose ps
命令顯示端口
8.5. 擴展服務
你能像這樣擴展服務:
docker-compose scale mywildfly=4 Creating and starting 2... done Creating and starting 3... done Creating and starting 4... done
檢查日誌:
docker-compose logs
檢查運行的實例:
docker-compose ps
Name Command State Ports
rafael_mysqldb_1 /entrypoint.sh mysqld Up 3306/tcp
rafael_mywildfly_1 /opt/jboss/wildfly/customi ... Up 0.0.0.0:32773->8080/tcp
rafael_mywildfly_2 /opt/jboss/wildfly/customi ... Up 0.0.0.0:32777->8080/tcp
rafael_mywildfly_3 /opt/jboss/wildfly/customi ... Up 0.0.0.0:32780->8080/tcp
rafael_mywildfly_4 /opt/jboss/wildfly/customi ... Up 0.0.0.0:32781->8080/tcp
你也能減少運行的實例數目:
docker-compose scale mywildfly=2 Stopping rafael_mywildfly_3... done Stopping rafael_mywildfly_4... done Removing rafael_mywildfly_3... done Removing rafael_mywildfly_4... done
8.6. 停止服務
停止服務:
docker-compose stop Stopping attendees_mywildfly_1... Stopping attendees_mywildfly_2... Stopping attendees_mysqldb_1...
警告:再次停止和啟動將會得到如下錯誤:
wildfly_1 | wildfly_1 | 09:11:07,802 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 4) JBAS014613: Operation ("add") failed - address: ([ wildfly_1 | ("subsystem" => "datasources"), wildfly_1 | ("jdbc-driver" => "mysql") wildfly_1 | ]) - failure description: "JBAS014803: Duplicate resource [ wildfly_1 | (\"subsystem\" => \"datasources\"), wildfly_1 | (\"jdbc-driver\" => \"mysql\") wildfly_1 | ]"
這是因為JDBC資源在每個運行的容器中創建,在實際情況下,這是已經寫在配置中的。
docker compose