# How traefik work with more than one docker network

**URL:** <https://community.traefik.io/t/how-traefik-work-with-more-than-one-docker-network/21749>\
**Category:** Traefik v2\
**Tags:** rest-api, docker\
**Created:** [March 5, 2024, 7:25pm UTC](https://community.traefik.io/t/how-traefik-work-with-more-than-one-docker-network/21749 "2024-03-05T19:25:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rasket](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/rasket/32/8351_2.png) [@Rasket](https://community.traefik.io/u/Rasket)\
**Post date:** [March 5, 2024, 7:25pm UTC](https://community.traefik.io/t/how-traefik-work-with-more-than-one-docker-network/21749/1 "2024-03-05T19:25:36Z")

</div>

Hello, i have 2 backend service (one of them use db - **web-db** and second not - **web** ) and 1 database (postgresql) all of them are in the docker containers. But i don't want to share database to net, so i want **web-db** to use database docker container address. I create external network **db-service-network** and add it to **web-db** and also add it to traefik compose file. It works well, but now if i want to use **web** i need to modify it's compose file and add **db-service-network** even if it didn't use it. If i don't add it traefik will not give access to my **web**.

**web-db file:**

```auto
version: '3.8'

services:
  web:
    build: .
    command: bash -c 'while !</dev/tcp/db/5432; do sleep 1; done; uvicorn app.main:app --host 0.0.0.0 --port 80'
    volumes:
      - .:/app
    expose:
      - 80
    environment:
      - DATABASE_URL=postgresql://fastapi_traefik:fastapi_traefik@db:5432/fastapi_traefik
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.fastapi.rule=Host(`fastapi.localhost`) && PathPrefix(`/test/db`)"
    networks:
      - db-service-network
      - traefik

networks:
  db-service-network:
    external: true
  traefik:
    external: true

```

**traefik file:**

```auto

services:

  traefik:
    image: "traefik:v2.11"
    container_name: "traefik"
    command:
      #- "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
    ports:
      - "80:80"
      - "443:443"
      - "8081:8080"
    volumes:
      - "./traefik.dev.toml:/etc/traefik/traefik.toml"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    networks:
      - db-service-network

networks:
  default:
    name: traefik
  db-service-network:
    external: true

```

**web file:**

```auto

version: '3.8'

services:
  web:
    build: .
    command: bash -c 'uvicorn app.main:app --host 0.0.0.0 --port 80'
    volumes:
      - .:/app
    expose:
      - 80
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.fastapi.rule=Host(`fastapi.localhost`) && PathPrefix(`/test/web`)"
    networks:
      - db-service-network
      - traefik

networks:
  traefik:
    external: true
  db-service-network:
    external: true

```

If i don't add **db-service-network** traefik will show gateway timeout

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [March 5, 2024, 7:39pm UTC](https://community.traefik.io/t/how-traefik-work-with-more-than-one-docker-network/21749/2 "2024-03-05T19:39:40Z")

</div>

You need to define `docker.network` to tell Traefik which Docker network to use to forward requests. It can be global on `providers.docker` for all target services or individual per service in dynamic config (file or labels).

Note that Docker compose usually prefixed a Docker network with the project name, so use `name:` to fix this in network definition, see [simple Traefik example](https://github.com/bluepuma77/traefik-best-practice/tree/main/docker-traefik-dashboard-letsencrypt).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/containo/original/2X/b/bd81ebdb578656e76e56ff3cc3eed021d3ba132d.png) [@system](https://community.traefik.io/u/system)\
**Post date:** [March 8, 2024, 7:40pm UTC](https://community.traefik.io/t/how-traefik-work-with-more-than-one-docker-network/21749/3 "2024-03-08T19:40:36Z")

</div>

Closed on March 8, 2024, 7:40pm UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
