我有带 PostgreSQL 和 Docker 容器的主机。 PostgreSQL 在 5432 端口上工作。 Docker 容器必须连接到数据库。如何通过 Dockerfile 或运行命令连接容器与数据库? EXPOSE 5432
和 docker run -p 5432:5432 ...
没有帮助。
最佳答案
来自documentation页:
Sometimes you need to connect to the Docker host from within your container. To enable this, pass the Docker host’s IP address to the container using the
--add-host
flag. To find the host’s address, use theip addr show
command.
$ HOSTIP=`ip -4 addr show scope global dev eth0 | grep inet | awk '{print \$2}' | cut -d / -f 1`
$ docker run --add-host=docker:${HOSTIP} --rm -it busybox telnet docker 5432
EXPOSE
或 -p
标志以相反的方式工作,例如将容器端口发布到您不想要的主机。
关于linux - 带有 DB 的主机端口到 Docker 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35327522/