Docker Series 13: container arrangement (1)

First, the basic layout container

1, the container layout concepts

  • The program can be specified in the individual containers are dependencies
  • For example, there is a web service must, in order to run mysql under normal circumstances, then this is the web container when the container is dependent on mysql, mysql container only normal start, to start the web container.

    2, the container layout tools

  • docker compose
  • k8s
  • k3s

    Two, compose Detailed

1. What is compose

  • Docker Compose will manage the container is divided into three, namely project (project), service (service), container (container)
  • All files (docker-compose.yml) under Docker Compose form a run directory project, a project comprises a plurality of services, each defined image container runs, parameters, dependent, a service may comprise a plurality of containers instances

2, installation compse

Method 1: Use this document conpose provided, this file into the / usr / local / bin
Docker Series 13: container arrangement (1)
Docker Series 13: container arrangement (1)
Method 2: epel and extra source installation package
1) Configuration source yum

[root@host1 ~]# cat /etc/yum.repos.d/epel.repo 
[epel]
name=epel
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7Server/x86_64/
enabled=1
gpgcheck=0

2) Installation docker-compose

[root@host1 ~]# yum install docker-compose -y

3, compose arguments detailed

Look at a sample file dyml
Docker Series 13: container arrangement (1)

Parameter 1: build

  • build, it can specify the path to the folder where Dockerfile. Compose will automatically use it to build the mirror, and then use this image to start the service container.
  • It may be a relative path, as long as it can read the context determination Dockerfile.
     

    Parameter 2: context

  • This is used to specify the working directory of the file dockerfile
  • This is the build of a sub-option

Parameter 3: dockerfile

  • Do specify an image dockerfile
  • This is the build of a sub-option

Parameter 4: ARGS

  • Set variables that will be used only during the build of
  • This is the build of a sub-option

Parameter 5: labels

  • This is the build of a sub-option
  • Metadata is used to specify build process

Parameter 6: shm_size

  • It is to build a sub-option, the control parameter build constructed based on the configuration of the mirror
  • /dev/shm是linux中系统中物理内存的映射,使用/dev/shm对文件操作的效率会高很多,我在优化系统的时候,常用用到

参数7:command

  • 使用 command 可以覆盖容器启动后默认执行的命令。
     

    参数8:image

  • image 则是指定服务的镜像名称或镜像 ID。
  • 如果镜像在本地不存在,Compose 将会尝试拉取这个镜像。
    image: redis
    image: ubuntu:14.04
    image: tutum/influxdb
    image: example-registry.com:4000/postgresql
    image: a4bc65fd

参数9:container_name

  • 可以使用这个标签指定容器名称

参数10:depends_on

  • 这个标签解决了容器的依赖、启动先后的问题

案例:下面容器会先启动 redis 和 db 两个服务,最后才启动 web 服务:

Docker Series 13: container arrangement (1)

参数11:dns

  • 指定DNS服务器的地址
    dns: 8.8.8.8
    dns:
     - 8.8.8.8
     - 9.9.9.9

参数12:dns_search

  • 设置搜索域
    dns_search: example.com
    dns_search:
     - dc1.example.com
     - dc2.example.com

参数13:tmpfs

  • 挂载临时目录到容器内部
    tmpfs:
     - /run
     - /tmp

参数14:entrypoint

  • 用于覆盖 Dockerfile 中的定义
    entrypoint:
       - php
       - -d
       - zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so
       - -d
       - memory_limit=-1
       - vendor/bin/phpunit

       

    参数15:env_file

  • docker-compose.yml 中可以定义一个专门存放变量的文件。
  • 如果通过 docker-compose -f FILE 指定了配置文件,则 env_file 中路径会使用配置文件路径。
  • 如果有变量名称与 environment 指令冲突,则以后者为准。
    env_file:
     - ./common.env
     - ./apps/web.env
     - /opt/secrets.env

参数16:environment

  • 这个标签的作用是设置镜像变量,它可以保存变量到镜像里面,也就是说启动的容器也会包含这些变量设置,这是与 arg 最大的不同。
  • environment 和 Dockerfile 中的 ENV 指令一样会把变量一直保存在镜像、容器中,类似 docker run -e 的效果。
    
    environment:
    RACK_ENV: development
    SHOW: 'true'
    SESSION_SECRET:

environment:
 - RACK_ENV=development
 - SHOW=true
 - SESSION_SECRET


#### 参数17:expose
* 这个标签与Dockerfile中的EXPOSE指令一样,用于指定暴露的端口,但是只是作为一种参考
* 实际上docker-compose.yml的端口映射还得ports这样的标签。

