Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing host from inside container

Tags:

podman

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?

like image 832
Thomas Glaser Avatar asked Nov 03 '19 09:11

Thomas Glaser


People also ask

Can a Docker container access host?

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.

How can you connect from the inside of your container to the localhost of your host where the container runs?

Use --network="host" in your docker run command, then 127.0. 0.1 in your docker container will point to your docker host.

Can a container communicate with the 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.

How do you gain access to a shell inside a running container?

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.


1 Answers

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.

like image 137
larsks Avatar answered Oct 03 '22 20:10

larsks