Problem to forward requests when container has more than one network

Hello,
I'm having problems to forward requests when container has more than one network.
In the docker-compose file, I create 2 networks: frontend for apps and backend for database. The container that is causing me problems is the dspace that has the 2 networks. When I leave only the frontend network the routing of the traefik works.
I've tried using the options "providers.docker.network=frontend" and "traefik.docker.network=frontend" but still don't work.

docker-compose.yml
#################################################################################
version: "3.7"

services:

traefik:
image: "traefik:v2.2"
container_name: "traefik"
command:
# - "--log.level=DEBUG"
#- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
- "--api.dashboard=true"
- "--providers.docker.exposedbydefault=false"
- "--providers.docker.network=frontend"
- "--accesslog=true"
- "--accesslog.filepath=/tmp/acceess.log"
- "--log.level=DEBUG"
- "--log.filePath=/tmp/traefik.log"
- "--log.format=json"

labels:
  - "traefik.enable=true"
  - "traefik.http.routers.traefik.rule=Host(`traefik.local`)"
  - "traefik.http.routers.traefik.service=api@internal"
  - "traefik.http.routers.traefik.middlewares=traefik-auth"
  - "traefik.http.middlewares.traefik-auth.basicauth.users=admin:$$apr1$$bJwG68Gg$$JWru9HMqsduYytGNpr6rO."
  - "traefik.port=8080"
ports:
  - "80:80"

- "9090:8080"

networks:
  - frontend
volumes:
  - "/var/run/docker.sock:/var/run/docker.sock:ro"
  - "/etc/localtime:/etc/localtime:ro"

#################################################################################
site:
image: httpd:2.4-alpine
container_name: site
restart: on-failure
ports:
- "70:80"
labels:
- "traefik.enable=true"
- "traefik.http.routers.site.rule=Host(site.lmts.com.br)"
- "traefik.http.routers.site.entrypoints=web"
- "providers.docker.network=frontend"
networks:
- frontend
volumes:
- "/etc/timezone:/etc/timezone:ro"
- "/etc/localtime:/etc/localtime:ro"
- "site:/var/www/html"

####################################################################################
dspacedb:
image: dspace/dspace-postgres-pgcrypto
# Note that a separate image exists for DSpace 4x
# image: dspace/dspace-postgres-4x
container_name: dspacedb
environment:
- PGDATA=/pgdata
volumes:
- pgdata:/pgdata
- "./dspace/add-ons/dspace-docker-tools:/dspace-docker-tools"
networks:
- backend
tty: true
stdin_open: true

dspace:
image: "{DOCKER_OWNER:-dspace}/dspace:{DSPACE_VER:-dspace-6_x-jdk8-test}"
container_name: dspace
environment:
# - JAVA_OPTS=
# The following memory allocation will be appended to any JAVA_OPTS settings
# - JAVA_MEM=-Xmx2500m
- SCRIPTVER=6
- DSPACE_INSTALL=/dspace
- ADMIN_EMAIL={ADMIN_EMAIL:-test@test.edu} - ADMIN_PASS={ADMIN_PASS:-admin}
- ADMIN_FNAME={ADMIN_FNAME:-AdminFirst} - ADMIN_LNAME={ADMIN_LNAME:-AdminLast}
- AIPZIP={AIPZIP:-https://github.com/DSpace-Labs/AIP-Files/raw/master/dogAndReport.zip} # export SKIPAIP=Y - SKIPAIP={SKIPAIP:-N}
# Number of sec to wait before attempting AIP ingest
- AIPWAIT=5
# Number of sec to wait before attempting to start tomcat
- DBWAIT=0
# Env vars with double underbars in names will be replaced with periods and written to local.cfg
# The defaul values for local.cfg will be provided here
# D -> -
# P -> .
- dspace__P__dir=/dspace
- db__P__url=jdbc:postgresql://dspacedb:5432/dspace
- dspace__P__hostname=localhost
- dspace__P__baseUrl=http://dspace.lmts.com.br
- dspace__P__name=DSpace Started with Docker Compose
labels:
- "traefik.http.routers.dsapce.rule=Host(dspace.lmts.com.br)"
- "traefik.http.routers.dspace.entrypoints=web"
- "traefik.port=8080"
- "traefik.enable=true"
- "traefik.http.services.dspace.loadbalancer.server.port=8080"
- "providers.docker.network=frontend"
- "traefik.docker.network=frontend"
ports:
- 8888:8080
volumes:
- "assetstore:/dspace/assetstore"
- "./dspace/add-ons/dspace-docker-tools:/dspace-docker-tools"
- "./dspace/add-ons/mirage2/xmlui.xconf:/dspace/config/xmlui.xconf"
- "solr_authority:/dspace/solr/authority/data"
- "solr_oai:/dspace/solr/oai/data"
- "solr_search:/dspace/solr/search/data"
- "solr_statistics:/dspace/solr/statistics/data"
- "dspace_config:/dspace/config/"
- "/etc/timezone:/etc/timezone:ro"
- "/etc/localtime:/etc/localtime:ro"
entrypoint: /dspace-docker-tools/ingestAIP.sh
networks:
- frontend

- backend

depends_on:
  - dspacedb
tty: true
stdin_open: true

####################################################################################
volumes:
site:
pgdata:
assetstore:
solr_authority:
solr_oai:
solr_search:
solr_statistics:
dspace_config:

networks:
frontend:
backend:

A few points:

  • Learn about the difference between static and dynamic configuration. It's a simple but not obvious concept, and it will make your life while configuring traefik easier. I'm saying that, because in your example you are trying to use static and dynamic configurations interchangeably.
  • The formatting of yaml in your post is off. With yaml formatting in general, and spaces / indentation in particular are very important. I've seen numerous cases when people had problems with traefik because they failed to format their yaml correctly. If you want someone possibly try and reproduce your problem you increase chances of that by showing something that can be copied and pasted and have a chance being the same thing that you are using. Currently chances of that are slim.
  • Better yet create a small reproducible example that one can easily take and docker-compose up to see your problem. There is a good chance, that while working on that example you will arrive to answer yourself. Remove all irrelevant containers, only leave traefik and the one you have problems with. If that container require dependencies, try substitute it one that does not. In deconstructing your problem this way you make it simpler, and also more accessible to possible helpers.
  • I personally used traefik with containers on two different networks and I know that it works.
  • "Do not work" is not a very good problem description. What where your expecting to see and what have you observed? Where there any errors? Did you examine the debug log?

Here is an example that works:

version: '3.7'
services:
  traefik:
    image: traefik:2.2.1
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - frontend
    ports:
      - "80:80"
    command:
      - "--api=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entryPoints.web.address=:80"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.traefik.rule=Host(`traefik.internal`)"
      - "traefik.http.routers.traefik.service=api@internal"
  whoami:
    image: containous/whoami:v1.5.0
    networks:
      - frontend
      - backend
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=frontend"
      - "traefik.http.routers.whoami.rule=Host(`whoami.internal`)"
networks:
  frontend:
    name: frontend
  backend:
    name: backend

Good luck!

For some containers we have to explicitly specify the network to use with traefik:

  web:
    labels:
      - "traefik.docker.network=networkname"
      - .......

Hi Jogi,

First of all, thank´s for your help.
I already declared the option to specify the network in the dspace container.
"traefik.docker.network=frontend"

Hi Zespri,
First of all thank's for your help and Excuse me for not meeting your high expectations regarding the formulation of the question.

1º - I didn't really understand the question of static and dynamic, my fault. I hate the traefik documentation (with your help now I understand better)

2º - I know about de formatting of yaml, the process of copying and pasting really messed up the yaml code, but the real thing is OK

3º - In my opinion, nothing better than the real example of the problem

4º Maybe it was wrong in english, but I didn't mean that it didn't work at all, what I meant was that it wasn't working for me.

5º With your help I found the problem. In docker-compose.yaml file I created the networks these way:
networks:
frontend:
backend:

After execute the command docker network ls, I realized that the docker created networks with a different name (docker_frontend and docker_backend).
After see your code I change to:
networks:
frontend:
name: frontend
backend:
name: backend

And now, traefik is working