Podman containers on Windows cannot connect to the host via localhost
Issue Description
Podman containers on Windows cannot connect to the host via localhost.
Steps to reproduce the issue
Invoke-WebRequest -Uri http://localhost:5079 | Select-Object StatusCode
# StatusCode
# ----------
# 200
docker run --rm --network host curlimages/curl -I -X GET http://localhost:5079
# % Total % Received % Xferd Average Speed Time Time Time Current
# Dload Upload Total Spent Left Speed
# 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0HTTP/1.1 200 OK
# 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
# Content-Type: application/json; charset=utf-8
# Date: Sat, 23 Nov 2024 13:31:55 GMT
# Server: Kestrel
# Transfer-Encoding: chunked
podman run --rm --network host curlimages/curl -I -X GET http://localhost:5079
# % Total % Received % Xferd Average Speed Time Time Time Current
# Dload Upload Total Spent Left Speed
# 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
# curl: (7) Failed to connect to localhost port 5079 after 0 ms: Could not connect to server
Describe the results you received
Podman container cannot connect to a port exposed on the host via localhost.
Describe the results you expected
Podman container can connect to a port exposed on the host via localhost.
podman info output
host:
arch: amd64
buildahVersion: 1.38.0
cgroupControllers: []
cgroupManager: cgroupfs
cgroupVersion: v1
conmon:
package: conmon-2.1.12-2.fc40.x86_64
path: /usr/bin/conmon
version: 'conmon version 2.1.12, commit: '
cpuUtilization:
idlePercent: 99.8
systemPercent: 0.1
userPercent: 0.1
cpus: 20
databaseBackend: sqlite
distribution:
distribution: fedora
variant: container
version: "40"
eventLogger: journald
freeLocks: 1991
hostname: MyPC
idMappings:
gidmap:
- container_id: 0
host_id: 1000
size: 1
- container_id: 1
host_id: 524288
size: 65536
uidmap:
- container_id: 0
host_id: 1000
size: 1
- container_id: 1
host_id: 524288
size: 65536
kernel: 5.15.167.4-microsoft-standard-WSL2
linkmode: dynamic
logDriver: journald
memFree: 15142686720
memTotal: 16626593792
networkBackend: netavark
networkBackendInfo:
backend: netavark
dns:
package: aardvark-dns-1.12.2-2.fc40.x86_64
path: /usr/libexec/podman/aardvark-dns
version: aardvark-dns 1.12.2
package: netavark-1.12.2-1.fc40.x86_64
path: /usr/libexec/podman/netavark
version: netavark 1.12.2
ociRuntime:
name: crun
package: crun-1.18.2-1.fc40.x86_64
path: /usr/bin/crun
version: |-
crun version 1.18.2
commit: 00ab38af875ddd0d1a8226addda52e1de18339b5
rundir: /run/user/1000/crun
spec: 1.0.0
+SYSTEMD +SELINUX +APPARMOR +CAP +SECCOMP +EBPF +CRIU +LIBKRUN +WASM:wasmedge +YAJL
os: linux
pasta:
executable: /usr/bin/pasta
package: passt-0^20241030.gee7d0b6-1.fc40.x86_64
version: |
pasta 0^20241030.gee7d0b6-1.fc40.x86_64
Copyright Red Hat
GNU General Public License, version 2 or later
<https://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
remoteSocket:
exists: true
path: unix:///run/user/1000/podman/podman.sock
rootlessNetworkCmd: pasta
security:
apparmorEnabled: false
capabilities: CAP_CHOWN,CAP_DAC_OVERRIDE,CAP_FOWNER,CAP_FSETID,CAP_KILL,CAP_NET_BIND_SERVICE,CAP_SETFCAP,CAP_SETGID,CAP_SETPCAP,CAP_SETUID,CAP_SYS_CHROOT
rootless: true
seccompEnabled: true
seccompProfilePath: /usr/share/containers/seccomp.json
selinuxEnabled: false
serviceIsRemote: true
slirp4netns:
executable: ""
package: ""
version: ""
swapFree: 4294967296
swapTotal: 4294967296
uptime: 0h 30m 26.00s
variant: ""
plugins:
authorization: null
log:
- k8s-file
- none
- passthrough
- journald
network:
- bridge
- macvlan
- ipvlan
volume:
- local
registries:
search:
- docker.io
store:
configFile: /home/user/.config/containers/storage.conf
containerStore:
number: 45
paused: 0
running: 0
stopped: 45
graphDriverName: overlay
graphOptions: {}
graphRoot: /home/user/.local/share/containers/storage
graphRootAllocated: 1081101176832
graphRootUsed: 9572601856
graphStatus:
Backing Filesystem: extfs
Native Overlay Diff: "true"
Supports d_type: "true"
Supports shifting: "false"
Supports volatile: "true"
Using metacopy: "false"
imageCopyTmpDir: /var/tmp
imageStore:
number: 21
runRoot: /run/user/1000/containers
transientStore: false
volumePath: /home/user/.local/share/containers/storage/volumes
version:
APIVersion: 5.3.0
Built: 1731456000
BuiltTime: Wed Nov 13 01:00:00 2024
GitCommit: ""
GoVersion: go1.22.7
Os: linux
OsArch: linux/amd64
Version: 5.3.0
Version: 5.3.0
Podman in a container
No
Privileged Or Rootless
Rootless
Upstream Latest Release
No
Additional environment details
podman version
# Client: Podman Engine
# Version: 5.2.5
# API Version: 5.2.5
# Go Version: go1.23.2
# Git Commit: 10c5aa720d59480bc7edad347c1f5d5b75d4424f
# Built: Wed Oct 23 19:54:08 2024
# OS/Arch: windows/amd64
#
# Server: Podman Engine
# Version: 5.3.0
# API Version: 5.3.0
# Go Version: go1.22.7
# Built: Wed Nov 13 01:00:00 2024
# OS/Arch: linux/amd64
podman machine inspect
# [
# {
# "ConfigDir": {
# "Path": "C:\\Users\\MyUser\\.config\\containers\\podman\\machine\\wsl"
# },
# "ConnectionInfo": {
# "PodmanSocket": null,
# "PodmanPipe": {
# "Path": "\\\\.\\pipe\\podman-machine-default"
# }
# },
# "Created": "2024-11-21T11:45:15.1653755+01:00",
# "LastUp": "2024-11-23T14:25:53.592163+01:00",
# "Name": "podman-machine-default",
# "Resources": {
# "CPUs": 10,
# "DiskSize": 100,
# "Memory": 2048,
# "USBs": []
# },
# "SSHConfig": {
# "IdentityPath": "C:\\Users\\MyUser\\.local\\share\\containers\\podman\\machine\\machine",
# "Port": 58684,
# "RemoteUsername": "user"
# },
# "State": "running",
# "UserModeNetworking": false,
# "Rootful": false,
# "Rosetta": false
# }
# ]
wsl --version
# WSL version: 2.3.26.0
# Kernel version: 5.15.167.4-1
# WSLg version: 1.0.65
# MSRDC version: 1.2.5620
# Direct3D version: 1.611.1-81528511
# DXCore version: 10.0.26100.1-240331-1435.ge-release
# Windows version: 10.0.22631.4460
Additional information
Additional information like issue happens only occasionally or issue happens with a particular architecture or on a particular setting
I stepped somehow in a similar issue. First of all I think you need to use host.docker.internal / host.containers.internal instead of localhost.
I noticed a difference between Windows and Linux here. On Windows the host.docker.internal / host.containers.internal IPs address the WSL2 VM in background. Thus I am using the following batch snippet to workaround the problem (based on [1]).
SET WSLCMD=wsl -d podman-machine-default -u root
FOR /F %%i IN ('powershell "Get-NetIpAddress | where { $_.InterfaceAlias -Like '*WSL*' -and $_.AddressFamily -EQ 'IPv4' } |select -ExpandProperty IPAddress"') DO set WSLIP=%%i
%WSLCMD% sh -c "sed -i '/^host_containers_internal_ip/d' /etc/containers/containers.conf"
%WSLCMD% sh -c "sed -i '/\[containers\]/a host_containers_internal_ip = \"%WSLIP%\"' /etc/containers/containers.conf"
In cleartext - set the host_containers_internal_ip setting to match the WSL bridge network ip from windows.
[1] https://stackoverflow.com/questions/79098571/podman-container-cannot-connect-to-windows-host
First of all I think you need to use host.docker.internal / host.containers.internal instead of localhost.
Thank you for your suggestion, but unfortunately, I have to use localhost as it's a convention in our setup. All our services have dev/local configurations where URLs to other services use localhost, so developers can run everything locally in IDE/Docker/Podman, and the services communicate with each other via localhost.
This mainly boils down to how WSL2 does it's networking.
You have multiple options:
Right Hostname
You can use $(hostname).local inside wsl to hit your windows host.
Windows Host Term1:
winget install Insecure.Nmap --source winget
ncat --listen --source-port 5050 -c '(echo HTTP/1.1 200 OK)' --keep-open
Windows Host Term2:
podman run --rm curlimages/curl -s -I -X GET http://$(hostname).local:5050
HTTP/1.1 200 OK
Modify .wslconfig
You can change the way WSL2 does networking:
%HOMEPATH%/.wslconfig:
[wsl2]
networkingMode = mirrored
Override the resolution of localhost
If all else fails you can do:
podman VM:
echo "<YOUR WINDOWS IP> localhost" | sudo tee /etc/hosts
ping localhost # test to see if it's overiding correctly
Note: this overrides your entire
/etc/hostsand will probably break other things, but if you must you must.
You might also need to set:[wsl2] generateHosts = false
networkingMode = mirrored should be the answer here and does work correctly for docker, but something is still not correct for podman.
With mirrored networking on, with docker, I can use localhost and it will resolve to the correct containers. With podman (v5.4.2 at least), it does not. Adding an entry to the Windows hosts file for 192.168.86.42 host.docker.internal makes that resolve in the WSL containers correctly so some things are translating down.
I’m experiencing the same problem
On 5.6.0, I can connect to things on the Windows host just fine via host.docker.internal, but only so long as they aren't listening to only 127.0.0.1.
In my case at least, the podman machine can connect to something via telnet localhost 12345, but containers can't use telnet host.docker.internal 12345. Unsure if this is related to this bug, but seems like it could be.
You can change the way WSL2 does networking:
%HOMEPATH%/.wslconfig:[wsl2] networkingMode = mirrored
WesselAtWork's approach worked for me.
Only thing I had to do after creating that file with networkingMode = mirrored set was the following:
- Run
wsl --shutdown - Restart my Podman Machine in Podman Desktop.
After that, localhost resolved to my Windows PC's localhost from my Podman container.
For me, connecting to a Windows-host service on port 2331 (GDB server) only works, when I use --network=host. I'm on podman 5.6.1...
podman run -it --rm --network=host busybox telnet localhost 2331
Connected to localhost # works
podman run -it --rm busybox telnet localhost 2331
telnet: can't connect to remote host (127.0.0.1): Connection refused
podman run -it --rm busybox telnet host.containers.internal 2331
telnet: can't connect to remote host (10.88.0.1): Connection refused
Firewall rules for 2331 inbound and wslconfig using mirrored networking mode are both set.
it happens also to me, I had to use podman machine ip, the config I used is at https://github.com/lorypelli/TestPHP
I'm running into the same issue on Podman 6.1.1, rootless. I have WSL2 with its networkingMode set to mirrored. I have completely powered my system off and back on since making any changes to the configuration of either WSL or Podman or the Podman machine.
I have a service running on my Windows host listening on http://localhost:65087. If I open a terminal into my podman machine, I can't access http://localhost:65087, but I can access http://host.containers.internal:65087. However, if I use a container (e.g. podman run --rm -it alpine sh), I can't access localhost, or host.containers.internal, or $(hostname).local, even if I use --network host. All of them fail with "Could not connect to server".