0

On server A, we used "host.docker.internal" in one Docker container configuration to access this container from another container on the same server A. We wanted to use this Docker environment on another server - server B, but the hostname "host.docker.internal" still points to the IP address of server A.

Clearing the cache, complete reinstallation of containers did not help. Hostname "host.docker.internal" on server B still points to server A.

Question: where is the binding between the IP and the hostname "host.docker.internal" recorded? How can I update it to the correct IP address?

docker-compose.yaml

version: '3.2'
services:
  postgres:
    build:
      context: .
      dockerfile: ./docker_files/postgres.Dockerfile
    container_name: ${POSTGRES_CONTAINER_NAME}
    restart: always
    ports:
      - target: ${POSTGRES_TARGET_PORT} # the port inside the container
        published: ${POSTGRES_PUBLISHED_PORT} # the publicly exposed port
        protocol: tcp # the port protocol (tcp or udp)
        mode: host
    extra_hosts:
      - "host.docker.internal:host-gateway"
    networks:
      - panter_docker_network
    volumes:
      - ${HOST_TRANSFER_DIRECTORY}:/tmp/host/
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
networks:
  panter_docker_network:
    driver: bridge

postgres.Dockerfile:

FROM postgres:15.1
RUN set -eux; \
    apt-get update && apt-get install -y \
    vim \
    zip \
    procps \
    locales \
    lsb-release \
    libicu-dev \
    iproute2 \
    unzip \
    iputils-ping && \
    mkdir /tmp/host/

USER root
jnemecz
  • 3,171
  • 8
  • 41
  • 77
  • check this conversation : https://stackoverflow.com/questions/74916970/cannot-connect-to-docker-containers-ip-on-forwarded-port/74917182#74917182 – Mate Jan 16 '23 at 08:22
  • Why does your database need to make outbound calls to the host? How do you see the IP address doesn't change? Why does your database need any of those tools you're installing in the Dockerfile? – David Maze Jan 16 '23 at 12:23

1 Answers1

0

There are different versions of Docker and host.docker.internal is defined by Docker Desktop but not by Docker on Linux.

The only sure-fire way to be able to reach the host is to have --add-host host.docker.internal:host-gateway on your docker run command, like this

docker run --rm --add-host host.docker.internal:host-gateway busybox ping host.docker.internal

That will run the busybox image and ping the host.

You say that the IP address on both your containers point to server A. On the bridge networks that Docker create, the gateway will usually have the same address. On Linux, the host address is always 172.17.0.1. Those are local addresses on the docker network, so if you have two host machines, the host address on networks on both machines will be 172.17.0.1, so it might look like they point to the same server. But since they're local, they do in fact point to the respective hosts.

I feel like that was a terrible explanation. But if you have a container running on host A and a container running on host B, they'll both have the same IP address for the host.

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
  • I added my `docker-compose.yaml` and Docker file, for more detail, thank you. – jnemecz Jan 16 '23 at 09:33
  • @Artegon Can you explain why you think the container on server B is pointing to server A? – Hans Kilian Jan 16 '23 at 09:46
  • Because when I `ping host.docker.internal` at server B i get reply from IP address of Server A. Really weird. – jnemecz Jan 16 '23 at 11:22
  • Both hosts should have the same IP address, so it's weird if host.docker.internal resolves to different IP addresses in the two containers. Can you edit your post and add the output from ping in both containers? – Hans Kilian Jan 16 '23 at 11:32