Quarkus native image: postgresql connection refused - postgresql

I created a quarkus application with access to a postgresql database on localhost. After creating the native image I try to run the application with:
docker run -i --rm -p 8080:8080 quarkusimage
if I try a method with postman I have this result:
addEmployee
Hibernate:
select
nextval ('quarkus.hibernate_sequence')
2020-06-18 20: 43: 38,214 WARN [io.agr.pool] (Agroal_20041415421) Datasource '': Connection to localhost: 5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP / IP connections.
if i try to run the application with
docker run --net = "host" -i --rm -p 8080:8080 quarkusimage
the application accesses the database on localhost, but if I try a postman call it can't reach the service.
What am I doing wrong?
Thank you

This has to do with docker networking. When an application is run inside docker, then localhost is not the hosts localhost unless you use --net = "host".
Aside from using --net = "host", you can could use the IP of host instead of localhost in the URL.
As for the podman issue you mention, that sounds like a podman bug or limitation - I propose you consult the podman documentation and / or open an issue in the podman issue tracker.

Related

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?

SSH to docker container that refuse to start

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.

Access local postgres database from podman container during build

I have a Spring Boot application that runs inside a container and needs to connect to a local Postgresql database.
Now it fails at build time, as that is the moment when it tries to configure Spring beans and to connect to database.
I have configured it like following:
spring
datasource:
initialization-mode: always
platform: postgres
url: jdbc:postgresql://192.168.122.1:5432/academy
username: myuser
password: mypassword
but it fails to connect.
How shall I configure Dockerfile/connection string?
I think you have at least two alternatives
Alternative 1: Connecting via Unix domain socket
If you could have Postgres listen on a Unix domain socket, you could then pass in that socket to the container with a bind-mount. Use podman run with one of the command-line arguments --volume or --mount.
Maybe something like:
--mount type=bind,src=/path/to/socket/on/host,target=/path/to/socket/in/container
If your system has SELINUX enabled, you would need to add the option Z
--volume /path/to/socket/on/host:/path/to/socket/in/container:Z
Alternative 2: Connecting via TCP socket
I think you could add the option
--network slirp4netns:allow_host_loopback=true
to the podman run command and connect to the IP address 10.0.2.2.
Quote "Allow the slirp4netns to reach the host loopback IP (10.0.2.2, which is added to /etc/hosts as host.containers.internal for your convenience)" from the podman run man page.
See also slirp4netns.1.md.
(The IP address 10.0.2.2 is a default value hard coded in the source code of slirp4netns).
Here is an example of a container that connects to a web server running on localhost:
esjolund#laptop:~$ curl -sS http://localhost:8000/file.txt
hello
esjolund#laptop:~$ cat /etc/issue
Ubuntu 20.04.2 LTS \n \l
esjolund#laptop:~$ podman --version
podman version 3.0.1
esjolund#laptop:~$ podman run --rm docker.io/library/fedora cur-l -sS http://10.0.2.2:8000/file.txt
curl: (7) Failed to connect to 10.0.2.2 port 8000: Network is unreachable
esjolund#laptop:~$ podman run --rm --network slirp4netns:allow_host_loopback=true docker.io/library/fedora curl -sS http://10.0.2.2:8000/file.txt
hello
esjolund#laptop:~$

Docker postgres connection from *inside* docker container to host machine's postgres localhost instance on a Mac

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

mongodb in docker ERR_CONNECTION_TIMED_OUT

Please help me with this. I tried the following steps, but I am not able to connect to MongoDB.
RUN: docker run -p 27017:27017 --name my-mongo -d mongo:latest
RUN: docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-mongo
The output of the above command is an IP-Address. Let the IP-Address is 172.17.0.2.
Open in browser this link: http://172.17.0.2:27017/
If the following output is displayed in a browser then everything is fine: It looks like you are trying to access MongoDB over HTTP on the native driver port. I am stuck here.
Thanks
The IP Address you are trying to access is the container's private ip that is only accessible from within the docker network.
Using the parameter -p that you're specifying on the docker run you're telling docker to map a local port on your host to the port specified on the container, in this case local port 27017 to container port 27017 and after that you can access it with localhost: http://localhost:27017
Read more about this here: https://docs.docker.com/config/containers/container-networking/