Home Archives Categories Tags Docs

使用Docker Compose管理多个容器

发布时间: 更新时间: 总字数:1430 阅读时间:3m 作者: 分享

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 创建并启动容器

安装Docker Compose

curl -L https://github.com/docker/compose/releases/download/1.11.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

配置文件

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

这个上面的文件 ,它表明:

  • a. 定义了两个服务分别叫做mysqldb和mywildfy
  • b. 使用image定义每个服务的镜像名
  • c. MySQL容器的环境变量定义在environment
  • d. MySQL容器使用links和WildFly容器链接
  • e. 使用ports实现端口转发

启动服务

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

验证应用

1.通过 http://dockerhost:32773/employees/resources/employees/ 访问应用。在浏览器里显示如下:

使用docker-compose ps命令显示端口

扩展服务

你能像这样扩展服务:

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

停止服务

停止服务:

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资源在每个运行的容器中创建,在实际情况下,这是已经写在配置中的。

相关文章
最近更新
最新评论
加载中...