西西的小布丁 2024-03-29 18:15 采纳率: 0%
浏览 15

docker-ipv6

使用容器编排pg,部署后使用ipv6地址访问失败,
本地地址

img

容器

img

docker run --name pgdata-ipv6_test --network bridge -p 22345:22345 -e POSTGRES_PASSWORD=12345 --ulimit nofile=65536:65536 -v /data/postgres/date:/var/lib/postgresql/data/ -d postgres:list

navicat失败提示

img

  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-03-29 18:15
    关注

    引自免费微信小程序:皆我百晓生

    在 Docker 中,使用容器编排 PostgreSQL,并使用 IPv6 地址进行通信时,可能出现网络配置错误导致无法访问的问题。以下是详细的解决方案:

    1. 检查网络配置: a. 在 Dockerfile 中,确保已经正确配置了容器的网络接口(--network)并指定了外网 IP 地址。

      FROM postgres
      DOCKER network add bridge
      

      b. 在 docker run 命令行中,使用 -p 指令指定外网 IP 地址与内部容器间的数据端口,以便应用连接到容器。

      docker run -it --name pgdata-ipv6_test -p 22345:22345 -e POSTGRES_PASSWORD=12345 --ulimit nofile=65536:65536 -v /data/postgres:date:/var/lib/postgresql/data/ -d postgres:latest
      
    2. 修改外部主机的 IP 地址: 如果外部主机的 IP 地址与内核或 Docker 配置文件中的默认 IP 不匹配,可能需要更新或配置外部主机的 DNS 服务器设置。 a. 使用 dig 命令获取外部主机的默认 DNS 邮件服务器 IP 地址,并将其替换为 Docker 实例的容器 ID($IP)。

      dig @external-host-ip-dns.org TXT | grep ^A [1-9]
      

      b. 将新地址保存为 /etc/resolv.conf 文件,其中包含以下部分:

      # nginx example
      nameserver external-host-ip-dns.org;
      nameserver 192.168.1.1;
      
      domain name_servers {
        internal 192.168.1.1;
      }
      
      resolver_nameservers {
        external-host-ip-dns.org;
      }
      
    3. 编辑 Dockerfile 并添加 ipv6 初始数据映射: To enable IPv6 support in the PostgreSQL container and allow it to use the specified IPv6 address during networking setup, update your Dockerfile by adding the following lines before running the postgres:latest image:

      FROM postgres
      DOCKER networks add bridge
      ENV "POSTGRES_USER" "postgres"
      ENV "POSTGRES_PASSWORD" "12345"
      ADD symbolic-link /etc/postgresql/data/pg_hba.conf /etc/postgresql/data/pg_hba.conf.bak
      COPY /etc/postgresql/data/pg_hba.conf.bak /etc/postgresql/data/pg_hba.conf
      CHOWN root:root /etc/postgresql/data/pg_hba.conf
      

      This will create a symbolic link named pg_hba.conf that points to the original pg_hba.conf file from within the Docker image. The CHOWN command changes ownership of this file to the root user (root) so that PostgreSQL can write its configuration files under the correct directory.

    4. 启动 Docker 容器并检查连接: Use the following command to start the PostgreSQL container with IPv6 connectivity:

      docker run -it --name pgdata-ipv6_test --network bridge -p 22345:22345 -e POSTGRES_PASSWORD=12345 --ulimit nofile=65536:65536 -v /data/postgres$date:/var/lib/postgresql/data/ -d postgres:latest
      

      Now, use the ping command to verify if the PostgreSQL container is accessible over IPv6 using the external host's public IPv6 address.

      ping 2409:8074:5a06:5::fa:2791
      

      If the ping response is successful (i.e., the port number is open), you should be able to access the PostgreSQL database using an IPv6 address or tunneling protocol such as netcat or VirtualBox Network Adapter settings.

    5. 测试 TCP 连接: If the issue persists after ensuring proper network configuration, try establishing a TCP connection between the host and PostgreSQL container using telnet. You can do this by creating a TCP listener on the host-side with nc tool and then connecting to the container through telnet <container-id>:<port>. For example, on a Linux system:

      nc -l <container-id>:22345
      

      In the response, you should see a connection established with the following output:

      Hello, World!
      

      Now, attempt to login to the PostgreSQL container via SSH with the username postgres and password 12345, assuming that the server has been configured to accept connections on the specified IPv6 address.

    If the above steps do not solve the issue or you encounter further issues, here are some additional troubleshooting tips:

    • Verify that the PostgreSQL container is properly stopped and has stopped listening on the specified ports: Use docker ps command to list all running containers and filter by the --status option with a label like running and onipv6.
    • Check the firewall rules on the host system: Ensure that there are no防火墙 rules preventing the incoming traffic from the external host to reach the PostgreSQL container. Open port 22345 in the appropriate firewall rule or add a new one if necessary.
    • Test the network configuration from the host machine: Access the PostgreSQL container from another host machine that has a direct connection to the host's interface and verify that the same issue occurs.
    • Validate the host's IP address configuration: Confirm that the Docker host is connected to the internet and that its IP address matches the one listed in the /etc/resolv.conf file.
    • Reinstall Docker and PostgreSQL if necessary: If any of the previous steps did not resolve the issue, remove Docker and reconfigure them, followed by starting PostgreSQL again.
    • Update the PostgreSQL service configuration: If Docker networking is enabled but the problem persists, check the service configuration options in the PostgreSQL data directory (~/.pgdata). Adjust the service configuration to use the specified IPv6 address instead of the local host's IP address.
    • Consult the official documentation for PostgreSQL and Docker for more specific information about networking configurations and error messages related to IPv6 connectivity.

    Remember to backup any data before making any significant changes to the Docker environment or modifying the PostgreSQL service configuration, as a sudden restart may cause data loss.

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 3月29日