1

I can can connect from outside the container (i.e. by typing localhost:5000 into a browser, or when running the same client code regularly from outside docker). I can also run my client when I replace http://localhost:5000/ in my client code with whatever url comes up when I start the server, like for example "Running on http://172.17.0.2:5000/ (Press CTRL+C to quit)"

server.py:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello World!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

client.py:

import requests
response = requests.get("http://localhost:5000/")
print(response.text)

server dockerfile:

FROM python:3.8.10
WORKDIR ./server
COPY ./app/server.py .
COPY ./requirements.txt .
RUN pip install -r requirements.txt
ENV FLASK_APP=server.py
CMD ["python", "server.py"]

client dockerfile:

FROM python:3.8.10
WORKDIR ./client
COPY ./app/client.py .
COPY ./requirements.txt .
RUN pip install -r requirements.txt
CMD ["python", "client.py"]

commands I use: docker build ./server -t server docker run -p 5000:5000 server

followed by: docker build ./client -t client docker run client

I get this error:

Traceback (most recent call last):
  File "client.py", line 3, in <module>
    response = requests.get("http://localhost:5000/")
  File "/usr/local/lib/python3.8/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=5000): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fa25d245a30>: Failed to establish a new connection: [Errno 111] Connection refused'))

what am I doing wrong?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • It's because "localhost:5000" in the client script is pointing to it's own port 5000, not the port 5000 of the server. You could try the docker bridge IP instead 172.17.0.2:5000 – Cedric Nov 08 '21 at 00:25
  • Best practice is to `docker network create` a network, then `docker run --net` both containers on the same network, and their `docker run --name` will be usable as host names. Or, if you run this stack under Docker Compose, it does all of that setup for you. The linked questions (and @HiranChaudhuri's similar answer) have more details. – David Maze Nov 08 '21 at 11:10

1 Answers1

1

You are connecting server and client via the url http://localhost:5000, which only works if the processes are running on the same host. Next you start each of them in their own container, but docker will give each of them a different network namespace - you should assume each container is a separate machine. Localhost on one will not be the same as localhost for the other. In the end the perceived result "connection refused" is to be expected as in the client's container that port is not used for listening.

To get out of that situation:

Queeg
  • 7,748
  • 1
  • 16
  • 42