简体   繁体   English

如何使docker容器可访问postgres.app数据库?

[英]How to make postgres.app db accessible to docker container?

I got postgres.app running locally on my Mac and would like my local docker container to be able to connect to it. 我在Mac上本地运行了postgres.app,并希望本地docker容器能够连接到它。 How do I best do this? 我如何最好地做到这一点?

I found this post that suggests to pass the Docker host's IP address to a container using the --add-host flag ( Host port with DB to Docker container ). 我发现这篇文章建议使用--add-host标志( 带有DB到Docker容器的Host端口 )将Docker主机的IP地址传递到容器 However, my laptop's IP address changes frequently. 但是,我的笔记本电脑的IP地址经常更改。 Isn't there an easier way of doing this? 有没有更简单的方法? Isn't there an easy way to open a local port to a container? 有没有简单的方法可以打开容器的本地端口?

Few things 一些事情

  • Use docker.for.mac.localhost as your HOST (This assumes you have the latest Docker for Mac as @Pete mentioned) 使用docker.for.mac.localhost作为您的主机(假设您拥有docker.for.mac.localhost提到的最新的Mac版Docker)
  • Make sure there is such a record in ~/Library/Application Support/Postgres/var-9.6/pg_hba.conf 确保~/Library/Application Support/Postgres/var-9.6/pg_hba.conf有这样的记录

host all all 0.0.0.0/0 trust

  • Change this line listen_addresses = 'localhost' in ~/Library/Application Support/Postgres/var-9.6/postgresql.conf ~/Library/Application Support/Postgres/var-9.6/postgresql.conf更改此行listen_addresses = 'localhost'

to

listen_addresses = '*'

or 要么

listen_addresses = 'localhost, docker.for.mac.localhost'

If we are talking about a developers workstations, you could start your Docker Container inside the Host Network. 如果我们在谈论开发人员工作站,则可以在主机网络内启动Docker容器。

docker run --net=host myContainer

So your container runs in the same stack as your Host, and should be able to access your postgres.app. 因此,您的容器与主机在同一堆栈中运行,并且应该能够访问postgres.app。

From your container, try connecting to hostname docker.for.mac.localhost . 从您的容器中,尝试连接到主机名docker.for.mac.localhost For example: 例如:

psql -U my_user docker.for.mac.localhost -U my_user my_database

From the docs : 从文档

The Mac has a changing IP address (or none if you have no network access). Mac的IP地址正在更改(如果没有网络访问权限,则没有IP地址)。 From 17.06 onwards our recommendation is to connect to the special Mac-only DNS name docker.for.mac.localhost which will resolve to the internal IP address used by the host. 从17.06开始,我们的建议是连接到专用于Mac的特殊DNS名称docker.for.mac.localhost ,它将解析为主机使用的内部IP地址。

Note: this requires Docker for Mac >= 17.06.0-ce-mac18, 2017-06-28 注意:这需要Docker for Mac >= 17.06.0-ce-mac18, 2017-06-28

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM