My setup -
project/
app.py
test_hello.py
test/
Dockerfile
requirements.txt
app.py body is
from flask import Flask
app = Flask(__name__)
@app.route('/hello')
def privet():
return 'hello bob'
if __name__ == '__main__':
app.run(debug=True)
test_hello.py body is
import requests
def test_hello():
session = requests.Session()
session.trust_env = False
s = session.get('http://localhost:5000/hello').text
assert s == 'hello bob'
Dockerfile is
# syntax=docker/dockerfile:1
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app
ENV FLASK_RUN_HOST=0.0.0.0
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]
CMD ["pytest"]
When I'm launching test locally on my machine - Everything is OK
But when I'lauching test on Docker container (after building image and running container) - I'm getting error from requests:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=5000): Max retries exceeded with url: /hello (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fbcacb07c90>: Failed to establish a new connection: [Errno 111] Connection refused'))
/usr/local/lib/python3.7/site-packages/requests/adapters.py:516: ConnectionError
Could anyone please tell me what is wrong? Awesome thanks
Try to connect to host.docker.internal
(instead of the 127.0.0.1
) when you are in the Docker environment. The special DNS name may be slightly different depending on the Docker version (Windows/MacOS).
Here is a very good answer which explains this issue.
So sorry
The problem was in 2 CMD lines usage
From docs -
There can only be one CMD instruction in a Dockerfile
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.