Occasionally, I need to communicate from a podman container to its host, and I keep forgetting how to that. I know I can always google it, but from what had happened earlier today, having a short note to remind myself how this can be easily done is much preferable in my opinion.

To communicate between the container and its host, we can just simply use host.containers.internal or host.docker.internal to refer to the host.

The following is an example of connecting from within the container to a service running on port 6556 on the host.

[kenno@xoa2 ~]$ ss -tlpn | rg 6556
LISTEN 0      4096               *:6556            *:*

[kenno@xoa2 ~]$ podman exec -it checkmk /bin/bash
root@30516e266e85:/# grep internal /etc/hosts
169.254.1.2     host.containers.internal host.docker.internal

root@30516e266e85:/# curl -v host.containers.internal:6556
*   Trying 169.254.1.2:6556...
* Connected to host.containers.internal (169.254.1.2) port 6556 (#0)
> GET / HTTP/1.1
> Host: host.containers.internal:6556
> User-Agent: curl/7.81.0
> Accept: */*
>
* Received HTTP/0.9 when not allowed
* Closing connection 0
curl: (1) Received HTTP/0.9 when not allowed

It can be seen from the above output that the connection from within of the ‘checkmk’ container to the host was successful.

As a bonus, here’s a sample output to a service that doesn’t exist on the host:

root@30516e266e85:/# curl -v host.containers.internal:5555
*   Trying 169.254.1.2:5555...
* connect to 169.254.1.2 port 5555 failed: Connection refused
* Failed to connect to host.containers.internal port 5555 after 1 ms: Connection refused
* Closing connection 0
curl: (7) Failed to connect to host.containers.internal port 5555 after 1 ms: Connection refused

That’s all folks. Sadly, I can’t remember where I leaned this info from, so there’s no links to reference for now.