1. 程式人生 > >Docker(十二):Docker集群管理之Compose

Docker(十二):Docker集群管理之Compose

png eas version pull 是否 com a star reat volume

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   #動態掛載路徑

  

Docker(十二):Docker集群管理之Compose