Connecting local psql db with web app inside the docker container - postgresql

I am new to docker and know basic things. I have postgresql installed on my local Ubuntu server and i want to connect it with the web application which is inside of the docker container. What setting should I apply to allow that?

You can use your server's Public IP address for this instead of localhost in your docker container.
Make sure that your firewall allows the port 5432
When you run an application on your local computer, the application can easily access all the services on your PC (i.e. localhost). When your application runs inside a Docker container it became a completely isolated environment from that machine (i.e. No access localhost and other system services unless you expose it directly) and can only use Host OS services directly served to Docker-Engine by Host OS. This is why you have to route through the Internet to access your service. Postgres in your case.

Related

rootless docker - communication between docker containers of different users

When using rootless Docker, Docker instances are running per user and not system-wide. How can I enable communication between Docker containers that have been started by different users?
Scenario is the following. The systems is setup with rootless Docker because multiple people are sharing the same workstation. Rootless Docker is used, so that users building their Docker images and running their containers will not interfer each other.
We also like to run a mongodb in a Docker container, that should be accessible for all users. How can this be done without system-wide Docker? We will be connected to the workstation via ssh and do not want to access the mongodb Docker container from outside the workstation (not being connected via ssh).
Using system-wide Docker is not an option.
Any suggestions how this can be done with root-less Docker?
Info:
Previously with the system-wide Docker we created a network (bridge) and used docker run --network=some_network_for_mongodb -d -p 27017:27017 mongodb:5.0.2
When using rootless Docker, Docker instances are running per user and not system-wide. How can I enable communication between Docker containers that have been started by different users?
The only way you'll be able to enable communication between containers started by different users is by publishing the necessary ports on the host. For example, if you want to create a MongoDB instance that's generally accessible, you could run:
docker run -p 27019:27019 ...
This binds port 27019 in the mongo container to port 27019 on the host. Any other container on the system would be able to connect to this service using an ip address of a host interface.
Of course, this will also open the port to outside connections. There are several ways of dealing with this:
Block the port in your firewall configuration.
Bind the port to a specific host interface on which it won't be available for outside access. For example, if you have a docker bridge docker0 at 172.17.0.1, you could run:
docker run -p 172.17.0.1:27019:27019 ...
This would only publish the port on 172.17.0.1. Other containers could access the service at 172.17.0.1:27019, but it wouldn't be available for outside access.

Can remote Docker be used locally?

For examample, I have a Postgres docker container and I connect to it with psql -H localhost:5432.
If I "move" this container to the remote machine and add a Docker context with docker context create remote --docker 'host=ssh://<remote machine>' then after docker use context remote I can see that it is up and running remotely. I can manage that remote container without ssh-ing remote machine, everything consider Docker is fine.
However, I don't know how to connect to this remote container with psql, without exposing a port to the internet on remote host.
I thought that when I use Docker context all connections to my local Docker are transfered via SSH-tunnel to the remote Docker, but it seems to be not true.
So, is remote host feature is only for remote Docker management and to actually interact with services inside them I should open ports, configure firewall, etc (and that kills the point of moving everything on remote machine) or I just do something wrong and Docker supposed to work as I described above?

Is it possible set ip in docker container from same net as host?

I have twenty five containers in my docker-compose.yml file.
And I have external server with database PostgreSQL that run on host (not in docker container).
All containers can send request to database.
I would like to get IP addresses of container that send request to database.
When I try get info from DB, I can see only IP of host with docker containers.
One of the ideas it is add server lan interface to custom docker bridge net. Then I will can setup my lan network IP-addresses in each container.
Unfortunately, I can't run Postgres also in docker and configure docker-swarm or kubernetes. One of customer requirement it is run Postgres locally in a separate host.

Spring Boot Docker Container can't connect to postgresql hosted on Docker host

when I use maven to build my spring boot app the application needs to have a connection to my postgresql database in the application.properties. It only can connect when I use
spring.datasource.url=jdbc:postgresql://localhost:5432/springbootdb
but when I containerize the buileded jar file, my application can't connect to my postgres database hosted outside the Container.
what is the solution for this problem?
I am still a Beginner
From the 18.03 docs:
I want to connect from a container to a service on the host
The host has a changing IP address (or none if you have no network
access). From 18.03 onwards our recommendation is to connect to the
special DNS name host.docker.internal, which resolves to the internal
IP address used by the host.
The gateway is also reachable as gateway.docker.internal.
example:
spring.datasource.url=jdbc:postgresql://host.docker.internal:5432/springbootdb
Where you hosted your PostgreSQL database?
Replace localhost with actual machine IP address (PostgreSQL database machine address)
spring.datasource.url=jdbc:postgresql:<ip_address>:5432/springbootdb

db connection path to connect application that is running in docker

I am running nodejs application in docker. In that application I am trying to connect my system database. But It is not working
In my environment file:
**MONGODB_URI=mongodb://192.168.1.174:27017/sampleDB**
SESSION_SECRET=sample
App_PORT = 8088
But I am getting error and unable to access the db.
My application is running on docker machine ip 192.168.99.100:8088
Here, I attached my docker running command statement:
How to connect my system db into that application
The IP depends how the containers are run. If using docker-compose, it creates a network for you in which containers are accessible to themselves using service name (eg. db should you use it). If not, and you did not specify any network, the default bridge network is used (called docker0 on your docker machine).
Since you are running containers separately (using docker run), you have to either give specific IP address to the container (you can get one from inside the container using docker exec container_name ip a) or connect to it via the gateway (your docker machine). However, in order to do that, the database port has to be exposed (eg. 27017:27017 when running).
I recommend you start using docker-compose from now on, many things will get easier when running a stack of linked containers.