Dockerfile linkspython3.7, latest (Dockerfile)python3.6 (Dockerfile)python3.6-index (Dockerfile)python3.6-alpine3.7 (Dockerfile)python3.6-alpine3.8 (Dockerfile)python3.5 (Dockerfile)python3.5-index (Dockerfile)python2.7 (Dockerfile)python2.7-index (Dockerfile)python2.7-alpine3.7 (Dockerfile)python2.7-alpine3.8 (Dockerfile)python3.7-alpine3.7 (Dockerfile) and python3.7-alpine3.8 (Dockerfile) Temporarily not supported as uWSGI has not been released with Python 3.7 support for Alpine 3.7 nor for Alpine 3.8Docker image with uWSGI and Nginx for Flask web applications in Python 3.7, Python 3.6, Python 3.5 and Python 2.7 running in a single container. Optionally using Alpine Linux.
This Docker image allows you to create Flask web applications in Python that run with uWSGI and Nginx in a single container.
The combination of uWSGI with Nginx is a common way to deploy Python Flask web applications. It is widely used in the industry and would give you decent performance. (*)
There is also an Alpine version. If you want it, use one of the Alpine tags from above.
If you are starting a new project, you might benefit from a newer and faster framework based on ASGI instead of WSGI (Flask and Django are WSGI-based).
You could use an ASGI framework like:
FastAPI, or Starlette, would give you about 800% (8x) the performance achievable with Flask using this image (tiangolo/uwsgi-nginx-flask). You can see the third-party benchmarks here.
Also, if you want to use new technologies like WebSockets it would be easier (and possible) with a newer framework based on ASGI, like FastAPI or Starlette. As the standard ASGI was designed to be able to handle asynchronous code like the one needed for WebSockets.
If you need to use Flask (instead of something based on ASGI) and you need to have the best performance possible, you can use the alternative image: tiangolo/meinheld-gunicorn-flask.
tiangolo/meinheld-gunicorn-flask will give you about 400% (4x) the performance of this image (tiangolo/uwsgi-nginx-flask).
It is very simlar to tiangolo/uwsgi-nginx-flask, so you can still use many of the ideas described here.
GitHub repo: https://github.com/tiangolo/uwsgi-nginx-flask-docker
Docker Hub image: https://hub.docker.com/r/tiangolo/uwsgi-nginx-flask/
python3.7 tag: general Flask web application:python3.7 tag: general Flask web application, structured as a package, for bigger Flask projects, with different submodules. Use it only as an example of how to import your modules and how to structure your own project:example-flask-package-python3.7.zip
python3.7 tag: static/index.html served directly in /, e.g. for Vue, React, Angular, or any other Single-Page Application that uses a static index.html, not modified by Python:example-flask-python3.7-index.zip
You don't have to clone this repo, you should be able to use this image as a base image for your project with something in your Dockerfile like:
FROM tiangolo/uwsgi-nginx-flask:python3.7
COPY ./app /app
There are several image tags available for Python 3.7, Python 3.6, Python 3.5 and Python 2.7, but for new projects you should use Python 3.7.
As of now, everyone should be using Python 3.
There are several template projects that you can download (as a .zip file) to bootstrap your project in the section "Examples (project templates)" above.
This Docker image is based on tiangolo/uwsgi-nginx. That Docker image has uWSGI and Nginx installed in the same container and was made to be the base of this image.
Note: You can download the example-flask-python3.7.zip project example and use it as the template for your project from the section Examples above.
Or you may follow the instructions to build your project from scratch:
Dockerfile with:FROM tiangolo/uwsgi-nginx-flask:python3.7
COPY ./app /app
app directory and enter in itmain.py file (it should be named like that and should be in your app directory) with:from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World from Flask"
if __name__ == "__main__":
# Only for debugging while developing
app.run(host='0.0.0.0', debug=True, port=80)
the main application object should be named app (in the code) as in this example.
Note: The section with the main() function is for debugging purposes. To learn more, read the Advanced instructions below.
.
├── app
│ └── main.py
└── Dockerfile
Dockerfile is, containing your app directory)docker build -t myimage .
docker run -d --name mycontainer -p 80:80 myimage
...and you have an optimized Flask server in a Docker container.
You should be able to check it in your Docker container's URL, for example: http://192.168.99.100 or http://127.0.0.1
There are several project generators that you can use to start your project, with everything already configured.
All these project generators include automatic and free HTTPS certificates generation provided by:
...using the ideas from DockerSwarm.rocks.
It would take about 20 minutes to read that guide and have a Docker cluster (of one or more servers) up and running ready for your projects.
You can have several projects in the same cluster, all with automatic HTTPS, even if they have different domains or sub-domains.
Then you can use one of the following project generators.
It would take about 5 extra minutes to generate one of these projects.
And it would take about 3 more minutes to deploy them in your cluster.
In total, about 28 minutes to start from scratch and get an HTTPS Docker cluster with your full application(s) ready.
These are the project generators:
Project link: https://github.com/tiangolo/flask-frontend-docker
Minimal project generator with a Flask backend, a modern frontend (Vue, React or Angular) using Docker multi-stage building and Nginx, a Traefik load balancer with HTTPS, Docker Compose (and Docker Swarm mode) etc.
Project Link: https://github.com/tiangolo/full-stack
Full stack project generator with Flask backend, PostgreSQL DB, PGAdmin, SQLAlchemy, Alembic migrations, Celery asynchronous jobs, API testing, CI integration, Docker Compose (and Docker Swarm mode), Swagger, automatic HTTPS, Vue.js, etc.
Project Link: https://github.com/tiangolo/full-stack-flask-couchbase
✨ This project generator is the most feature-complete. ✨
Full stack project generator with Flask backend, Couchbase, Couchbase Sync Gateway, Celery asynchronous jobs, API testing, CI integration, Docker Compose (and Docker Swarm mode), Swagger, automatic HTTPS, Vue.js, etc.
Similar to the one above (full-stack), but with Couchbase instead of PostgreSQL, and some more features.
Project Link: https://github.com/tiangolo/full-stack-flask-couchdb
Full stack project generator with Flask backend, CouchDB, Celery asynchronous jobs, API testing, CI integration, Docker Compose (and Docker Swarm mode), Swagger, automatic HTTPS, Vue.js, etc.
Similar to full-stack-flask-couchbase, but with CouchDB instead of Couchbase (or PostgreSQL).
If you are building modern frontend applications (e.g. Vue, React, Angular) you would most probably be compiling a modern version of JavaScript (ES2015, TypeScript, etc) to a less modern, more compatible version.
If you want to serve your (compiled) frontend code by the same backend (Flask) Docker container, you would have to copy the code to the container after compiling it.
That means that you would need to have all the frontend tools installed on the building machine (it might be your computer, a remote server, etc).
That also means that you would have to, somehow, always remember to compile the frontend code right before building the Docker image.
And it might also mean that you could then have to add your compiled frontend code to your git repository (hopefully you are using Git already, or learning how to use git).
Adding your compiled code to Git is a very bad idea for several reasons, some of those are:
For these reasons, it is not recommended that you serve your frontend code from the same backend (Flask) Docker container.
There's a much better alternative to serving your frontend code from the same backend (Flask) Docker container.
You can have another Docker container with all the frontend tools installed (Node.js, etc) that:
To learn the specifics of this process for the frontend building in Docker you can read:
After having one backend (Flask) container and one frontend container, you need to serve both of them.
And you might want to serve them under the same domain, under a different path. For example, the backend (Flask) app at the path /api and the frontend at the "root" path /.
You can then use Traefik to handle that.
And it can also automatically generate HTTPS certificates for your application using Let's Encrypt. All for free, in a very easy setup.
If you want to use this alternative, check the project generators above, they all use this idea.
In this scenario, you would have 3 Docker containers:
If you want to check the previous (deprecated) documentation on adding a frontend to the same container, you can read the deprecated guide for single page apps.
Note: You can download the example-flask-package-python3.7.zip project example and use it as an example or template for your project from the section Examples above.
You should be able to follow the same instructions as in the "QuickStart" section above, with some minor modifications:
app/ directory, put it in a directory app/app/.__init__.py inside of that app/app/ directory.uwsgi.ini inside your app/ directory (that is copied to /app/uwsgi.ini inside the container).uwsgi.ini file, add:[uwsgi]
module = app.main
callable = app
The explanation of the uwsgi.ini is as follows:
app.main. So, in the package app (/app/app), get the main module (main.py).app object (app = Flask(__name__)).Your file structure would look like:
.
├── app
│ ├── app
│ │ ├── __init__.py
│ │ ├── main.py
│ └── uwsgi.ini
└── Dockerfile
...instead of:
.
├── app
│ ├── main.py
└── Dockerfile
...after that, everything should work as expected. All the other instructions would apply normally.
.
├── app
│ ├── app
│ │ ├── api
│ │ │ ├── api.py
│ │ │ ├── endpoints
│ │ │ │ ├── __init__.py
│ │ │ │ └── user.py
│ │ │ ├── __init__.py
│ │ │ └── utils.py
│ │ ├── core
│ │ │ ├── app_setup.py
│ │ │ ├── database.py
│ │ │ └── __init__.py
│ │ ├── __init__.py
│ │ ├── main.py
│ │ └── models
│ │ ├── __init__.py
│ │ └── user.py
│ └── uwsgi.ini
└── Dockerfile
Make sure you follow the offical docs while importing your modules:
For example, if you are in app/app/main.py and want to import the module in app/app/core/app_setup.py you would write it like:
from .core import app_setup
or
from app.core import app_setup
app/app/api/endpoints/user.py and you want to import the users object from app/app/core/database.py you would write it like:from ...core.database import users
or
from app.core.database import users
You can customize several things using environment variables.
index.html directlyNotice: this technique is deprecated, as it can create several issues with modern frontend frameworks. For the details and better alternatives, read the section above.
Setting the environment variable STATIC_INDEX to be 1 you can configure Nginx to serve the file in the URL /static/index.html when requested for /.
That would improve speed as it would not involve uWSGI nor Python. Nginx would serve the file directly. To learn more follow the section above "QuickStart for SPAs".
For example, to enable it, you could add this to your Dockerfile:
ENV STATIC_INDEX 1
By default, the image starts with 2 uWSGI processes running. When the server is experiencing a high load, it creates up to 16 uWSGI processes to handle it on demand.
If you need to configure these numbers you can use environment variables.
The starting number of uWSGI processes is controlled by the variable UWSGI_CHEAPER, by default set to 2.
The maximum number of uWSGI processes is controlled by the variable UWSGI_PROCESSES, by default set to 16.
Have in mind that UWSGI_CHEAPER must be lower than UWSGI_PROCESSES.
So, if, for example, you need to start with 4 processes and grow to a maximum of 64, your Dockerfile could look like:
FROM tiangolo/uwsgi-nginx-flask:python3.7
ENV UWSGI_CHEAPER 4
ENV UWSGI_PROCESSES 64
COPY ./app /app
You can set a custom maximum upload file size using an environment variable NGINX_MAX_UPLOAD, by default it has a value of 0, that allows unlimited upload file sizes. This differs from Nginx's default value of 1 MB. It's configured this way because that's the simplest experience an inexperienced developer in Nginx would expect.
For example, to have a maximum upload file size of 1 MB (Nginx's default) add a line in your Dockerfile with:
ENV NGINX_MAX_UPLOAD 1m
By default, the container made from this image will listen on port 80.
To change this behavior, set the LISTEN_PORT environment variable. You might also need to create the respective EXPOSE Docker instruction.
You can do that in your Dockerfile, it would look something like:
FROM tiangolo/uwsgi-nginx-flask:python3.7
ENV LISTEN_PORT 8080
EXPOSE 8080
COPY ./app /app
uwsgi.ini configurationsThere is a default file in /app/uwsgi.ini with app specific configurations (on top of the global uwsgi configurations).
It only contains:
[uwsgi]
module = main
callable = app
module = main refers to the file main.py.callable = app refers to the Flask "application", in the variable app.You can customize uwsgi by replacing that file with your own, including all your configurations.
For example, to extend the default one above and enable threads, you could have a file:
[uwsgi]
module = main
callable = app
enable-threads = true
uwsgi.ini file locationYou can override where the image should look for the app uwsgi.ini file using the envirnoment variable UWSGI_INI.
With that you could change the default directory for your app from /app to something else, like /application.
For example, to make the image use the file in /application/uwsgi.ini, you could add this to your Dockerfile:
ENV UWSGI_INI /application/uwsgi.ini
COPY ./application /application
WORKDIR /application
Note: the WORKDIR is important, otherwhise uWSGI will try to run the app in /app.
Note: you would also have to configure the static files path, read below.
./static/ pathYou can make Nginx use a custom directory path with the files to serve directly (without having uWSGI involved) with the environment variable STATIC_PATH.
For example, to make Nginx serve the static content using the files in /app/custom_static/ you could add this to your Dockerfile:
ENV STATIC_PATH /app/custom_static
Then, when the browser asked for a file in, for example, http://example.com/static/index.html, Nginx would answer directly using a file in the path /app/custom_static/index.html.
Note: you would also have to configure Flask to use that as its static directory.
As another example, if you needed to put your application code in a different directory, you could configure Nginx to serve those static files from that different directory.
If you needed to have your static files in /application/static/ you could add this to your Dockerfile:
ENV STATIC_PATH /application/static
/static URLYou can also make Nginx serve the static files in a different URL, for that, you can use the environment variable STATIC_URL.
For example, if you wanted to change the URL /static to /content you could add this to your Dockerfile:
ENV STATIC_URL /content
Then, when the browser asked for a file in, for example, http://example.com/content/index.html, Nginx would answer directly using a file in the path /app/static/index.html.
/app/prestart.shIf you need to run anything before starting the app, you can add a file prestart.sh to the directory /app. The image will automatically detect and run it before starting everything.
For example, if you want to add Alembic SQL migrations (with SQLALchemy), you could create a ./app/prestart.sh file in your code directory (that will be copied by your Dockerfile) with:
#! /usr/bin/env bash
# Let the DB start
sleep 10;
# Run migrations
alembic upgrade head
and it would wait 10 seconds to give the database some time to start and then run that alembic command.
If you need to run a Python script before starting the app, you could make the /app/prestart.sh file run your Python script, with something like:
#! /usr/bin/env bash
# Run custom Python script before starting
python /app/my_custom_prestart_script.py
Note: The image uses source to run the script, so for example, environment variables would persist. If you don't understand the previous sentence, you probably don't need it.
By default, Nginx will start one "worker process".
If you want to set a different number of Nginx worker processes you can use the environment variable NGINX_WORKER_PROCESSES.
You can use a specific single number, e.g.:
ENV NGINX_WORKER_PROCESSES 2
or you can set it to the keyword auto and it will try to autodetect the number of CPUs available and use that for the number of workers.
For example, using auto, your Dockerfile could look like:
FROM tiangolo/uwsgi-nginx-flask:python3.7
ENV NGINX_WORKER_PROCESSES auto
COPY ./app /app
Content type
Image
Digest
Size
348.4 MB
Last updated
over 7 years ago
docker pull lnls/uwsgi-nginx-flask:python3.6