Archiv für den Monat: Januar 2023

Inter Container Communication

Problem

Running services in container enrironments often leads to the need enabling access from one conteiner to another. This is quiet easy, if we are runnung all containers in the same docker net. If the containers running in different networks, we have to access the other container by connecting the exposed port on the host.
Since we are running in a container localhost is refering to the container itself. In the first it seems simple to connect to IP or hostname of the host in its LAN, but this is not very flexible. If the host gets another IP oder the container moves to another system, the config allways has to be adopted.

Possible Solution

While instancing the container, we create an entry in the docker internal address solution to refere to the host.

docker run -d \
  --restart: always \
  --add-host host.docker.internal:host-gateway \
  alpine:latest

Or within
“`docker-compose.yaml“` …

...
services:
  my_host:
    restart: always
    image: alpine:latest
    extra_hosts:
      - host.docker.internal:host-gateway
...