My spring boot app is connecting to postgres and I want to set /etc/hosts like in add-postgres-port-hosts but my container refusing to run when I try to run it using docker run -d -p 8080:8080 springio/flyaway-postgres it fails to run in the background because it tries to find localhost inside the container -
Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections
So how can I run docker exec [container] nc -v -z localhost 5432 in a container that I cannot run in detach mode?
Thank you.
Related
All the questions on SO about this seem to refer to an opposite case of creating a postgres container and connecting it from Mac host. But I am trying to do the opposite, without success. I have localhost running on my Mac host machine, and despite setting port flags, I cannot get code inside my container to talk to my localhost postgres (talks to remote host postgres just fine).
docker run -it -p 5000:5000 -p 5432:5432 yard-stats
Then inside docker:
telnet 0.0.0.0 5432
Trying 0.0.0.0...
telnet: Unable to connect to remote host: Connection refused
or telnet 127.0.0.1 or localhost. Connection is refused.
Edit: I also tried with flag --network="host", which did not change anything except break inbound connections to the container on localhost:5000 as well.
If you are using docker for mac, you can use use host.docker.internal special DNS name which resolves to the internal IP address used by the host.
You can also use --network="host" with your docker run command to run the container in host network. Then the localhost interface inside the container will be same as localhost interface of the host machine when run in host network. So you should be able to use localhost:5432 to connect to postgresql. You can remove -p option as it has no effect when running with --network="host".
docker run -it --network=host yard-stats
I am running a postgresql database on docker, hosting it on my local machine. When I try and connect to it using DBeaver though, I am given the error:
Connection to localhost:5432 refused.
Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Connection refused: connect
Here are the commands I ran in Docker to create my PostgreSQL image:
rrowe#LAPTOP-AFM43BOB MINGW64 /c/Program Files/Docker Toolbox
$ docker run --name RiohRoweDB -e POSTGRES_PASSWORD=Charlie -d -p 5432:5432 postgres
55c78d059fdb2b19bfdd24f579eaf26ef5b00c77ead564ee7d128fd06996789d
rrowe#LAPTOP-AFM43BOB MINGW64 /c/Program Files/Docker Toolbox
$ docker exec -it RiohRoweDB bash
root#55c78d059fdb:/# psql -U postgres
psql (12.0 (Debian 12.0-2.pgdg100+1))
Type "help" for help.
postgres=# CREATE DATABASE postgresqlDB
postgres-# \q
root#55c78d059fdb:/# exit
exit
rrowe#LAPTOP-AFM43BOB MINGW64 /c/Program Files/Docker Toolbox
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55c78d059fdb postgres "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:5432->5432/tcp RiohRoweDB
On the DBeaver side, I add a connection with the following parameters:
Host:localhost
Port:5432
Database:postgresqlDB
User:postgres
Password:Charlie
When I test the connection ("Test Connection" button in Connection Settings)
I get the following error:
Connection to localhost:5432 refused.
Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Connection refused: connect
I was following this tutorial that did not use DBeaver: https://medium.com/better-programming/connect-from-local-machine-to-postgresql-docker-container-f785f00461a7
If you got here from the official Postgres Docker page, don't forget to add the port-forwarding flag -p 5432:5432 to your initialization command like Rioh did above:
docker run --name RiohRoweDB -e POSTGRES_PASSWORD=Charlie -d -p 5432:5432 postgres
Also consider studying this how-to on Medium:
https://medium.com/#wkrzywiec/database-in-a-docker-container-how-to-start-and-whats-it-about-5e3ceea77e50
If you're using Docker Toolbox, programs on the host need to use the IP address from docker-machine ip, usually 192.168.99.100, to reach container processes; localhost won't work. – David Maze
I'm trying to run a docker image of rundeck, using PostGres for the database.
The issue I'm having is mapping my local postgres installation to the docker rundeck image. The postgres port runs on 5432, and have confirmed using netstat that the port is open and listening. The port for rundeck needs to run on 4440.
I have tried the following command:
docker run -p 127.0.0.1:4440:4440 -e RUNDECK_DATABASE_URL=jdbc:postgresql://localhost/rundeck -e RUNDECK_DATABASE_DRIVER=org.postgresql.Driver -e RUNDECK_DATABASE_USERNAME=xxx -e RUNDECK_DATABASE_PASSWORD=xxx --name test-rundeck -t rundeck/rundeck:3.0.19
But it fails with an error: Connection to 127.0.0.1:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP
I'm guessing it's because the internal 5432 port isnt mapped to the docker container port 5432?
I tried mapping the ports with -p 127.0.0.1:5432:5432 but that fails with the error:
Error starting userland proxy: listen tcp 127.0.0.1:5432: bind: address already in use
At this point I might just resort to running both PostGres and Rundeck as docker images, but I would rather like to resolve this problem.
Any ideas on how to map a local PostGres to a docker ran Rundeck?
Found the answer.
Had to edit the pg_hba.conf file to allow the docker0 ip address through.
host all all 172.23.0.0/16 md5
I'm working out how to run openproject with docker. I'm working through openproject/docker.
I've got the docker image running with an external postgres directory.
I'm now working out how to connect to an existing running instance of postgresql.
The command line I'm using looks ok according to the official documentation.
EDIT. Added in the missing -p.
docker run -p 8082:80 -p 5432:5432 --name openproject_dev -e SECRET_KEY_BASE=secret -e DATABASE_URL=postgresql://openproject:openproject-dev-
password#localhost:5432/openproject_dev \
-v /Users/admin/var/lib/openproject/logs:/var/log/supervisor \
-v /Users/admin/var/lib/openproject/static:/var/db/openproject openproject/community:5.0
I've ommitted the -d [deamon] flag so I can see any errors.
When the docker container is being created I get
-----> You're using an external database. Not initializing a local database cluster.
/usr/src/app /usr/src/app
Starting memcached: memcached.
Which I expect.
Then I get an error about connecting to the postgresql server which I don't expect.
...
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
/usr/local/bundle/gems/activerecord-
4.2.7.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize'
I'm guessing the script initialising the container is expecting postgres to be running and it's not. How would you make the docker container port forward requests to 5432 to the host machine on the command line? ... the opposite of docker run -p 5432:5432 ... which exposes 5432 from the docker container to the host.
-e DATABASE_URL=postgresql://openproject:openproject-dev-
password#localhost:5432/openproject_dev
When you add this url, the container expects postgres to be running on localhost, i.e. in itself.
If you are running postgres on your host machine, you can let the container share the network stack with the host by passing --network host in the run command. In that case, localhost will refer to the host machine where postgres is running.
Your approach is wrong currently
docker run -p 8082:80 -p 5432:5432 --name openproject_dev -e SECRET_KEY_BASE=secret -e DATABASE_URL=postgresql://openproject:openproject-dev-password#localhost:5432/openproject_dev \
-v /Users/admin/var/lib/openproject/logs:/var/log/supervisor \
-v /Users/admin/var/lib/openproject/static:/var/db/openproject openproject/community:5.0
when you used -p 5432:5432 it means that you expect something to run inside docker on that port and you want your host machine 5432 port to map to 5432 inside docker.
Next if you were able to run that command, even though nothing is listening inside container, then that would imply that the port on your host is available. This means postgress is not listening on 5432 on host also. It may be listening on a socket. You should try to execute below command on host
psql -h 127.0.0.1
If you are not able to connect on host using this that means the postgres db is bind to a socket file and not to a IP. Now you have few options that you can exercise
Mount the socket
docker run -p 8082:80 -p 5432:5432 --name openproject_dev -e SECRET_KEY_BASE=secret -e DATABASE_URL=postgresql://openproject:openproject-dev-password#localhost:5432/openproject_dev \
-v /Users/admin/var/lib/openproject/logs:/var/log/supervisor \
-v : \
-v /Users/admin/var/lib/openproject/static:/var/db/openproject openproject/community:5.0
Bind on 0.0.0.0 on host
If you don't want to mount volume then you should change the bind address of psql to 0.0.0.0 and then change your database url to -e DATABASE_URL=postgresql://openproject:openproject-dev-password#<YOURMACHINEIP>:5432/openproject_dev
Run on host network
docker run --net host --name openproject_dev -e SECRET_KEY_BASE=secret -e DATABASE_URL=postgresql://openproject:openproject-dev-password#localhost:5432/openproject_dev \
-v /Users/admin/var/lib/openproject/logs:/var/log/supervisor \
-v : \
-v /Users/admin/var/lib/openproject/static:/var/db/openproject openproject/community:5.0
Given the following docker-compose.test.yml file:
version: '2'
services:
sut:
build: .
command: nosetests
environment:
- DJANGO_SETTINGS_MODULE=test.settings
links:
- db
db:
image: postgres
expose:
- 5432
Building the sut container
docker-compose -f docker-compose.test.yml build sut
Running the container:
thomas#linuxclientlobnek01:~/github/djlobnek$ docker-compose -f docker-compose.test.yml run sut /bin/bash
root#6694ec7148ac:/djlobnek# psql -h localhost -U postgres
psql: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
**could not connect to server: Connection refused**
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
It is possible that your application is attempting to connect to the database before the database is ready to accept connections. If this is the case, then implementing some sort of wait-for-the-database loop into the sut container can resolve the problem. I have in the past used something like:
while ! psql -h db -U postgres postgres -c 'select 1'; do
echo "waiting for database"
sleep 1
done
This is unnecessary if your application knows how to retry unsuccessful database connections.
Before you try this, it's a good idea to verify that the postgres container is actually functioning (e.g., by entering the sut container with docker exec and attempting to manually connect using psql).
Update
Trying to connect to localhost from inside the sut container won't work (postgres isn't run inside that container). You would need to use the hostname db, or you would need to the ip address of the db container. E.g:
postgres -h db -U postgres postgres
You could use localhost as the hostname if you docker exec into the db container itself.