Docker container not able to access port 5432 - postgresql

I have a spring application which is trying to connect postgres db (jdbc:postgresql://xxxxxxx:5432/My_DB). It connects fine when I run the jar using "java -jar app.jar" command.But when i run inside the docker container it fails to connect. Below is the command , I used to run.
docker run -p 5432:5432 my_image:latest
It looks like 5432 is not open inside the container seems. I came across a similar post for this, but didnt give any solutions.
Docker container for Postgres 9.1 not exposing port 5432 to host
Any thoughts on this?
Thanks

You have to specifically user Expose 5432 in your docker file. Otherwise it is not allowed to expose it via a container.

Related

How to expose locally dockerized postgres service to the net

Context:
I'm building a full stack project and have decided to deploy my services on docker, I have 3 services at the moment, database-service, frontend-service, and backend-service, in addition to postgres and pgadmin.
I have set up a docker-compose file and have ran all the services.
But, at some point, I have added Prisma to my database-service and some Prisma-Docker-Apple Silicon problems have started to prevent me from build and running my database-service in the localhost.
What I decided is to build my docker image for database-service on the cloud, at fly.io.
The Issue:
As I said before, I have decided to work with Prisma on my database-service, so in my prisma.yml, I should provide my postgres link. I have tried to expose my 5432 port to the internet using ngrok, but it didn't work.
If you have encountered a similar issue, please let me know how you could resolve this conflict. thank you!
You should use ngrok tcp 5432, and not ngrok http 5432.
I've started a PosgreSQL using docker run --name so75297011 -e POSTGRES_PASSWORD=so75297011 -d -p 5432:5432 postgres:latest. Don't forget about the ports when running PostgreSQL in Docker!
And exposed it via ngrok: ngrok tcp 5432. Got a tunnel: tcp://0.tcp.eu.ngrok.io:10821 -> localhost:5432.
Here is the result, connection successful 👇

Postgres container does not accept connections when run host mode

I am trying to run Postgres in a container.
When I start the container using the following command wherein I map the port 5432 of my machine with that of the container, the Postgres accepts connections from another process and everything works as intended.
docker run --name postgres --rm -e POSTGRES_HOST_AUTH_METHOD=trust -p 5432:5432 -d postgres
netstat-ing the port also works well i.e
nc -z localhost 5432
Connection to localhost port 5432 [tcp/postgresql] succeeded!
Now if I use the host mode to run the postgres container, it stops accepting connections. Basically the following doesn't work:
docker run --name postgres --rm -e POSTGRES_HOST_AUTH_METHOD=trust --net=host -d postgres
I saw a similar question on StackOverflow but it doesn't explain why things don't work. Here is the link to that question:
Connection Error with docker postgres using network=host
Any ideas why the second command doesn't work are appreciated. Thank you.
I wasn't aware of the fact that host networking is not supported on mac.
A snippet from https://docs.docker.com/network/host/
The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.
Another related SO question:
How to tell docker on mac to use host network for all purpose?

Access Docker postgres container from another container

I am trying to make a portable solution to having my application container connect to a postgres container. By 'portable' I mean that I can give the user two docker run commands, one for each container, and they will always work together.
I have a postgres docker container running on my local PC, and I run it like this,
docker run -p 5432:5432 -v $(pwd)/datadir:/var/lib/postgresql/data -e POSTGRES_PASSWORD=qwerty -d postgres:11
and I am able to access it from a python flask app, using the address 127.0.0.1:5432.
I put the python app in a docker container as well, and I am having trouble connecting to the postgres container.
Address 127.0.0.1:5432 does not work.
Address 172.17.0.2:5432 DOES work (172.17.0.2 is the address of the docker container running postgres). However I consider this not portable because I can't guarantee what the postgres container IP will be.
I am aware of the --add-host flag, but it is also asking for the host-ip, which I want to be the localhost (127.0.0.1). Despite several hits on --add-host I wasn't able to get that to work so that the final docker run commands can be the same on any computer they are run on.
I also tried this: docker container port accessed from another container
My situation is that the postgres and myApp will be containers running on the same computer. I would prefer a non-Docker compose solution.
The comment from Truong had me try that approach (again) and I got it working. Here are my steps in case it helps out another. The crux of the problem was needing one container to address another container in a way that was static (didn't change). Using user defined network was the answer, because you can name a container, and thus reference that container IP by that name.
My steps,
docker network create mynet
docker run --net mynet --name mydb -v $(pwd)/datadir:/var/lib/postgresql/data -e POSTGRES_PASSWORD=qwerty -d postgres:11
Now the IP address of the postgres database is mydb, and all the ports of this container are exposed to any other container running in this network.
Now add the front end app,
docker run --net mynet -ti -p 80:80 -v mydockerhubaccount/myapp

Connect to Windows Postgres from Debian Docker container

I am running Postgres on a Windows 10 computer, and I want to connect to it from a Docker container. I've followed instructions from many sources and things should be working, but they're not.
Command line used to create Docker container:
docker run --rm -d --network=host --name mycontainer myimage
In postgresql.conf:
listen_addresses = '*'
In pg_hba.conf:
host all all 172.17.0.0/16 trust
In the bash shell of my container, I run:
psql -h 127.0.0.1
and I get the error:
psql: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?
Needless to say, Postgres is definitely running on my computer and I am able to query it from local applications. What am I missing?
THIS WON'T WORK FOR DOCKER v18.03 AND ONWARDS
The answer is already there - From inside of a Docker container, how do I connect to the localhost of the machine?
This question is related to a mysql setup, but it should work for your case too.
FOR DOCKER v18.03 ONWARDS
Use host.docker.internal to refer to the host machine.
https://docs.docker.com/docker-for-windows/networking/#i-cannot-ping-my-containers
As you've discovered, --network-host doesn't work with Docker for Windows or Docker for Mac. It only works on Linux hosts.
One option for this scenario might be to host PostgreSql in a container, also. If you deploy them with a docker-compose file, you should be able to have two separate Docker containers (one for the database and one for your service) that are networked together. By default, docker-compose will expose containers to others in the same compose file using the container name as its DNS name.
You could also consider including the database in the same container as your service, but I think the docker-compose solution is better for several reasons:
It adheres to the best practice of each container having only a single process and single responsibility.
It means that you can easily change and re-deploy your service without having to recreate the database container.
Configure the connection inside your docker container with the real ip-address of your host or as workaround with a dns name

connection refused postgres docker

I made a small application with spring-boot, spring jpa data, that connects to a dockerized postgres instace and it works pretty fine, even if I try to connect via
'psql' to the dockerized postgres instace it works well. The porblem is when I try to dockerize an image's instance of my spring-boot application and I try to link it with the dockerized postegres instance.
The docker command I use is this
docker run -it --link mypgcontainerwithpwd:postgres --name postgresclient1 sprinbootjpa
As a I already mentioned the container mypgcontainerwithpwd is running and reachable either with a local application either via psql
psql -p 5555 postgres postgres
in the jar I'm going to execute the application.properties file looks like this
spring.datasource.url=jdbc:postgresql://localhost:5555/postgres
spring.datasource.username=postgres
spring.datasource.password=password
spring.jpa.generate-ddl=true
During the starting phase an exception is raised up that prints: connection refused localhost-> 5555
The dockerfile that builds the instace looks like this
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD ./SpringJPA-PostgreSQ-0.0.1.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
I'm new to docker and I didn't find anything to fix the issue, I'm running docker on windows 10 with unix containers.
Thanks to all.
In your property file you are stating that postgres is running in the same container as your Spring Boot application (localhost) which is not true as it is running in a different container.
Replace you property by this:
spring.datasource.url=jdbc:postgresql://postgres:5555/postgres
You could also point to the docker bridge ip which usually is 172.17.0.1.
Change -
spring.datasource.url=jdbc:postgresql://localhost:5555/postgres
TO
spring.datasource.url=jdbc:postgresql://postgres:5555/postgres
Since you started the client container with link --link mypgcontainerwithpwd:postgres which means your client will be able to reach your mypgcontainerwithpwd container using alias postgres. localhost means your client container itself & not mypgcontainerwithpwd.
This works, but I just want to emphasize Vivek's point that "postgres" comes from the container name and not the userID or the database type. I am using Docker Compose, so this name comes from my docker-compose.yml file.