docker-compose

https://github.com/docker/compose

通过一个yaml文件来管理容器中的服务,包括网络和存储。

安装:

1
2
3
https://docs.docker.com/compose/install/
$ sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose

docker-compose命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
-f/--file
-p/--project-name # 默认目录名
-H/--host

# 拉取compose文件中指定的镜像
$ docker-compose -f service.yml pull

# 根据docker-compose.yml把stack打包成一个Distributed Application Bundles文件.
$ docker-compose bundle -o <project name>.dab

$ docker-compose start [servoce...]
$ docker-compose stop [service...]
$ docker-compose restart [service...]
$ docker-compose up -d [service...]
$ docker-compose down -v

$ docker-compose logs -f

docker-compose.yml

compose文件

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
version: "3.6"
services:
  mongo:
    image: mongo:latest
    hostname: hostname
    deploy: // for swarm
    networks:
    - mynetwork
    volumes: // short syntax
    - myvolume:/container/dir
    volumes: // long syntax
    - type: valume/bind/tmpfs
      source: 
      target:
      read_only:
      bind:
      volume:
      tmpfs:
      consistency:
    ports:  // long syntax
    - target: 80
      published: 8080
      mode: host
      protocol: tcp/udp
    ports: // short syntax
      - 80:80
      - 1234:1234/udp
    environment:
      RABBITMQ_DEFAULT_USER: sandbox
      RABBITMQ_DEFAULT_PASS: password
    environment:
      - RABBITMQ_DEFAULT_USER=sandbox
      - RABBITMQ_DEFAULT_PASS=password
    depends_on:
      - service-name
    command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]

    // 在compose v3 中针对非swarm模式的container做资源限制等操作.
    // --compatibility If set, Compose will attempt to convert keys in v3 files to their non-Swarm equivalent
    $ docker-compose --compatibility up -d
    // --compatibility 支持下面三种key:
    replicas:
    restart_policy:
      condition:
      max_attempts:
    resources:
      limits:
        cpus: '0.5'
        memory: 1G
      reservations:
        cpus: '0.25'
        memory: 20M

    # 下列选项不能用于swarm stack部署.
    build
    cgroup_parent
    container_name
    devices
    tmpfs
    external_links
    links
    network_mode
    restart
    security_opt
    sysctls
    userns_mode
​
// 使用已经创建好的网络
networks:
  mynetwork:
    external:
      name: lan0
​
// 创建bridge网络
networks:
  mynetwork:
    driver: bridge
    driver_opts:
      com.docker.network.bridge.name: lan0
    ipam:
      driver: default
      config:
        - subnet: 192.168.1.0/24

// 创建overlay网络
networks:
  ol0:
    driver: overlay
    attachable: true
    driver_opts:
      com.docker.network.bridge.name: ol0
    ipam:
      driver: default
      config:
      - subnet: 172.12.0.0/16

// 解析为: "line1 line2\n",  会自动加换行符.
command: >
  line1
  line2

// 解析为: "line1 line2", 没有换行符.
command: >-
  line1
  line2

# compose文件中用到的变量
.Service.ID Service ID 
.Service.Name Service name 
.Service.Labels Service labels 
.Node.ID Node ID 
.Node.Hostname Node Hostname 
.Task.ID Task ID 
.Task.Name Task name 
.Task.Slot Task slot 

Kompose

https://github.com/kubernetes/kompose

install:

1
2
3
4
$ curl -L https://github.com/kubernetes/kompose/releases/download/v1.19.0/kompose-linux-amd64 -o kompose

$ chmod +x kompose
$ sudo mv ./kompose /usr/local/bin/kompose