Home > Enterprise >  Docker Compose with multiple .yml files and same shared network not resolving container names to ip
Docker Compose with multiple .yml files and same shared network not resolving container names to ip

Time:01-23

I have two docker compose yml files. Both should use the same network. First is the backend project with database, second a Angular frontend.

I tried the follow:

BACKEND

version: "3.7"

services:

  ....... MySQL and so on

  backend:
    container_name: backend
    build: .
    ports:
      - 3000:3000
    depends_on:
      - db
    networks:
      - explorer-docs-net

networks:
  explorer-docs-net:
    name: explorer-docs-net
    external: true

FRONTEND

version: "3.7"

services:
  frontend:
    build: .
    ports:
      - 4200:4200
    networks:
      - explorer-docs-net

networks:
  explorer-docs-net:
    name: explorer-docs-net
    external: true

Normally when all in the same yml file I can call a API in the frontend like this: http://backend/api/test (with Axios as example) and the backend will be converted to its current container IP by docker. If I use 2 yml files docker do not resolve the container name and a error occurs like this:

enter image description here

If I call docker inspect network explorer-docs-net the result looks good:

....

  "Containers": {
            "215cb01256d8e4d669064ed0b6026ce486fee027e999d2746655b090b75d2015": {
                "Name": "backend",
                "EndpointID": "0b4f7e022e38507300c049f43c880e5baf18ae993e19bb5c13892e9618688353",
                "MacAddress": "02:42:ac:1a:00:04",
                "IPv4Address": "172.26.0.4/16",
                "IPv6Address": ""
            },
            "240cfbe158f3024b90fd05ebc06b36e271bc8fc6af7d1991015ea63c0cb0fbec": {
                "Name": "frontend-frontend-1",
                "EndpointID": "c347862269921715fac67b4b7e10133c18ec89e8ea230f177930bf0335b53446",
                "MacAddress": "02:42:ac:1a:00:05",
                "IPv4Address": "172.26.0.5/16",
                "IPv6Address": ""
            },
....

So why docker do not resolve my container name when using more yml files for one shared network?

CodePudding user response:

Your browser runs on the host system, not in a container. The frontend container doesn't have a connection to the backend container. The browser loads the frontend and opens a connection to the backend.

You have to use the hostname of the host system in the frontend. Either use localhost or configure the hostname backend in /etc/hosts.

  • Related