docker-compose指令详解上(二)

发布时间:2020-05-22 08:42:44 作者:IdealReality
来源:网络 阅读:416

compose文件格式

定义配置文件中的部分(如构建,部署,依赖,网络等)的顶级键与支持它们作为子主题的选项一起列出。这映射到Compose文件的<key>:<option>:<value>缩进结构

服务配置参考

Compose文件是定义services,networks和volumes的YAML文件。 Compose文件的默认路径是./docker-compose.yml。

服务定义包含应用于为该服务启动的每个容器的配置. 类似:

docker container create
docker network create 
docker volume create
build
version: "3.7"    #版本
services:         #服务
  nginx:          #服务名称
    build: ./dir  #指定构建目录
    image: nginx:tag  #指定基础镜像
context
build:
  context: ./dir
dockerfile
build:
  context: .
  dockerfile: Dockerfile-alternate
ARGS
ARG buildno
ARG gitcommithash
build:
  context: .
  args:
    buildno: 1

注意: 在Dockerfile中,如果在FROM指令之前指定ARG,则在FROM下的构建指令中不能使用ARG。

YAML布尔值(true,false,yes,no,on,off)必须用引号括起来,以便解析器将它们解释为字符串。

CACHE_FROM
build:
  context: .
  cache_from:
    - alpine:latest
    - corp/web_app:3.14
LABELS
build:
  context: .
  labels:
    com.example.description: "Accounting webapp"
SHM_SIZE
build:
  context: .
  shm_size: '2gb'
TARGET
build:
  context: .
  target: prod
cap_add和cap_drop
cap_add:
  - ALL

cap_drop:
  - NET_ADMIN
  - SYS_ADMIN
cgroup_parent
cgroup_parent: m-executor-abcd
command
command: ["bundle", "exec", "thin", "-p", "3000"]
configs
version: "3.7"
services:
  redis:
    image: redis:latest
    deploy:
      replicas: 1
    configs:
      - my_config
      - my_other_config
configs:
  my_config:
    file: ./my_config.txt
  my_other_config:
    external: true
LONG SYNTAX
version: "3.7"
services:
  redis:
    image: redis:latest
    deploy:
      replicas: 1
    configs:
      - source: my_config
        target: /redis_config
        uid: '103'
        gid: '103'
        mode: 0440
configs:
  my_config:
    file: ./my_config.txt
  my_other_config:
    external: true
container_name
container_name: nginx-test
EXAMPLE GMSA CONFIGURATION
version: "3.8"
services:
  myservice:
    image: myimage:latest
    credential_spec:
      config: my_credential_spec

configs:
  my_credentials_spec:
    file: ./my-credential-spec.json|
depends_on
  1. 服务依赖关系之间的Express依赖关系会导致以下行为:
    • docker-compose up: 以依赖顺序启动服务.
    • docker-compose up SERVICE: 自动包含SERVICE的依赖项.
    • docker-compose stop: 按依赖顺序停止服务.
version: "3.7"
services:
  web:
    build: .
    depends_on:
      - db
      - redis
  redis:
    image: redis
  db:
    image: postgres
deploy
version: "3.7"
services:
  redis:
    image: redis:alpine
    deploy:
      replicas: 6
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure
ENDPOINT_MODE
version: "3.7"

services:
  wordpress:
    image: wordpress
    ports:
      - "8080:80"
    networks:
      - overlay
    deploy:
      mode: replicated
      replicas: 2
      endpoint_mode: vip

  mysql:
    image: mysql
    volumes:
       - db-data:/var/lib/mysql/data
    networks:
       - overlay
    deploy:
      mode: replicated
      replicas: 2
      endpoint_mode: dnsrr

volumes:
  db-data:

networks:
  overlay:
LABELS
version: "3.7"
services:
  web:
    image: web
    deploy:
      labels:
        com.example.description: "This label will appear on the web service"
MODE
version: "3.7"
services:
  worker:
    image: dockersamples/examplevotingapp_worker
    deploy:
      mode: global
PLACEMENT
version: "3.7"
services:
  db:
    image: postgres
    deploy:
      placement:
        constraints:
          - node.role == manager
          - engine.labels.operatingsystem == ubuntu 14.04
        preferences:
          - spread: node.labels.zone
REPLICAS
version: "3.7"
services:
  worker:
    image: dockersamples/examplevotingapp_worker
    networks:
      - frontend
      - backend
    deploy:
      mode: replicated
      replicas: 6
RESOURCES
version: "3.7"
services:
  redis:
    image: redis:alpine
    deploy:
      resources:
        limits:
          cpus: '0.50'
          memory: 50M
        reservations:
          cpus: '0.25'
          memory: 20M
Out Of Memory Exceptions (OOME)
RESTART_POLICY
version: "3.7"
services:
  redis:
    image: redis:alpine
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
ROLLBACK_CONFIG
UPDATE_CONFIG
version: "3.7"
services:
  vote:
    image: dockersamples/examplevotingapp_vote:before
    depends_on:
      - redis
    deploy:
      replicas: 2
      update_config:
        parallelism: 2
        delay: 10s
        order: stop-first
dns
dns:
  - 8.8.8.8
  - 9.9.9.9
dns_search
dns_search:
  - dc1.example.com
  - dc2.example.com
entrypoint
entrypoint: /code/entrypoint.sh
env_file
env_file:
  - ./common.env
  - ./apps/web.env
  - /opt/secrets.env
environment
environment:
  RACK_ENV: development
  SHOW: 'true'
  SESSION_SECRET:
expose
expose:
 - "3000"
 - "8000"
external_links
external_links:
 - redis_1
 - project_db_1:mysql
 - project_db_1:postgresql
extra_hosts
extra_hosts:
 - "somehost:162.242.195.82"
 - "otherhost:50.31.209.229"
162.242.195.82  somehost
50.31.209.229   otherhost
healthcheck
healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost"]
  interval: 1m30s
  timeout: 10s
  retries: 3
  start_period: 40s
test: ["CMD", "curl", "-f", "http://localhost"]
test: ["CMD-SHELL", "curl -f http://localhost || exit 1"]
test: curl -f https://localhost || exit 1
禁用运行状况检查
healthcheck:
  disable: true
init
version: "3.7"
services:
  web:
    image: alpine:latest
    init: true
links
web:
  links:
   - db
   - db:database
   - redis
推荐阅读:
  1. docker-compose
  2. OSPF详解二之OSPF邻接关系剖析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

docker-compose指令 指令介绍 dock

上一篇:go 变量的定义

下一篇:ORA-09925: Unable to create au

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》