2

I have a dockerized Python app that outputs data on port 8080 and port 8081. I am running the code on a Ubuntu system.

$ docker version | grep Version
 Version:      18.03.1-ce

The app responds on port 8080

$ curl -k localhost:8080 | tail -4
-->
TYPE hello_world_total counter
hello_world_total 3.0
TYPE hello_world_created gauge
hello_world_created 1.5617357381235116e+09

The app returns an ERROR on port 8081

$ curl -k localhost:8081
curl: (56) Recv failure: Connection reset by peer

Although I am not familiar with netstat, I used it to check that ports 8080 and 8081 were both in the LISTEN state ...

root@1d1ac2974893:/# netstat -apn
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      1/python3
tcp        0      0 127.0.0.1:8081          0.0.0.0:*               LISTEN      1/python3
tcp        0      0 172.17.0.2:58220        16.46.41.11:8080        TIME_WAIT   -
tcp        0      0 172.17.0.2:58218        16.46.41.11:8080        TIME_WAIT   -
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
root@1d1ac2974893:/#

My Dockerfile looks as follows ...

$ cat Dockerfile
FROM python:3
RUN pip3 install prometheus_client
COPY sampleapp.py /src/sampleapp.py
EXPOSE 8081
CMD [ "python3", "/src/sampleapp.py" ]

When I run the application, I map both ports 8080 and 8081 from the Docker container to the same ports on the host as follows ...

$ docker run -p 8081:8081 -p 8080:8080 sampleapp 

If I go into the Container and repeat the above curl commands, they work as I expect.

root@1d1ac2974893:/# curl -k localhost:8081 | tail -4
TYPE hello_world_total counter
hello_world_total 3.0
TYPE hello_world_created gauge
hello_world_created 1.5617357381235116e+09
root@1d1ac2974893:/#

AND

$ docker exec -it 1d1ac2974893 /bin/bash
root@1d1ac2974893:/# curl -k localhost:8081
Hello World

SO the question is why the latter curl command does NOT work from the host system.

$ curl -k localhost:8081
curl: (56) Recv failure: Connection reset by peer
Ginger
  • 119
  • 1
  • 2
  • 8
gf1
  • 21
  • 1

1 Answers1

0

Solution was as follows

  1. Expose both ports in the Dockerfile $ grep EXPOSE Dockerfile EXPOSE 8080 EXPOSE 8081

  2. Use 0.0.0.0 rather than 127.0.0.1 import http.server from prometheus_client import start_http_server from prometheus_client import Counter

    HOST='0.0.0.0' HELLO_WORLD_PORT=8080 HELLO_WORLD_METRICS_PORT=8081 REQUESTS = Counter('hello_world_total', 'Hello World Requested')

    class MyHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): REQUESTS.inc() self.send_response(200) self.end_headers() self.wfile.write(b"Hello World\n")

    if name == "main": start_http_server(HELLO_WORLD_METRICS_PORT) server = http.server.HTTPServer((HOST, HELLO_WORLD_PORT), MyHandler) server.serve_forever()

Contaainer now gives the expected results when run from the host $ curl -k localhost:8080 Hello World $ $ curl -k localhost:8081 | tail -4 ... # TYPE hello_world_total counter hello_world_total 1.0 # TYPE hello_world_created gauge hello_world_created 1.5619773258069074e+09 $

Xref :- Docker Rails app fails to be served - curl: (56) Recv failure: Connection reset by peer for details of a similar issue

gf1
  • 21
  • 1