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?