Search code examples
dockerdocker-networkdocker-stack

Multiple docker networks on a single container


I have a docker stack configuration with an overlay network called traefik. It contains my traefik reverse-proxy container and then several containers that serve various subdomains. I'm adding a new one that'll need to access a database server that I've created in another container, so I added something like this:

networks:
  traefik:
    driver: overlay
  database:
    driver: overlay

services:
  traefik:
    image: traefik
    networks:
      - traefik
    ports:
      - 80:80
      - 443:443
      - 8080:8080
    volumes:
      # ...

  # ...

  database:
    image: postgres:9.6-alpine # match what's being used by heroku
    networks:
      - database

  staging:
    image: staging
    networks:
      - traefik
      - database
deploy:
  labels:
    traefik.enable: "true"
    traefik.frontend.rule: Host:lotto-ticket.example.com
    traefik.docker.network: traefik
    traefik.port: 3000

When I did this, the reverse proxy started returning Gateway Timeout codes, indicating that the staging container was no longer available to the traefik network. If I remove the database network from the staging container, the subdomain serves up as expected (although it obviously fails to access the database), and when I add it back in, the reverse-proxy starts getting timeouts from it.

Is there some kind of conflict between the two networks? Do I need to configure IP routing in order to use multiple docker networks on a single container?

EDIT: added more detail to the config excerpt


Solution

  • You need to tell traefik on which network to connect to your service using the label traefik.docker.network. In your case, that label would be set to ${stack_name}_traefik where ${stack_name} is the name of your stack.