1、Compose安装
curl -L https://github.com/docker/compose/releases/download/1.1.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
2、简单使用
创建compose工作目录
# mkdir composework
在compose工作目录内创建docker-compose.yml文件
# cat docker-compose.yml
tomcat1:
image: myweb:v2
ports:
- "9099:8080"
- "9093:8443"
volumes:
- ./websrc:/usr/local/tomcat/webapps/myproj
# tty: true
command: catalina.sh run
在.yml文件同级目录下创建websrc目录,存放tomcat源码文件
启动compose
# docker-compose up Creating composework_tomcat1_1... Attaching to composework_tomcat1_1 tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: Server version: Apache Tomcat/7.0.82 tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: Server built: Sep 29 2017 12:23:15 UTC tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: Server number: 7.0.82.0 tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: OS Name: Linux tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: OS Version: 3.16.0-30-generic tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: Architecture: amd64 tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: Java Home: /usr/lib/jvm/java-8-openjdk-amd64/jre tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: JVM Version: 1.8.0_141-8u141-b15-1~deb9u1-b15 tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: JVM Vendor: Oracle Corporation tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: CATALINA_BASE: /usr/local/tomcat tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: CATALINA_HOME: /usr/local/tomcat tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: Command line argument: -Djdk.tls.ephemeralDHKeySize=2048 tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: Command line argument: -Djava.endorsed.dirs=/usr/local/tomcat/endorsed tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: Command line argument: -Dcatalina.base=/usr/local/tomcat tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: Command line argument: -Dcatalina.home=/usr/local/tomcat tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.VersionLoggerListener log tomcat1_1 | INFO: Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.coyote.AbstractProtocol init tomcat1_1 | INFO: Initializing ProtocolHandler ["http-bio-8080"] tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.coyote.AbstractProtocol init tomcat1_1 | INFO: Initializing ProtocolHandler ["http-bio-8443"] tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.coyote.AbstractProtocol init tomcat1_1 | INFO: Initializing ProtocolHandler ["ajp-bio-8009"] tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.Catalina load tomcat1_1 | INFO: Initialization processed in 588 ms tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.core.StandardService startInternal tomcat1_1 | INFO: Starting service Catalina tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.core.StandardEngine startInternal tomcat1_1 | INFO: Starting Servlet Engine: Apache Tomcat/7.0.82 tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.HostConfig deployDirectory tomcat1_1 | INFO: Deploying web application directory /usr/local/tomcat/webapps/myproj tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.HostConfig deployDirectory tomcat1_1 | INFO: Deployment of web application directory /usr/local/tomcat/webapps/myproj has finished in 250 ms tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.HostConfig deployDirectory tomcat1_1 | INFO: Deploying web application directory /usr/local/tomcat/webapps/host-manager tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.HostConfig deployDirectory tomcat1_1 | INFO: Deployment of web application directory /usr/local/tomcat/webapps/host-manager has finished in 110 ms tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.HostConfig deployDirectory tomcat1_1 | INFO: Deploying web application directory /usr/local/tomcat/webapps/manager tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.HostConfig deployDirectory tomcat1_1 | INFO: Deployment of web application directory /usr/local/tomcat/webapps/manager has finished in 47 ms tomcat1_1 | Oct 16, 2017 6:59:20 AM org.apache.catalina.startup.HostConfig deployDirectory tomcat1_1 | INFO: Deploying web application directory /usr/local/tomcat/webapps/docs tomcat1_1 | Oct 16, 2017 6:59:21 AM org.apache.catalina.startup.HostConfig deployDirectory tomcat1_1 | INFO: Deployment of web application directory /usr/local/tomcat/webapps/docs has finished in 36 ms tomcat1_1 | Oct 16, 2017 6:59:21 AM org.apache.catalina.startup.HostConfig deployDirectory tomcat1_1 | INFO: Deploying web application directory /usr/local/tomcat/webapps/ROOT tomcat1_1 | Oct 16, 2017 6:59:21 AM org.apache.catalina.startup.HostConfig deployDirectory tomcat1_1 | INFO: Deployment of web application directory /usr/local/tomcat/webapps/ROOT has finished in 30 ms tomcat1_1 | Oct 16, 2017 6:59:21 AM org.apache.catalina.startup.HostConfig deployDirectory tomcat1_1 | INFO: Deploying web application directory /usr/local/tomcat/webapps/examples tomcat1_1 | Oct 16, 2017 6:59:21 AM org.apache.catalina.startup.HostConfig deployDirectory tomcat1_1 | INFO: Deployment of web application directory /usr/local/tomcat/webapps/examples has finished in 176 ms tomcat1_1 | Oct 16, 2017 6:59:21 AM org.apache.coyote.AbstractProtocol start tomcat1_1 | INFO: Starting ProtocolHandler ["http-bio-8080"] tomcat1_1 | Oct 16, 2017 6:59:21 AM org.apache.coyote.AbstractProtocol start tomcat1_1 | INFO: Starting ProtocolHandler ["http-bio-8443"] tomcat1_1 | Oct 16, 2017 6:59:21 AM org.apache.coyote.AbstractProtocol start tomcat1_1 | INFO: Starting ProtocolHandler ["ajp-bio-8009"] tomcat1_1 | Oct 16, 2017 6:59:21 AM org.apache.catalina.startup.Catalina start tomcat1_1 | INFO: Server startup in 727 ms
查看进程
# docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------------------
composework_tomcat1_1 catalina.sh run Up 0.0.0.0:9099->8080/tcp, 0.0.0.0:9093->8443/tcp
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
598bfcd8760a myweb:v2 "catalina.sh run" 3 minutes ago Up 3 minutes 0.0.0.0:9099->8080/tcp, 0.0.0.0:9093->8443/tcp composework_tomcat1_1
访问测试
修改宿主机websrv查看是否同步
# cat index.jsp
Test Page
3、Compose命令详解
Compose命令详解: build #创建或者再建服务 help #显示命令的帮助和使用信息 kill #通过发送SIGKILL的信号强制停止运行的容器,这个信号可以选择性的通过, #比如: docker-compose kill -s SIGKINT logs #显示服务的日志输出 port #为端口绑定输出公共信息 ps #显示容器 pull #拉取服务镜像 rm #删除停止的容器 run #在服务上运行一个一次性命令 start #启动已经存在的容器作为一个服务 stop #停止运行的容器而不删除它们 up #启动 docker-compose.yml命令说明: image #启动容器使用的镜像 build #创建镜像,需要指定Dockerfile的目录 command #重写默认的命令 links #连接到其他服务中的容器 ports #端口映射 volumes #动态挂载路径