Search code examples
dockerdocker-swarmswarm

docker stack deploy doesn't use overlay network created


I created an overlay network using this command:

docker network create --driver overlay --internal --subnet 192.168.0.0/24 test

and then I tried deploying a stack with one service. the stack's yaml file looks like:

---
version: "3.0"
services:
  web1:
    image: "test"
    ports:
       - "12023:22"
    hostname: "ard014-web1"
    networks:
      - test
networks:
  test:
    driver: overlay

I deploy the stack using the command:

docker stack deploy -c test.yaml test

if I check the networks with docker network ls I find new overlay network gets created with name test_test and the container uses this network instead of the already created one. is this an expected behavior? I want the stack to use the network I created manually instead of creating new network because I want to control the ip CIDR range used by the overlay network. how can I achieve that?


Solution

  • To use an existing network, define it as "external":

    version: "3.0"
    services:
      web1:
        image: "test"
        ports:
           - "12023:22"
        hostname: "ard014-web1"
        networks:
          - test
    networks:
      test:
        external: 
          name: test
    

    More details here: https://docs.docker.com/compose/compose-file/#external-1