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?
When running Docker natively on Linux, you can access host services using the IP address of the docker0 interface. From inside the container, this will be your default route. This would permit access to any ports on the host from Docker containers.
Use --network="host" in your docker run command, then 127.0. 0.1 in your docker container will point to your docker host.
The Docker bridge driver automatically installs rules in the host machine so that containers on different bridge networks cannot communicate directly with each other. The communication would be established only if the bridge network is provided and the proper permissions on the iptables rules are given.
You can build, test, and deploy your applications inside the container itself. Using the Docker run command to run a container and access its shell. Using the Docker exec command to run commands in an active container. Using the Docker start command and attach a shell to a stopped container.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With