Serving and Deploying Keras Models using Flask, UWSGI, NGINX and Docker

Anyone who has tried (and failed) deploying their Keras models will understand the frustration of getting your trained models out there on the cloud. It is all well and good having all of the code sitting their in a Jupyter Notebook, but eventually you will want to deploy and run these models at scale. Hopefully this guide will help!

The Problem

Enter Docker

So lets assume the following application tree:

- .
- app
-- # this contains your keras model and prediction code
- Dockerfile

Now we populate the Dockerfile.

FROM joelogan/keras-tensorflow-flask-uwsgi-nginx-dockerCOPY ./app /app

Note that the joelogan/keras-tensorflow-flask-uwsgi-nginx-docker image installs all of the serving frameworks, Python and a number of dependencies, such as Keras, TensorFlow, Pillow, Matplotlib and H5PY so that you can get up and running with serving your models easily. If you need any additional libraries (for example, Pandas) you can do it as follows in the Dockerfile:

FROM joelogan/keras-tensorflow-flask-uwsgi-nginx-dockerRUN pip install pandasCOPY ./app /app

Then we run docker build which will spin up a container inheriting all of the serving code (and Python, TensorFlow, Matplotlib and a few other dependencies) and copy your app code into the container.

docker build -t some_random_name .

Docker will proceed to download all of the dependencies and set up the container. Then we can run it and test it on your local machine:

docker run -d --name random_container -p 80:80 some_random_image

Or deploy it somewhere else, such as a DigitalOcean VM or Google Cloud.

And a big shout out to tiangolo, who I inherited the base for the keras-tensorflow-flask-uwsgi-nginx-docker image from.



Developer and AI enthisiast from Sydney. Founder of Alixir. Check me out @

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dr. Joe Logan

Developer and AI enthisiast from Sydney. Founder of Alixir. Check me out @