使用docker stack 部署你的服务

admin


本文最后更新于 2020年08月13日

差不多可以抛弃docker-compose了。docker-compose被docker官方定位于开发,而不是部署,docker官方的建议是,使用docker stack 来部署容器

并且docker stack 是原生内置于docker,不需要单独安装,二者之间的差异,可以参照官方文档。当然企业中依然是K8S为主导地位

简单示例

我推荐WordPress的官方文档,写的非常详细,简单明了

docker stack 兼容 V3版本,基本无需修改即可使用

Code example
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
version: '3.1'
 
services:
 
  wordpress:
    image: wordpress
    restart: always
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - wordpress:/var/www/html
 
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql
 
volumes:
  wordpress:
  db:

-c 指定yml文件,deploy可以换成up

Code example
1
docker stack deploy -c stack.yml wordpress

CLI

Code example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
root@debian:~# docker stack deploy --help
 
Usage:  docker stack deploy [OPTIONS] STACK
 
Deploy a new stack or update an existing stack
 
Aliases:
  deploy, up
 
Options:
      --bundle-file string     Path to a Distributed Application Bundle file
  -c, --compose-file strings   Path to a Compose file, or "-" to read
                               from stdin
      --orchestrator string    Orchestrator to use (swarm|kubernetes|all)
      --prune                  Prune services that are no longer referenced
      --resolve-image string   Query the registry to resolve image digest
                               and supported platforms
                               ("always"|"changed"|"never") (default "always")
      --with-registry-auth     Send registry authentication details to
                               Swarm agents

初始化

第一次使用docker stack需要初始化

Code example
1
docker swarm init
Code example
1
this node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again


1.273636s