Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection reset by peer when when hitting Docker container

Tags:

docker

I'm having a problem, where I can't send network requests to a Docker container I've created. I've exposed the correct ports, so I'm not sure what other issues could be at fault here.

I have a server running in container alice at localhost:10009:

$ docker exec -it alice bash bash-4.4# curl localhost:10009 curl: (52) Empty reply from server 

Port 10009 is exposed from my container:

$ docker port alice  10009/tcp -> 0.0.0.0:10009 

When doing the same curl from my host machine I get a different message:

$ curl localhost:10009 curl: (56) Recv failure: Connection reset by peer 
like image 955
torkel Avatar asked Sep 03 '19 14:09

torkel


People also ask

What is Connection reset by peer?

Resolution. Connection Reset by peer means the remote side is terminating the session. This error is generated when the OS receives notification of TCP Reset (RST) from the remote server.

What happens when you press Ctrl P Q inside the container in Docker?

You have to use two combinations, one after the other: ctrl+p followed by ctrl+q. You turn interactive mode to daemon mode, which keeps the container running but frees up your terminal. You can attach to it later using docker attach, if you need to interact with the container more.

Why is Docker killing my container?

The container has consumed too much memory, and has been killed by the host OS: If the operating system detects that it's running out of memory, it might start killing processes to free up memory. If a container is using a lot of memory or resources, it might be killed by the OS.

Does Docker expose a port by default?

By default, the EXPOSE instruction does not expose the container's ports to be accessible from the host. In other words, it only makes the stated ports available for inter-container interaction. For example, let's say you have a Node.


2 Answers

I would check to see if the server application is configured to only listen to requests coming from its "localhost", this check depends on the type of server that you're using which is not mentioned.

an easy check is to start your container as follows:

docker run --network host -d yourimagename 

You don't need to worry about port mapping since you're using the host network

then try to curl, if that works, then you'll just need to review your server listening IP setting.

curl localhost:10009 
like image 139
Bouzid Zitouni Avatar answered Sep 24 '22 16:09

Bouzid Zitouni


I think there are some problems with @Bouzid Zitouni's answer, according to Docker official documentation:

this is the same level of isolation as if the nginx process were running directly on the Docker host and not in a container

However, if you use the --network host you will not have isolated networking in the container, and the host networking driver only works on Linux hosts.

The problem of Connection refused/reset happens because your Server is listening on 127.0.0.1 inside the container and the port forwarding is going to external IP of the container (e.g. 172.17.0.2).

Solution

In your case you need to run a new container making your server to listen on all interfaces. Example using python http.server :

docker run -p 8000:8000 -it python:3.7-slim python3 -m http.server --bind 0.0.0.0 

Note

The option --bind 0.0.0.0 it's specific option of http.server. Probally your server has other ways to specify this.

References:

https://pythonspeed.com/articles/docker-connection-refused/

https://docs.docker.com/network/network-tutorial-host/

like image 41
C. Flor Avatar answered Sep 22 '22 16:09

C. Flor