Access PostgreSQL running in Docker container in Vagrant

I have a Vagrant box in which I have a Docker container that runs PostgreSQL. The container is the official one found here

I want to be able to connect to Postgres from the host (i.e outside of Vagrant) using psql but can’t get it to work. (Get a “could not connect”-error). I have added a port forward in my Vagrantfile:

  • Getting started with Laradock on Mac
  • Docker for mac not starting after latest version upgrade Version 1.12.1 (build: 12133) 2d5b4d9c3daa089e3869e6355a47dd96dbf39856
  • Docker on Elastic Beanstalk - connect() failed (111: Connection refused) while connecting to upstream
  • Rundeck is not showing docker containers in nodes
  • is it possible to wrap an entire ubuntu 14 os in a docker image
  • Getting NettyTcpClient connection error with Spring WebSocket with amqp broker (rabbitmq) while running in docker
  • "forwarded_port", guest: 5432, host: 5432

    But I’m guessing this is not enough since the Docker container has its own IP ( My thought was that I would put a iptable rule on the box that forwards all requests to the Vagrant box on port 5432 to destination like this:

    iptables -t nat -A PREROUTING -p tcp --dport 5432 -j DNAT --to-destination
    iptables -t nat -A POSTROUTING -j MASQUERADE

    But I still can’t make it work. Thankful for any help!

  • Should I install Docker on Server Having Cpanel already on it?
  • How can I make other machines on my network access my Docker containers (using port mapping)?
  • Can't reach a local website through apache in a docker container (windows & docker toolbox)
  • Is there a way to pre-compress docker images before pushing?
  • How to connect to docker container in local machina [duplicate]
  • Docker docker-compose not picking up relevant cached image
  • One Solution collect form web for “Access PostgreSQL running in Docker container in Vagrant”

    No need to add iptables rule externally. vagrant port forwarding will take care itself.

    Check below points:

    1. check your PostgreSQL port in vagrant instance. if port is bind with particular IP then you have to give IP in Vagrantfile to forward that IP and port. Like

    guest_ip (string) – The guest IP to bind the forwarded port to. If
    this is not set, the port will go to every IP interface. By default,
    this is empty.

    1. Similarly, you can give host IP also

    host_ip (string) – The IP on the host you want to bind the forwarded
    port to. If not specified, it will be bound to every IP. By default,
    this is empty.

    If everything ok and you can connect using command like:

    psql -p 5432 -d db_name -U user -h localhost

    Don’t forget to add -h with hostname or ip. Also see These answer:

    1.unable to connect to forwarded port over ssh

    2.Accessing to a remote PostgreSQL server using port forwarding to another machine

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.