简体   繁体   中英

Hostname host.docker.internal is pointing to the wrong IP address

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM