linux - 带有 DB 的主机端口到 Docker 容器

标签 linux docker port

我有带 PostgreSQL 和 Docker 容器的主机。 PostgreSQL 在 5432 端口上工作。 Docker 容器必须连接到数据库。如何通过 Dockerfile 或运行命令连接容器与数据库? EXPOSE 5432docker 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 the ip 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/

相关文章:

docker - 将Docker用于持久数据的好处是什么

Java:监听具有更多线程的单个端口与监听多个端口

Java Runtime.getRuntime().exec for unix find command with not path

docker - 使用 docker 时如何执行 ../COPY

linux - docker compose 中的 secret

integer - 什么时候应该使用 std_logic_vector,什么时候应该使用其他数据类型?

linux - FTP 服务器如何处理来自同一端口的多个连接?

linux - 如何解压 .img 文件?

linux - Bash,执行命令但继续交互式 session

linux - 如何区分 2 个目录中的所有文件并将其重定向到其他文件