美文网首页
Docker-Compose简介与使用

Docker-Compose简介与使用

作者: 啾咪啾啾酱 | 来源:发表于2019-11-13 16:59 被阅读0次

说明:本人来源于Docker官方文档的个人翻译,如有疏漏,还请见谅。
原文地址:https://docs.docker.com/compose/

一、Docker-Compose的安装与基础命令

操作系统版本:CentOS7.6

1.使用root用户安装依赖

yum -y install epel-release
yum -y install python-pip python-devel  libffi-devel openssl-devel  libc-devel gcc make

2.安装docker compose(使用非root用户,这里使用的是gavin用户)

sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

3.授予执行权限

sudo chmod +x /usr/local/bin/docker-compose

4.检查是否安装成功

docker-compose --version

5.启动docker-compose

docker-compose up

6.后台运行docker-compose

docker-compose up -d

7.查看后台运行的进程

docker-compose ps

8.停止后台运行的进程

docker-compose stop

9.移除容器并删除数据卷

docker-compose down --volumes

二、Docker堆栈与分布式应用捆绑

【Produce bundle】

1.produce bundle

docker-compose bundle

【Create a stack from bundle】

2.通过docker deploy命令创建一个堆栈

docker deploy --help

3.部署之前创建一个堆栈

docker deploy vossibility-stack

4.验证服务是否正确创建

docker service ls

【Manage stacks】

5.通过docker stack命令管理堆栈

docker stack --help

【Bundle file format】
一个捆绑有两个顶级字段:version和services.

三、通过Swarm使用Compose

$ eval "$(docker-machine env --swarm <name of swarm master machine>)"
$ docker-compose up

【局限性】

1.创建镜像

If you want to use Compose to scale the service in question to multiple nodes, build the image, push it to a registry such as Docker Hub, and reference it from docker-compose.yml:

$ docker build -t myusername/web .
$ docker push myusername/web

$ cat docker-compose.yml
web:
  image: myusername/web

$ docker-compose up -d
$ docker-compose scale web=3

2.多重依赖

如果某项服务具有强制进行联合调度的类型的多个依赖项(请参阅下面的自动调度),则Swarm可能会在不同的节点上调度依赖项,从而使依赖服务无法进行调度。 例如,在这里foo需要与bar和baz共同调度:

version: "2"
services:
  foo:
    image: foo
    volumes_from: ["bar"]
    network_mode: "service:baz"
  bar:
    image: bar
  baz:
    image: baz

The problem is that Swarm might first schedule bar and baz on different nodes (since they’re not dependent on one another), making it impossible to pick an appropriate node for foo.
To work around this, use manual scheduling to ensure that all three services end up on the same node:

version: "2"
services:
  foo:
    image: foo
    volumes_from: ["bar"]
    network_mode: "service:baz"
    environment:
      - "constraint:node==node-1"
  bar:
    image: bar
    environment:
      - "constraint:node==node-1"
  baz:
    image: baz
    environment:
      - "constraint:node==node-1"

3.主机端口和重建容器

Specify a named volume, and use a volume driver which is capable of mounting the volume into the container regardless of what node it’s scheduled on.
Compose does not give Swarm any specific scheduling instructions if a service uses only named volumes.

version: "2"

services:
  web:
    build: .
    ports:
      - "80:8000"
    volumes:
      - web-logs:/var/log/web

volumes:
  web-logs:
    driver: custom-volume-driver

Remove the old container before creating the new one. You lose any data in the volume.

$ docker-compose stop web
$ docker-compose rm -f web
$ docker-compose up web

【容器编排】

1.自动编排

network_mode: "service:..." and network_mode: "container:..." (and net: "container:..." in the version 1 file format).
volumes_from
links

2.手动编排

四、Environment variables in Compose

1.在Composer files中替换环境变量

web:
  image: "webapp:${TAG}"

2.在容器中设置环境变量

docker run -e VARIABLE=VALUE ...:

web:
  environment:
    - DEBUG=1

3.将环境变量传递给容器

docker run -e VARIABLE ...:

web:
  environment:
    - DEBUG

4.env_file配置选项

docker run --env-file=FILE ...:

web:
  env_file:
    - web-variables.env

5.设置docker-compose run的环境变量

docker-compose run -e DEBUG=1 web python console.py
docker-compose run -e DEBUG web python console.py

6. .env文件

$ cat .env
TAG=v1.5
$ cat docker-compose.yml
version: '3'
services:
  web:
    image: "webapp:${TAG}"
$ docker-compose config

version: '3'
services:
  web:
    image: 'webapp:v1.5'
$ export TAG=v2.0
$ docker-compose config

version: '3'
services:
  web:
    image: 'webapp:v2.0'
$ cat ./Docker/api/api.env
NODE_ENV=test
$ cat docker-compose.yml
version: '3'
services:
  api:
    image: 'node:6-alpine'
    env_file:
     - ./Docker/api/api.env
    environment:
     - NODE_ENV=production
$ docker-compose exec api node
> process.env.NODE_ENV
'production'

五、Extend services in Compose

在文件和工程间共享Compose配置

