Search code examples
dockerdocker-composedocker-stack

How to disable network generation on docker stack deploy


I am deploying a compose file onto the UCP via:

docker stack deploy -c docker-compose.yml custom-stack-name

In the end I want to deploy multiple compose files (each compose file describes the setup for a separate microservice) onto one docker network e.g. appsnetwork

version: "3"
services:

  service1:
    image: docker/service1
    networks:
      - appsnetwork

  customservice2:
    image: myprivaterepo/imageforcustomservice2
    networks:
      - appsnetwork

networks:
  appsnetwork:

The docker stack deploy command automatically creates a new network with a generated name like this: custom-stack-name_appsnetwork

What are my options?


Solution

  • Try to create the network yourself first

    docker network create --driver=overlay --scope=swarm appsnetwork
    

    After that make the network external in your compose

    version: "3"
    services:
      service1:
        image: nginx
        networks:
          - appsnetwork
    
    networks:
      appsnetwork:
        external: true
    

    After that running two copies of the stack

    docker stack deploy --compose-file docker-compose.yml stack1
    docker stack deploy --compose-file docker-compose.yml stack2
    

    Docker inspect for both shows IP in same network

    $ docker inspect 369b610110a9
      ...
                "Networks": {
                    "appsnetwork": {
                        "IPAMConfig": {
                            "IPv4Address": "10.0.1.5"
                        },
                        "Links": null,
                        "Aliases": [
                            "369b610110a9"
                        ],
    
    $ docker inspect e8b8cc1a81ed
    
                "Networks": {
                    "appsnetwork": {
                        "IPAMConfig": {
                            "IPv4Address": "10.0.1.3"
                        },
                        "Links": null,
                        "Aliases": [
                            "e8b8cc1a81ed"
                        ],