简体   繁体   English

从来宾游民游箱连接到主机上Docker中的PostgreSQL中的db

[英]Connect to postgres db in docker on host from guest vagrant box

Not sure if this is possible, but I have a Vagrant box running ubuntu 18.04 running in VirtualBox on my macbook. 不知道这是否可能,但是我在Macbook上的VirtualBox中有一个运行ubuntu 18.04的Vagrant盒。 I also have a docker container running on my mac which has a postgres database in it. 我也有一个在我的Mac上运行的docker容器,其中有一个postgres数据库。

I would like to be able to connect from within ubuntu to the database in docker. 我希望能够从ubuntu内部连接到docker中的数据库。

My first attempt at this was to add the following to my VagrantFile 我的第一次尝试是将以下内容添加到我的VagrantFile中

config.vm.network "forwarded_port", guest: 5432, host: 5432

However that resulted in the following error: 但是,这导致以下错误:

Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 5432 is already in use
on the host machine.

To fix this, modify your current project's Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:

  config.vm.network :forwarded_port, guest: 5432, host: 1234

Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding. You could
try 'vagrant reload' (equivalent of running a halt followed by an up)
so vagrant can attempt to auto-correct this upon booting. Be warned
that any unsaved work might be lost.

I understand the error, it makes perfect sense because the host is already listening to port 5432 (or rather the docker/postgres container is). 我理解该错误,因为主机已经在侦听端口5432(或更确切地说是docker / postgres容器),所以这很有意义。 but i don't know how to fix the problem. 但是我不知道如何解决这个问题。 If i do as suggested and change the host port number it would not work because then I wouldn't be connecting to the right port. 如果我按照建议进行操作并更改主机端口号,它将无法正常工作,因为那样我就无法连接到正确的端口。

Suggestions? 有什么建议吗?

I figured it out. 我想到了。 it's pretty easy actually. 实际上这很容易。

You see, the docker postgres db is availabe at 'localhost' or '127.0.0.1' on the HOST machine, but the HOST machine appears at 10.0.2.2 for some reason to the GUEST machine. 您会看到,docker postgres db位于HOST机器上的“ localhost”或“ 127.0.0.1”,但是出于某些原因,GUEST机器上的HOST机器显示为10.0.2.2。 All i had to do was point my database connect script to 10.0.2.2 and no extra network config was needed. 我要做的就是将我的数据库连接脚本指向10.0.2.2,并且不需要额外的网络配置。

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

相关问题 从 Windows Docker 主机连接到 dockerized postgres - Connect to dockerized postgres from Windows Docker host 从主机连接到docker容器中的postgres - Connect to postgres in docker container from host machine 无法从Homestead Vagrant框中连接到Heroku Postgres数据库 - Can't connect to Heroku Postgres database from Homestead Vagrant box 无法通过主机归纳连接到Vagrant guest box中的Postgresql数据库? - Trouble connecting to Postgresql database in Vagrant guest box with Induction from host machine? 有人可以指出为什么我无法从主机连接到来宾的PGSQL吗? (流浪汉) - Can someone point out why I can't connect to PGSQL on guest from host? (vagrant) 如何从主主机访问 docker postgres db - How to access docker postgres db from main host 无法从Windows主机上运行的PGAdmin连接到Docker中运行的Postgres - Cannot connect to Postgres running in Docker from PGAdmin running on Windows Host 如何从外部主机连接到使用 docker-compose 创建的 postgres - How to connect to postgres created with docker-compose from outside host 如何从主机连接到Docker Postgres容器 - How to Connect to Docker Postgres Container from Host Machine 如何从 Docker 连接到主机的 Postgres? - How do I connect to the host machine's Postgres from Docker?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM