0

Not sure if this is possible, but I have a Vagrant box running ubuntu 18.04 running in VirtualBox on my macbook. I also have a docker container running on my mac which has a postgres database in it.

I would like to be able to connect from within ubuntu to the database in docker.

My first attempt at this was to add the following to my 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). 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?

1 Answer 1

1

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. All i had to do was point my database connect script to 10.0.2.2 and no extra network config was needed.

Sign up to request clarification or add additional context in comments.

1 Comment

worked for me as well! nice work =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.