Could not connect from container A to container B

Hi, I'm not sure if traefik is the problem but I will try it here.

I have two docker containers from which I have to access the other via PHP script but this not working.

docker-compose.yml from traefik:

version: "3.5"
services:
    traefik:
      image: traefik:v2.8.5
      container_name: "traefik"
      command:
      - "--api.insecure=true"
      - "--providers.docker.network=web"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      -  "--entrypoints.web.address=:80"

      ports:
        - 80:80
        - 8080:8080 #Management UI
      volumes:
        - /var/run/docker.sock:/var/run/docker.sock
      networks:
        - web

networks:
  web:
    external: true

docker-compose.yml from A:

services:

  php:
    build: ./docker
    container_name: "frontend-app"
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=web"
      - "traefik.http.routers.frontend.rule=Host(`frontend-app.docker.localhost`)"
    ports:
      - "8009:80"
    volumes:
      - ./docker/php/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
      # Source code
      - ./apps/frontend:/var/www/html
    networks:
      project:
      web:
        aliases:
          - "frontend.docker.localhost"
networks:
  project:
    driver: bridge
  web:
    external: true

docker-compose.yml from B:

services:

  php:
    build: ./docker
    container_name: "backend-app"
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=web"
      - "traefik.http.routers.backend.rule=Host(`backend.docker.localhost`)"
    ports:
      - "8008:80"
    volumes:
      - ./docker/php/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
      # Source code
      - ./apps/api:/var/www/html
    networks:
      project:
      web:
        aliases:
          - "backend.docker.localhost"
networks:
  project:
    driver: bridge
  web:
    external: true

From my host system (mac) the urls of both container are reachable and work fine. But if I'm execute a call from php routing class in container A to the url (http://backend.docker.localhost/api/) results in a 404 from container A.

Is this a problem with traefik or have anyone hints where i can found the problem?

Thanks,
Stefan

Try setting the target service port (loadbalancer.server.port , doc).

Hi,

on both docker-compose.yml i have set this

- "traefik.http.services.backend.loadbalancer.server.port=80"

(on frontend with the container-name) and restart all, but unfortunatelly not working.

To quote ChatGPT:

"Docker Compose network aliases are scoped to their specific compose projects."

So they are not available to services in other compose files.

I'm sorry, didn't understand where the problem is.

I could fix it now. The loadbalancer.server.port isn't neccessary on frontend docker-compose. I have change the URL on .env.local on the frontend system only to the container-name of the backend-system:

API_BASE_URL=http://backend-app # instead of "http://backend.docker.localhost"

and now all working fine. Thanks for support.

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