7

What I am trying to accomplish is to connect to a database installed on the host system. Now there is a similar question already for docker, but I could not get that to work with Podman, I imagine because networking works a bit differently here.

My solution so far has been to use --add-host=dbhost:$(ip route show dev cni-podman0 | cut -d\ -f7), but I am not certain that's a good idea and it's not going to work when a different network is used.

What is the best approach to accomplish this? Is there perhaps a default hostname for the container host already defined?

Thomas Glaser
  • 1,202
  • 17
  • 20

1 Answers1

4

The solution with podman is identical to that described in the answer to which you provided a link: the default route visible inside the container can be used to connect to host services (assuming they are listening on all addresses or are explicitly bound to the podman bridge).

For example, if I have a webserver running on port 8080 on my host...

darkhttpd . --port 8080

I can start a container:

$ sudo podman run -it --rm alpine sh

And inside that container if I get the address of the default gateway:

/ # ip route
default via 10.88.0.1 dev eth0
10.88.0.0/16 dev eth0 scope link  src 10.88.0.42

I can connect to the webserver on that address:

/ # wget -O- http://10.88.0.1:8080/hello.txt
Connecting to 10.88.0.1:8080 (10.88.0.1:8080)
Hello world
-                    100% |***************************************|    12  0:00:00 ETA

The only caveat -- which is also true for Docker -- is that your host firewall must be configured such that it does not block inbound connections from your containers.

larsks
  • 228,688
  • 37
  • 348
  • 338
  • 2
    Thanks, but I was looking for a more generic solution - in this case my container would become dependent on the host, but it would be nice to have flexibility where I can just access a hostname and it could be either the host, another server, or a container. – Thomas Glaser Nov 15 '19 at 13:18