3

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?

1 Answer 1

4

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:~$ 
Sign up to request clarification or add additional context in comments.

6 Comments

Where did magic number 10.0.2.2 come from?
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 podman-run.1.md See also slirp4netns.1.md
Still doesn't work, on podman version 3.4.2. Host is MacOS.
Turned out, at least in my case, it was a bug - I have created it and it's going to be fixed in the next 3.4 release, see github.com/containers/podman/issues/12507
0 VirtualBox: The default address of the virtual DHCP server used in the NAT mode is 10.0.2.2 (this is also the IP address of the default gateway for a VM) The answer assumes that you are using VirtualBox as the virtualization engine.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.