expose:

  • "3000"
  • "8000"

参数18:external_links

  • 为了使Compose能够连接这些不在docker-compose.yml中定义的容器,external_links可以让Compose项目里面的容器连接到那些项目配置外部的容器
     external_links:
    - redis_1
    - project_db_1:mysql
    - project_db_1:postgresql

参数19:extra_hosts

  • 添加主机名的标签,就是往/etc/hosts文件中添加一些记录
    extra_hosts:
    - "somehost:162.242.195.82"
    - "otherhost:50.31.209.229"

标签20:labels

  • 向容器添加元数据,和Dockerfile的LABEL指令一个意思
    labels:
    com.example.description: "Accounting webapp"
    com.example.department: "Finance"
    com.example.label-with-empty-value: ""
    labels:
     - "com.example.description=Accounting webapp"
     - "com.example.department=Finance"
     - "com.example.label-with-empty-value"

标签21:links

  • depends_on吧,那个标签解决的是启动顺序问题
  • 这个标签解决的是容器连接问题,与Docker client的--link一样效果,会连接到其它服务中的容器。
    links:
    - db
    - db:database
    - redis

标签22:logging

  • 这个标签用于配置日志服务。
    logging:
    driver: syslog
    options:
    syslog-address: "tcp://192.168.0.42:123"

标签23:pid

  • 容器使用这个标签将能够访问和操纵其他容器和宿主机的名称空间。
  • pid: "host":将PID模式设置为主机PID模式,跟主机系统共享进程命名空间。

标签24:ports

  • 映射端口的标签。
  • 使用HOST:CONTAINER格式或者只是指定容器的端口,宿主机会随机映射端口。
    ports:
    - "3000"
    - "8000:8000"
    - "49100:22"
    - "127.0.0.1:8001:8001"
  • 当使用HOST:CONTAINER格式来映射端口时,如果你使用的容器端口小于60你可能会得到错误得结果
  • 因为YAML将会解析xx:yy这种数字格式为60进制。所以建议采用字符串格式。

标签25:security_opt

  • 为每个容器覆盖默认的标签。简单说来就是管理全部服务的标签。比如设置全部服务的user标签值为USER。
    security_opt:
     - label:user:USER
     - label:role:ROLE

标签26:stop_signal

  • 设置另一个信号来停止容器。
  • 在默认情况下使用的是SIGTERM停止容器。

标签27:volumes

  • 挂载一个目录或者一个已存在的数据卷容器
  • 可以直接使用 [HOST:CONTAINER] 这样的格式,或者使用 [HOST:CONTAINER:ro] 这样的格式,后者的数据卷是只读的
  • Compose的数据卷指定路径可以是相对路径,使用 . 或者 .. 来指定相对目录。

标签28:volumes_from

  • 从其它容器或者服务挂载数据卷
  • The optional parameters are: ro or: rw, read only the former represents a container, which represents the data volume of the container is readable and writable
  • The default is readable and writable.
    volumes_from:
     - service_name
     - service_name:ro
     - container:container_name
     - container:container_name:rw

    Label 29: cap_add, cap_drop

  • Add or remove container kernel function

Label 30: cgroup_parent

  • Specify a container parent cgroup.

Label 31: devices

  • Device mapping list. Similar to the Docker client's --device parameters.

Label 32: extends

  • This tag can be extended another service, content can be extended from the current file, it can also come from other files
  • The case of the same service, and later there will be selectively overwrite the previous one.
    extends:
    file: common.yml
     service: webapp

    Label 33: network_mode

  • Set network mode
  • --Net Docker client with similar parameters, except that a relatively more service: [service name] format
    network_mode: "bridge"
    network_mode: "host"
    network_mode: "none"
    network_mode: "service:[service name]"
    network_mode: "container:[container name/id]"

    Label 34: networks

    services:
    some-service:
       networks:
        - some-network
        - other-network

    Port exposure problems

  • ports: Port exposure information. Use: Host: a container (HOST: CONTAINER) format or just a designated port of the container (selected randomly host ports) may be used.
  • expose: expose the port, but does not map to the host, service access is only connected.

Questions about the boot sequence control

  • depends_on: express service dependencies before, there are two effects:
  • 1) docker-compose up, before the start web service, start redis, db.
  • 2) docker-compose up web, when starting the web container, check dependencies of depends_on configuration content, start and redis db
    version: '2'
    services: 
    web:
      build: .
       depends_on:
          - db
         - redis
    redis:   
        image: redis
    db:   
       image: postgres

Guess you like

Origin blog.51cto.com/54dev/2462082