zoukankan      html  css  js  c++  java
  • (四)Docker三剑客之Compose

    Compose 定位是 「 定义和运行多个 Docker 容器的应用( Defining and running multicontainer Docker applications) 」 ,其前身是开源项目 Fig。Github上维护。

    参考:

    1. 下载安装

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

    2. 启动

    mkdir compose
    cd compose/
    vim docker-compose.yml
    
    docker-compose up
    

    docker-compose.yml

    version: '3'
    services:
        redis:
            image: "redis:latest"
            ports:
            - "6379:6379"
    

    3. docker-compose help

    [root@Thor compose]# docker-compose help
    Define and run multi-container applications with Docker.
    
    Usage:
      docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
      docker-compose -h|--help
    
    Options:
      -f, --file FILE             Specify an alternate compose file (default: docker-compose.yml)
      -p, --project-name NAME     Specify an alternate project name (default: directory name)
      --verbose                   Show more output
      --no-ansi                   Do not print ANSI control characters
      -v, --version               Print version and exit
      -H, --host HOST             Daemon socket to connect to
    
      --tls                       Use TLS; implied by --tlsverify
      --tlscacert CA_PATH         Trust certs signed only by this CA
      --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
      --tlskey TLS_KEY_PATH       Path to TLS key file
      --tlsverify                 Use TLS and verify the remote
      --skip-hostname-check       Don't check the daemon's hostname against the name specified
                                  in the client certificate (for example if your docker host
                                  is an IP address)
      --project-directory PATH    Specify an alternate working directory
                                  (default: the path of the Compose file)
    
    Commands:
      build              Build or rebuild services
      bundle             Generate a Docker bundle from the Compose file
      config             Validate and view the Compose file
      create             Create services
      down               Stop and remove containers, networks, images, and volumes
      events             Receive real time events from containers
      exec               Execute a command in a running container
      help               Get help on a command
      images             List images
      kill               Kill containers
      logs               View output from containers
      pause              Pause services
      port               Print the public port for a port binding
      ps                 List containers
      pull               Pull service images
      push               Push service images
      restart            Restart services
      rm                 Remove stopped containers
      run                Run a one-off command
      scale              Set number of containers for a service
      start              Start services
      stop               Stop services
      top                Display the running processes
      unpause            Unpause services
      up                 Create and start containers
      version            Show the Docker-Compose version information
    [root@Thor compose]# 
    

    4. 启动项目中的服务(容器)

    [root@Thor compose]# docker-compose start redis
    Starting redis ... done
    
    [root@Thor compose]# docker-compose ps
         Name                    Command               State    Ports  
    -------------------------------------------------------------------
    compose_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp
    [root@Thor compose]#
    

    5. docker-compose exec

    [root@Thor compose]# docker-compose exec redis bash
    root@883179463cd2:/data# redis-cli
    127.0.0.1:6379> keys *
    1) "name"
    127.0.0.1:6379> 
    127.0.0.1:6379> 
    

    6. Compose模板文件

    模板文件是使用 Compose 的核心,涉及到的指令关键字也比较多。大部分指令和 docker run 相关参数的含义类似。

    默认的模板文件名为 docker-compose.yml ,格式为 YAML 格式。

    6.1 使用示例

    [root@Thor compose-redis]# vim docker-compose.yml 
    [root@Thor compose-redis]# cat docker-compose.yml 
    version: '3'
    services:
            redis-server:
                    build: .
                    ports:
                    - "6379:6379"
    
            httpd:
                    image: "httpd:latest"
                    ports:
                    - "7777:80"
    [root@Thor compose-redis]# 
    
    [root@Thor compose-redis]# docker-compose up
    Creating network "composeredis_default" with the default driver
    Building redis-server
    Step 1/5 : FROM ubuntu:latest
     ---> 113a43faa138
    Step 2/5 : RUN apt-get update     && apt-get install -y redis     && mkdir -p /home/ld
     ---> Using cache
     ---> f16df9849ef0
    Step 3/5 : COPY ./testfile.txt /home/ld
     ---> 43470c11aef2
    Removing intermediate container c0e35ebbb193
    Step 4/5 : VOLUME /data
     ---> Running in e8ff92be38af
     ---> 00f6628e94f7
    Removing intermediate container e8ff92be38af
    Step 5/5 : CMD redis-server
     ---> Running in bf4d5db3edb6
     ---> 1917e53f658f
    Removing intermediate container bf4d5db3edb6
    Successfully built 1917e53f658f
    WARNING: Image for service redis-server was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
    Creating composeredis_redis-server_1 ... 
    Creating composeredis_httpd_1 ... 
    
    Creating composeredis_httpd_1 ... done
    Attaching to composeredis_redis-server_1, composeredis_httpd_1
    redis-server_1  | 1:C 06 Aug 13:57:29.464 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    httpd_1         | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.19.0.3. Set the 'ServerName' directive globally to suppress this message
    redis-server_1  | 1:C 06 Aug 13:57:29.464 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
    httpd_1         | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.19.0.3. Set the 'ServerName' directive globally to suppress this message
    redis-server_1  | 1:C 06 Aug 13:57:29.464 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
    httpd_1         | [Mon Aug 06 13:57:29.497130 2018] [mpm_event:notice] [pid 1:tid 140095686104960] AH00489: Apache/2.4.33 (Unix) configured -- resuming normal operations
    httpd_1         | [Mon Aug 06 13:57:29.497235 2018] [core:notice] [pid 1:tid 140095686104960] AH00094: Command line: 'httpd -D FOREGROUND'
    redis-server_1  | 1:M 06 Aug 13:57:29.466 * Running mode=standalone, port=6379.
    redis-server_1  | 1:M 06 Aug 13:57:29.467 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    redis-server_1  | 1:M 06 Aug 13:57:29.467 # Server initialized
    redis-server_1  | 1:M 06 Aug 13:57:29.467 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
    redis-server_1  | 1:M 06 Aug 13:57:29.467 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    redis-server_1  | 1:M 06 Aug 13:57:29.467 * Ready to accept connections
    
    [root@Thor compose-redis]# docker-compose ps
               Name                   Command        State           Ports         
    -------------------------------------------------------------------------------
    composeredis_httpd_1          httpd-foreground   Up      0.0.0.0:7777->80/tcp  
    composeredis_redis-server_1   redis-server       Up      0.0.0.0:6379->6379/tcp
    [root@Thor compose-redis]# 
    
    
    [root@Thor compose-redis]# docker image ls
    REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
    composeredis_redis-server   latest              1917e53f658f        4 minutes ago       125 MB
    redis                       1.0.0               614b878bc14e        13 days ago         125 MB
    registry                    latest              b2b03e9146e1        4 weeks ago         33.3 MB
    golang                      latest              4e611157870f        5 weeks ago         794 MB
    redis                       latest              71a81cb279e3        5 weeks ago         83.4 MB
    httpd                       latest              2a7d646dbba8        5 weeks ago         177 MB
    ubuntu                      latest              113a43faa138        2 months ago        81.1 MB
    google/cadvisor             latest              75f88e3ec333        8 months ago        62.2 MB
    [root@Thor compose-redis]# 
    

    6.2 常用Compose 命令说明

    6.2.1 build 命令

    指定 Dockerfile 所在文件夹的路径( 可以是绝对路径,或者相对 docker-compose.yml 文件的路径)。Compose 将会利用它自动构建这个镜像,然后使用这个镜像。

    6.2.2 command 命令

    覆盖容器启动后默认执行的命令。

    command: redis-server
    

    6.2.3 container_name 命令

    指定容器名称。默认将会使用 项目名称_服务名称_序号 这样的格式。

    container_name: docker-redis-container
    

    6.2.4 depends_on 命令

    解决容器的依赖、启动先后的问题。

    version: '3'
    	services:
    		web:
    			build: .
    			depends_on:
    			- db
    			- redis
    		redis:
    			image: redis
    		db:
    			image: postgres
    

    6.2.5 image 命令

    指定为镜像名称或镜像 ID。如果镜像在本地不存在,Compose 将会尝试拉取这个镜像。

    6.2.6 ports 命令

    暴露端口信息,格式为:

    - 宿主端口:容器端口 (HOST:CONTAINER)
    - 容器端口 
    

    如果仅仅指定容器的端口( 宿主将会随机选择端口)

    6.2.7 volumes 命令

    数据卷所挂载路径设置。可以设置宿主机路径 (HOST:CONTAINER ) 或加上访问模式(HOST:CONTAINER:ro ),该指令中路径支持相对路径。

    volumes:
    	- /var/lib/mysql
    	- cache/:/tmp/cache
    	- ~/configs:/etc/configs/:ro
    
  • 相关阅读:
    LOJ6435 「PKUSC2018」星际穿越
    LOJ6433 「PKUSC2018」最大前缀和
    LOJ2541 「PKUWC2018」猎人杀
    LOJ2545 「JXOI2018」守卫
    LOJ2290 「THUWC 2017」随机二分图
    CF1007B Pave the Parallelepiped
    【学习笔记】卡特兰数
    Linux系统命令“su
    免密
    Window操作系统下的SSL证书管理
  • 原文地址:https://www.cnblogs.com/walkinginthesun/p/9419072.html
Copyright © 2011-2022 走看看