Compose支持两种方法来共享通用配置:
1.通过使用多个Compose文件扩展整个Compose文件
2.使用扩展字段扩展单个服务(适用于2.1或更高版本的Compose文件)

【Multiple Compose files】
1.Understanding multiple Compose files
2.Example use case
<DIFFERENT ENVIRONMENTS>

docker-compose.yml
web:
  image: example/my_web_app:latest
  depends_on:
    - db
    - cache

db:
  image: postgres:latest

cache:
  image: redis:latest
docker-compose.override.yml
web:
  build: .
  volumes:
    - '.:/code'
  ports:
    - 8883:80
  environment:
    DEBUG: 'true'

db:
  command: '-d'
  ports:
    - 5432:5432

cache:
  ports:
    - 6379:6379
docker-compose.prod.yml

web:
  ports:
    - 80:80
  environment:
    PRODUCTION: 'true'

cache:
  environment:
    TTL: '500'

在生产环境部署这个docker-compose:

docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d

<ADMINISTRATIVE TASKS>

docker-compose.yml

web:
  image: example/my_web_app:latest
  depends_on:
    - db

db:
  image: postgres:latest
docker-composer.admin.yml

dbadmin:
  build: database_admin/
  depends_on:
    - db

启动一个正常的docker-compose文件和一个数据库备份的docker-compose文件

docker-compose -f docker-compose.yml -f docker-compose.admin.yml  run dbadmin db-backup

【Extending services】

请记住,使用extends不能在服务之间共享volumes_from和depends_on。
1.Understand the extends configuration

docker-compose.yml

web:
  extends:
    file: common-services.yml
    service: webapp

common-services.yml

webapp:
  build: .
  ports:
    - "8000:8000"
  volumes:
    - "/data"
    
web:
  extends:
    file: common-services.yml
    service: webapp
  environment:
    - DEBUG=1
  cpu_shares: 5

important_web:
  extends: web
  cpu_shares: 10
  
  
web:
  extends:
    file: common-services.yml
    service: webapp
  environment:
    - DEBUG=1
  cpu_shares: 5
  depends_on:
    - db
db:
  image: postgres

2.Example use case
common.yml

app:
  build: .
  environment:
    CONFIG_FILE_PATH: /code/config
    API_KEY: xxxyyy
  cpu_shares: 5

docker-compose.yml

webapp:
  extends:
    file: common.yml
    service: app
  command: /code/run_web_app
  ports:
    - 8080:8080
  depends_on:
    - queue
    - db

queue_worker:
  extends:
    file: common.yml
    service: app
  command: /code/run_worker
  depends_on:
    - queue

【Adding and overriding configuration】

# original service
command: python app.py

# local service
command: python otherapp.py

# result
command: python otherapp.py

For the multi-value options ports, expose, external_links, dns, dns_search, and tmpfs, Compose concatenates both sets of values:

# original service
expose:
  - "3000"

# local service
expose:
  - "4000"
  - "5000"

# result
expose:
  - "3000"
  - "4000"
  - "5000"

In the case of environment, labels, volumes, and devices, Compose “merges” entries together with locally-defined values taking precedence. For environment and labels, the environment variable or label name determines which value is used:

# original service
environment:
  - FOO=original
  - BAR=original

# local service
environment:
  - BAR=local
  - BAZ=local

# result
environment:
  - FOO=original
  - BAR=local
  - BAZ=local

Entries for volumes and devices are merged using the mount path in the container:

# original service
volumes:
  - ./original:/foo
  - ./original:/bar

# local service
volumes:
  - ./local:/bar
  - ./local:/baz

# result
volumes:
  - ./original:/foo
  - ./local:/bar
  - ./local:/baz

六、Networking in Compose

【Update containers】

version: "3"
services:

  web:
    build: .
    links:
      - "db:database"
  db:
    image: postgres
【Specify custom networks】
version: "3"
services:

  proxy:
    build: ./proxy
    networks:
      - frontend
  app:
    build: ./app
    networks:
      - frontend
      - backend
  db:
    image: postgres
    networks:
      - backend

networks:
  frontend:
    # Use a custom driver
    driver: custom-driver-1
  backend:
    # Use a custom driver which takes special options
    driver: custom-driver-2
    driver_opts:
      foo: "1"
      bar: "2"

【Configure the default network】

version: "3"
services:

  web:
    build: .
    ports:
      - "8000:8000"
  db:
    image: postgres

networks:
  default:
    # Use a custom driver
    driver: custom-driver-1

【Use a pre-existing network】

networks:
  default:
    external:
      name: my-pre-existing-network

六、问题解决

1.启动docker-compose报错

[gavin@gavin composetest]$ docker-compose up
ERROR: Couldn't connect to Docker daemon at http+docker://localhost - is it running?

If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.

【解决方法】

(1)使用gavin用户新建docker组,并将当前用户加入docker组

sudo groupadd docker
sudo gpasswd -a ${USER} docker

(2)使用root用户重启docker服务

systemctl restart docker.service

(3)使用gavin用户启动docker-compose

cd composetest
docker-compose up

相关文章

网友评论

      本文标题:Docker-Compose简介与使用

      本文链接:https://www.haomeiwen.com/subject/olckictx.html