How do I connect the Postgres database running on the local machine to the docker container - postgresql

I have a Flask application running on the container and Postgres database on the local machine and Now, I want to connect the Postgres DB (listening on 5432) to the docker container.
Can any one suggest the best way to do it ? (I am new to docker containers)
Thanks in advance.

You'll likely want to Dockerize your Postgres database in its own container and use a persistent volume to store your data locally.
Tutorial on how to Dockerize your Postgres database: https://docs.docker.com/engine/examples/postgresql_service/
Documentation on Docker volumes: https://docs.docker.com/engine/tutorials/dockervolumes/
Postgres image on Docker Hub: https://hub.docker.com/_/postgres/

You can use the IP address of your local machine to connect to the postgres from your Docker container. localhost will not be resolvable in certain machines.
You may have some interest in this post. From inside of a Docker container, how do I connect to the localhost of the machine?

Related

How can i see PostgreSQL UI in pgAdmin if the db is in Docker?

I have installed one open-source project from docker and that project has a database in PostgreSQL. Now I want to see the database from pgAdmin 4 and when I am trying to connect with "host.docker.internal" I am getting an error see image below.
Simply do mapping port for your PostgreSQL (which is running inside Docker). and from your pgAdmin4 you'll connect using localhost:<mapped_port>
If you're using docker-compose
services:
postgre:
ports:
- "8080:5432"
If you're using docker-cli
docker run postgre -p 8080:5432
Then from pgAdmin4 connect to your database using localhost:8080
I suspect this is a networking issue.
If port mapping does not work as suggested in other answers, try specifying the network as host for your container
If you are using docker run
--net=host
Or use the following on docker compose
network_mode: 'host'

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.

How do you register a Postgres container host on docker-compose up?

I have an app (Flask/Pony ORM) container that must register the host for a postgres db container on start up. How can I get the host's (postgres container) location?
On my local set up I have "localhost" but this does not work in the app container.
I tried "172.17.0.2" which was what the pg containers IP when I was not using docker-compose.
But both of these return an error.
Is there a way to specify a host location for a particular container in the docker-compose file?
You must link your two containers in your docker-compose file. Then you will be able to access to your database using the service name of the container.
Example
links:
- postgres:postgres
Link your docker-compose.yml if you need more help.

Equivalent of using a ssh tunnel

Using virtual hosts rather than deployed Docker container it was a normal work process for me to create ssh tunnels in order to access delimited machines from my local box. For instance connect with my psql client to a Postgres instance which I could only reach from a bastion box.
With Docker everything is boxes away even more. Is there an equivalent for doing the same but with Docker? Tunnel through the Docker instance to the RDS instance?
You use the docker CLI to connect to a running container. For instance...
To log into a db running in a container you can use (from your local machine)
docker exec -it mypsqlcontainer psql -U username dbname
I personally almost never have to ssh into the host. Everything can be done through the docker CLI.
You can make ssh-tunnel to the docker host. DB port must be accessible from docker host (i.e. using "-p" docker run option).
If you prefer not publishing DB port you can create jumpbox container with ssh server, publish port 22 on this container and user container linking to link jumpbox container with DB container.