Docker networks and Traefik

Viewed 1586

Here's my context: I have a caddy+php+mysql blog project with his docker-compose file, and I need to put a traefik reverse proxy in front. To test locally, I plan to serve this on blog.localhost to see what happens.

When I stack everything in the same docker-compose.yml, it works like a charm; But when I cut this in 2 different docker-compose.yml files, with the traefik in his own, I get some "Gateway Timeout".

I guess there is an issue of networks, that every containers into a single composition implicitely has a network between them, but between 2 files I need to declare one (first question : am I right ?)

But I tried to declare a network and nothing happened.

In fact, my issue can be reduced to this very case, the typical basic example from the traefik's official documentation:

version: "3.3"

services:
  traefik:
    image: "traefik:v2.3"
    container_name: "traefik"
    command:
      #- "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

  whoami:
    image: "traefik/whoami"
    container_name: "simple-service"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"
      - "traefik.http.routers.whoami.entrypoints=web"

The question is how do you make this works in 2 separated docker-compose files / docker run commands ?

Thank you very much in advance !

2 Answers

OK I get it:

  1. create an external network (out of the compositions) with a "docker network create proxy" command.
  2. Link your traefik reverse proxy AND your composition(s) (in this case, at the end of the whoami docker-compose.yml file you will create) to this network with the following:
networks:   
  default:
    external:
      name: proxy
  1. Launch your 2 compositions
  2. Profit

My only concern is that I didn't find a way to create this network directly in the traefik's docker-compose.yml file. Maybe we can't create an external network IN a composition ?

Way to create this network directly:

https://docs.docker.com/compose/networking/

Networks can also be given a custom name (since version 3.5):

version: "3.9"
services:
  # ...
networks:
  frontend:
    name: custom_frontend
    driver: custom-driver-1
Related