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

容器

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失败提示

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

容器

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失败提示

关注引自免费微信小程序:皆我百晓生
在 Docker 中,使用容器编排 PostgreSQL,并使用 IPv6 地址进行通信时,可能出现网络配置错误导致无法访问的问题。以下是详细的解决方案:
检查网络配置:
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
修改外部主机的 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;
}
编辑 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.
启动 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.
测试 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:
docker ps command to list all running containers and filter by the --status option with a label like running and onipv6./etc/resolv.conf file.~/.pgdata). Adjust the service configuration to use the specified IPv6 address instead of the local host's IP address.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.