官网安装文档:

https://docs.docker.com/compose/install/

二进制文件下载地址:

https://github.com/docker/compose/releases

下载二进制文件:

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

添加执行权限:

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


配置文件 docker-compose.yaml 的使用:

version: "3.7" # 配置文件格式的版本
services: # 这个字段下面的每个服务代表一个容器

  redis:
    image: redis:alpine
    ports: # 要暴露的端口
      - "6379"
    networks: # 要加入到那个网络中去
      - frontend
    deploy:
      replicas: 2 # 要部署几个实例
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy: # 重启策略
        condition: on-failure

  db:
    image: postgres:9.4
    volumes: # 调用的存储券
      - db-data:/var/lib/postgresql/data
    networks:
      - backend
    deploy:
      placement:
        constraints: [node.role == manager] # 部署到那个主机上

  vote:
    image: dockersamples/examplevotingapp_vote:before
    ports:
      - "5000:80" # 容器端口:宿主机端口
    networks:
      - frontend
    depends_on: # 依赖的其他容器
      - redis
    deploy:
      replicas: 2
      update_config:
        parallelism: 2
      restart_policy:
        condition: on-failure

  result:
    image: dockersamples/examplevotingapp_result:before
    ports:
      - "5001:80"
    networks:
      - backend
    depends_on:
      - db
    deploy:
      replicas: 1
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure

  worker:
    image: dockersamples/examplevotingapp_worker
    networks:
      - frontend
      - backend
    deploy:
      mode: replicated
      replicas: 1
      labels: [APP=VOTING]
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 3
        window: 120s
      placement:
        constraints: [node.role == manager]

  visualizer:
    image: dockersamples/visualizer:stable
    ports:
      - "8080:8080"
    stop_grace_period: 1m30s
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    deploy:
      placement:
        constraints: [node.role == manager]

networks: # 定义的网络
  frontend:
  backend:

volumes: # 定义的存储券
  db-data:

文档:https://docs.docker.com/compose/compose-file/

试验结束之后删除镜像和网络:

docker-compose down --rmi local