Search code examples
dockerdocker-composedocker-swarmdocker-network

Creating a network on docker swarm between docker-compose applications and other containers


So, let's say I have an application that gets deployed to Docker Swarm and it has a compose file that looks something like:

version: '2'

services:
  postgres:
    image: postgres
    environment: 
      - POSTGRES_PASSWORD=password
    networks:
      - app-network
    volumes: 
      - postgres:/var/lib/postgresql/data

  myapp:
    container_name: 'myapp'
    image: myapp:debug
    depends_on:
      - postgres
    build:
      context: .
      dockerfile: Dockerfile.debug
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

volumes:
  postgres:

Maybe I want to have several of these applications deployed and they communicate via a rabbitmq container. I thought I could just create a network on the swarm like so:

$ docker network create -d overlay common-network

And then I could provision a rabbitmq instance like so:

$ docker run -d \
--hostname rabbit \
--name rabbit \
-e RABBITMQ_ERLANG_COOKIE='bleh' \
-e RABBITMQ_DEFAULT_USER='rabbitmq' \
-e RABBITMQ_DEFAULT_PASS='rabbitmq' \
-e RABBITMQ_DEFAULT_VHOST='/' \
rabbitmq:3

Then deploy my applications:

$ docker-compose up

But, it doesn't look like the applications can reach "rabbit".

$ docker network ls
NETWORK ID          NAME                                                 DRIVER              SCOPE
176e455e8215        common                                               overlay             global
8f1b14690f2b        swarm-agent-40001BF7000000/bridge                    bridge              local
3cfaae53a0dc        swarm-agent-40001BF7000000/host                      host                local
a01b27b94fcc        swarm-agent-40001BF7000000/none                      null                local
aa6f5b56a6d3        swarm-agent-40001BF7000000/publish_app-network   bridge              local

The applications can see services that are defined in the same compose file (as shown above), but they can't see services outside of that.

How can I get all the applications on the same network?

EDIT 1 I have also tried changing the compose file to this:

networks:
      - app-network
      - common

networks:
  adapter-network:
    driver: bridge
  common:
    external: true

Solution

  • So, it turns out I just needed to create the rabbitmq service with a --net argument:

    docker run -d \
    --hostname rabbit \
    --name rabbit \
    --net common \
    -e RABBITMQ_ERLANG_COOKIE='blah' \
    -e RABBITMQ_DEFAULT_USER='rabbitmq' \
    -e RABBITMQ_DEFAULT_PASS='rabbitmq' \
    -e RABBITMQ_DEFAULT_VHOST='/' \
    rabbitmq:3
    

    With a reference to the "common" network in the compose file:

    networks:
          - app-network
          - common
    
    networks:
      adapter-network:
        driver: bridge
      common:
        external: true