Vagrant + Docker + Postgresql - Cannot connect from host -
i'm trying simulate our production setup locally using vagrant. in production, use docker container our postgresql database, running on centos6.5/redhat (not choice).
so, locally, i've installed vagrant, created machine, got postgresql docker container , running on machine, ensured it's running connecting vm. cannot figure out how connect postgresql host (or vm).
here vagrant file:
vagrantfile_api_version = "2" vagrant.configure(vagrantfile_api_version) |config| config.vm.box = "chef/centos-6.5" config.vm.provision "shell" |s| s.inline = "ps aux | grep 'sshd:' | awk '{print $2}' | xargs kill" end config.vm.define "db" |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
note i'm forwarding guest port 5432 host port 6543.
on vm, can see docker running postgresql container:
[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 12 hours 0.0.0.0:5432->5432/tcp hungry_morse
when on vm, have connect using command like:
psql -h 0.0.0.0 -u <username> -d db
from host, seems should using:
psql -h 192.168.50.4 -p 6543 -u <username> -d db
but that's giving me:
psql: not connect server: connection refused server running on host "192.168.50.4" , accepting tcp/ip connections on port 6543?
note not specific postgresql. have redis container setup same way giving same issues.
i'm not sure if issue vagrant setup, firewall on centos, or what. ideas on how make work?
update 1
the pg_hba.conf file in our docker container looks so:
local peer hostssl 0.0.0.0/0 md5
the postgresql.conf file in our docker container looks so:
#------------------------------------------------------------------------------ # 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 , 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
update 2
the vm's iptables rules:
[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 -- anywhere anywhere accept -- anywhere anywhere ctstate related,established accept -- anywhere anywhere accept -- 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
it looks you're misunderstanding how access services on vagrant instance. can either connect vm host of service , it's service port or can forward traffic local port vm's port using port forwarding.
from host, seems should using:
psql -h 192.168.50.4 -p 6543 -u <username> -d db
with vagrant, if forward port on access if on localhost.
in case, should use either:
psql -h 192.168.50.4 -p 5432 -u <username> -d db
or
psql -h 127.0.0.1 -p 6543 -u <username> -d db
instead of <vm ip>:<forwarded port>
.
on top of need make sure postgres instance configured allow remote access asmpostgres, out of box, accepts connections localhost.
to open remote access must first modify pg_hba.conf , listen_address
postresql.conf.
the pg_hba.conf needs have line allowing vagrant host connect it. typically seen vm 10.0.2.2 line need add like:
# allow connections vagrant host on 10.0.2.2 datababases users using md5 hashed password host 10.0.2.2/32 md5
your postgresql.conf change simple need replace:
listen_addresses='localhost'
with:
listen_addresses='*'
with typical vm i'd suggest using provisioner make changes docker should instead set via dockerfile. helpfully, docker provides useful example of in documentation.
Comments
Post a Comment