Use docker-compose with docker swarm

2020-03-30 08:41发布

问题:

I'm using docker 1.12.1 I have an easy docker-compose script.

version: '2'

services:
  jenkins-slave:
    build: ./slave
    image: jenkins-slave:1.0
    restart: always
    ports:
     - "22"
    environment:
     - "constraint:NODE==master1"
  jenkins-master:
    image: jenkins:2.7.1
    container_name: jenkins-master
    restart: always
    ports:
     - "8080:8080"
     - "50000"
    environment:
     - "constraint:NODE==node1"

I run this script with docker-compose -p jenkins up -d. This Creates my 2 containers but only on my master (from where I execute my command). I would expect that one would be created on the master and one on the node. I also tried to add

networks:
  jenkins_swarm:
    driver: overlay

and

  networks:
     - jenkins_swarm

After every service but this is failing with:

Cannot create container for service jenkins-master: network jenkins_jenkins_swarm not found

While the network is created when I perform docker network ls

Someone who can help me to deploy 2 containers on my 2 nodes with docker-compose. Swarm is defenitly working on my "cluster". I followed this tutorial to verify.

回答1:

Compose doesn't support Swarm Mode at the moment.

When you run docker compose up on the master node, Compose issues docker run commands for the services in the Compose file, rather than docker service create - which is why the containers all run on the master. See this answer for options.

On the second point, networks are scoped in 1.12. If you inspect your network you'll find it's been created at swarm-level, but Compose is running engine-level containers which can't see the swarm network.



回答2:

  • We can do this with docker compose v3 now.

    https://docs.docker.com/engine/swarm/#feature-highlights https://docs.docker.com/compose/compose-file/

  • You have to initialize the swarm cluster using command

    $ docker swarm init

  • You can add more nodes as worker or manager -

    https://docs.docker.com/engine/swarm/join-nodes/

  • Once you have your both nodes added to the cluster, pass your compose v3 i.e deployment file to create a stack. Compose file should just contain predefined images, you can't give a Dockerfile for deployment in Swarm mode.

    $ docker stack deploy -c dev-compose-deploy.yml --with-registry-auth PL

  • View your stack services status -

$ docker stack services PL

  • Try to use Labels & Placement constraints to put services on different nodes.

Example "dev-compose-deploy.yml" file for your reference

version: "3"

services:

  nginx:
    image: nexus.example.com/pl/nginx-dev:latest
    extra_hosts:
      - "dev-pldocker-01:10.2.0.42”
      - "int-pldocker-01:10.2.100.62”
      - "prd-plwebassets-01:10.2.0.62”
    ports:
      - "80:8003"
      - "443:443"
    volumes:
      - logs:/app/out/
    networks:
      - pl
    deploy:
      replicas: 3
      labels:
        feature.description: “Frontend”
      update_config:
        parallelism: 1
        delay: 10s
      restart_policy:
        condition: any
      placement:
        constraints: [node.role == worker]
    command: "/usr/sbin/nginx"

  viz:
    image: dockersamples/visualizer
    ports:
      - "8085:8080"
    networks:
      - pl
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
    deploy:
      replicas: 1
      labels:
        feature.description: "Visualizer"
      restart_policy:
        condition: any
      placement:
        constraints: [node.role == manager]

networks:
pl:

volumes:
logs: