title: Vagrant + Docker + Postgresql - Cannot connect from host
body: <p>I'm trying to simulate our production setup locally using Vagrant. In production, we use a docker container for our postgresql database, running on centos6.5/redhat (not by choice).</p>
  
  <p>So, locally, I've installed Vagrant, created a machine, got the postgresql docker container up and running on that machine, ensured it's running by connecting from the VM. However I cannot figure out how to connect to postgresql from the host (or from another VM). </p>
  
  <p>Here is my Vagrant file:</p>
  
  <pre><code>VAGRANTFILE_API_VERSION = "2"
  
  Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|  
  
    config.vm.box = "chef/centos-6.5"
  
    config.vm.provision "shell" do |s|
      s.inline = "ps aux | grep 'sshd:' | awk '{print $2}' | xargs kill"
    end
  
    config.vm.define "db" do |db|
      db.vm.synced_folder "../db", "/vagrant/db"
      db.vm.synced_folder "../deploy", "/vagrant/deploy"
      db.vm.hostname = "dbserver"
      db.vm.network :private_network, ip: "192.168.50.4"
      db.vm.network :forwarded_port, guest: 5432, host: 6543 
    end
  
  end
  </code></pre>
  
  <p>Note I'm forwarding the guest port 5432 to the host port 6543.</p>
  
  <p>On the VM, you can see docker running the postgresql container:</p>
  
  <pre><code>[vagrant@dbserver vagrant]$ sudo docker ps
  CONTAINER ID        IMAGE                                     COMMAND             CREATED             STATUS              PORTS                    NAMES
  075f71e9f8de        quay.io/aptible/postgresql:standardized   "run-database.sh"   12 hours ago        Up 12 hours         0.0.0.0:5432-&gt;5432/tcp   hungry_morse  
  </code></pre>
  
  <p>When on the VM, I have to connect using a command like:</p>
  
  <pre><code>psql -h 0.0.0.0 -U &lt;username&gt; -d db
  </code></pre>
  
  <p>From the host, it seems like I should be using:</p>
  
  <pre><code>psql -h 192.168.50.4 -p 6543 -U &lt;username&gt; -d db
  </code></pre>
  
  <p>But that's giving me:</p>
  
  <pre><code>psql: could not connect to server: Connection refused
      Is the server running on host "192.168.50.4" and accepting
      TCP/IP connections on port 6543?
  </code></pre>
  
  <p>Note that this is not specific to postgresql. I have a redis container setup the same way giving the same issues.</p>
  
  <p>I'm not sure if this is an issue with my Vagrant setup, the firewall on Centos, or what. Any ideas on how to make this work?</p>
  
  <h1>Update 1</h1>
  
  <p>The pg_hba.conf file in our docker container looks like so: </p>
  
  <pre><code>local     all   all                        peer
  hostssl   all   all        0.0.0.0/0       md5
  </code></pre>
  
  <p>The postgresql.conf file in our docker container looks like so:</p>
  
  <pre><code>#------------------------------------------------------------------------------
  # FILE LOCATIONS
  #------------------------------------------------------------------------------
  
  data_directory = '/var/lib/postgresql/9.3/main'
  hba_file = '/etc/postgresql/9.3/main/pg_hba.conf'
  ident_file = '/etc/postgresql/9.3/main/pg_ident.conf'
  external_pid_file = '/var/run/postgresql/9.3-main.pid'
  
  #------------------------------------------------------------------------------
  # CONNECTIONS AND AUTHENTICATION
  #------------------------------------------------------------------------------
  
  listen_addresses = '*'
  port = 5432
  max_connections = 250
  unix_socket_directories = '/var/run/postgresql'
  ssl = on
  ssl_ciphers = 'DEFAULT:!LOW:!EXP:!MD5:@STRENGTH'
  ssl_cert_file = '/etc/postgresql/9.3/ssl/server.crt'
  ssl_key_file = '/etc/postgresql/9.3/ssl/server.key'
  
  #------------------------------------------------------------------------------
  # RESOURCE USAGE (except WAL)
  #------------------------------------------------------------------------------
  
  shared_buffers = 128MB
  
  
  #------------------------------------------------------------------------------
  # QUERY TUNING
  #------------------------------------------------------------------------------
  
  log_line_prefix = '%t '
  log_timezone = 'UTC'
  client_min_messages = ERROR
  log_min_messages = FATAL
  log_min_error_statement = FATAL
  
  #------------------------------------------------------------------------------
  # CLIENT CONNECTION DEFAULTS
  #------------------------------------------------------------------------------
  
  datestyle = 'iso, mdy'
  timezone = 'UTC'
  lc_messages = 'C'
  lc_monetary = 'C'
  lc_numeric = 'C'
  lc_time = 'C'
  default_text_search_config = 'pg_catalog.english'
  
  tcp_keepalives_idle = 30
  tcp_keepalives_interval = 30
  </code></pre>
  
  <h1>Update 2</h1>
  
  <p>The VM's iptables rules:</p>
  
  <pre><code>[vagrant@dbserver vagrant]$ sudo iptables -L
  Chain INPUT (policy ACCEPT)
  target     prot opt source               destination         
  ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http 
  ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:postgres 
  
  Chain FORWARD (policy ACCEPT)
  target     prot opt source               destination         
  DOCKER     all  --  anywhere             anywhere            
  ACCEPT     all  --  anywhere             anywhere            ctstate RELATED,ESTABLISHED 
  ACCEPT     all  --  anywhere             anywhere            
  ACCEPT     all  --  anywhere             anywhere            
  
  Chain OUTPUT (policy ACCEPT)
  target     prot opt source               destination         
  
  Chain DOCKER (1 references)
  target     prot opt source               destination         
  ACCEPT     tcp  --  anywhere             172.17.0.3          tcp dpt:postgres 
  </code></pre>