DB到Docker容器的主机端口 [英] Host port with DB to Docker container

查看:118
本文介绍了DB到Docker容器的主机端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我托管了PostgreSQL和Docker容器. PostgreSQL在5432端口上工作. Docker容器必须连接到数据库.如何通过Dockerfile或运行命令将容器与数据库连接? EXPOSE 5432docker run -p 5432:5432 ...没有帮助.

I have host with PostgreSQL and Docker container. PostgreSQL work on 5432 port. Docker container must connect to database. How to connect container with database through Dockerfile or run command? EXPOSE 5432 and docker run -p 5432:5432 ... did not help.

推荐答案

来自

有时您需要从您的内部连接到Docker主机 容器.要启用此功能,请将Docker主机的IP地址传递给 使用--add-host标志的容器.要查找主持人的地址,请使用 ip addr show命令.

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标志以其他方式工作,例如在此处发布您不需要的容器端口以托管.

EXPOSE or -p flag work the other way around e.g. publish container ports to host which you don't want here.

这篇关于DB到Docker容器的主机端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