Python Dockerfile

Dockerfile to run a python Flask app.

Assume the application is in main.py file,
Flask app has been instantiated on a variable called app using something similar to:

load Flask into app variableMinimal application
1
2
3
4
5
6
7
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"

The following Dockerfile runs a Flask appplication on a uwsgi server.

DockerfileDockerfile Reference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
FROM python:3.6.5

RUN pip3 install flask pymongo pyyaml
RUN CFLAGS="-I/usr/local/opt/openssl/include" \
LDFLAGS="-L/usr/local/opt/openssl/lib" \
UWSGI_PROFILE_OVERRIDE=ssl=true \
pip3 install uwsgi -I --no-cache-dir

ENV DB_HOST=${MONGO_HOST}
ENV DB_PORT=${MONGO_PORT}
ENV CLIENT_CORS=${CLIENT_CORS}

RUN mkdir /code
COPY main.py /code
WORKDIR /code

ENV DB_HOST=${DB_HOST:-127.0.0.1}
ENV DB_PORT=${DB_PORT:-27017}
ENV CLIENT_CORS=${CLIENT_CORS:-http://localhost:3000}

CMD uwsgi -w main:app \
--http11-socket 0.0.0.0:5000 \
--enable-threads \
--threads 4 \
--log-x-forwarded-for \
--logformat '%(addr) - %(user) [%(ltime)] "%(method) %(uri) %(proto)" %(status) %(size) "%(referer)" "%(uagent)"'

Dockerfile for a Socket.io application

If you would like to run a Socket.io Python application with Docker, check the code of the following Dockerfile.