简体   繁体   English

主机名 host.docker.internal 指向错误的 IP 地址

[英]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.在服务器 A 上,我们在一个 Docker 容器配置中使用“host.docker.internal”从同一服务器 A 上的另一个容器访问该容器。我们想在另一台服务器 - 服务器 B 上使用这个 Docker 环境,但是主机名“host .docker.internal”仍然指向服务器A的IP地址。

Clearing the cache, complete reinstallation of containers did not help.清除缓存、完全重新安装容器都无济于事。 Hostname "host.docker.internal" on server B still points to server A.服务器 B 上的主机名“host.docker.internal”仍然指向服务器 A。

Question: where is the binding between the IP and the hostname "host.docker.internal" recorded?问题: IP和主机名“host.docker.internal”之间的绑定记录在哪里? How can I update it to the correct IP address?如何将其更新为正确的 IP 地址?

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 : 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.有不同版本的 Docker 和host.docker.internal由 Docker Desktop 定义,而不是由 Linux 上的 Docker 定义。

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命令上使用--add-host host.docker.internal:host-gateway ,就像这样

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.这将运行busybox图像并 ping 主机。

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.您说两个容器上的 IP 地址都指向服务器 A。在 Docker 创建的 bridge.networks 上,网关通常具有相同的地址。 On Linux, the host address is always 172.17.0.1.在 Linux 上,主机地址始终为 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.这些是 docker.network 上的本地地址,因此如果您有两台主机,两台机器上的主机地址 on.networks 都将是 172.17.0.1,因此看起来它们可能指向同一台服务器。 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.但是,如果您有一个容器在主机 A 上运行,一个容器在主机 B 上运行,它们的主机地址都是相同的 IP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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