Nice time of the day for everybody and Hello, I am new at docker and I have faced a lot of problems with deploying of a Flask app. My Dockerfile:
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM ubuntu:20.04
EXPOSE 5000
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE 1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED 1
# Install pip requirements
RUN apt-get update && apt-get install -y python3-pip unixodbc-dev python3-dev
ADD requirements.txt .
RUN pip3 install -r requirements.txt
RUN pip3 install flask-restful
RUN pip3 install werkzeug
WORKDIR /app
ADD . /app
# Switching to a non-root user, please refer to https://aka.ms/vscode-docker-python-user-rights
RUN useradd appuser && chown -R appuser /app
USER appuser
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["gunicorn", "--bind", "127.0.0.1:5000", "main:app"]
Docker-compose:
version: '3.4'
services:
tutorial:
image: tutorial
build:
context: .
dockerfile: Dockerfile
ports:
- 5000:5000
main.py:
api.add_resource(Video,"/videos/<int:video_id>")
if __name__=='__main__':
app.run(host="127.0.0.1", port=5000,debug=False)
One if the main problems is that I cannot get a respose from container when it is locally runned on the local host.
In my docker log I see that gunicorn
listening to a port 5000
[2020-10-09 14:20:19 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2020-10-09 14:20:19 +0000] [1] [INFO] Listening at: http://127.0.0.1:5000 (1)
[2020-10-09 14:20:19 +0000] [1] [INFO] Using worker: sync
[2020-10-09 14:20:19 +0000] [7] [INFO] Booting worker with pid: 7
But according infromation from terminal I see my container port opened.But it is not binded as it is specified in docker-compose file ports:- 5000:5000.
C:\Users\fele>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3e0bfae76ec containerregistr.azurecr.io/restvideoshop:1 "gunicorn --bind 127…" 5 hours ago Up 33 seconds 5000/tcp beautiful_chatterjee
Furthermore if I open my windows docker desktop and inspect my container I see one port opened in not binded state.
So when I run my test.py
import requests
BASE="http://127.0.0.1:5000/"
data=[{"name":"Duke Nuken","views":1000,"likes":10},
{"name":"Never Again","views":20,"likes":80},
{"name":"Belarus","views":500,"likes":70}]
for i in range(len(data)):
response=requests.put(BASE+"videos/"+str(i),data[i])
print(response.json())
I see folowing log in the terminal:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=5000): Max retries exceeded with url: /videos/0 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x02FB8C30>: Failed to establish a new connection: [WinError 10061] Es konnte keine Verbindung hergestellt werden, da der Zielcomputer
die Verbindung verweigerte'))
Can anybody give my advice and tell me why I cannot get a response from container? Should I do any other configuration in docker-compose file or set some additional code for gunicorn?One more question is not directly related to docker, I am planning to deploy container with Azure container instances so is it requiered to use inside of container a Nginx or Azure takes itself things to be done with Nginx? Thanks in advance