I have a weird problem with connecting to docker containers via tcp.
My OS is Ubuntu 20.04
What I do.
I start my web server in a container. I have tried official Postgresql image and the problem stays the same. So the problem is probably is not my image.
It listens 0.0.0.0 on port 8080 . I have changed the port several times, so it's not about 8080 only.
I forward 8080 container port to 8080 on host. I have tried forwarding to different ports and the problem stays.
Here's the command
docker run --rm --name my-web-container -p8080:8080 my-web-image
The is try to wget localhost:8080 and it hangs for a while and then says
Connection closed by foreign host.
telnet localhost 8080 works for some time and then says the same thing
# telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
If I wget localhost:8080 from within the container everything is fine.
If I add --net=host , to the command, starting the container, the problem goes away.
So I suppose there is something wrong with docker network. I could always use --net=host, but that obviously creates problems.
This appeared out of the blue, I didn't do anything. No system configuration, no installing new software.