Hello World

Flask Deployment with Docker and Nginx

Get Docker CE for Ubuntu

Follow the instructions get Docker commnunity edition for Ubuntu.

When you run the “Hello World” test:

  1. The Docker client contacted the Docker daemon.
  2. The Docker daemon pulled the “hello-world” image from the Docker Hub.
  3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
  4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.

Major concepts of Docker

  1. Image
  2. Container
  3. Repository

An image is a lightweight, stand-alone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files.

A container is a runtime instance of an image—what the image becomes in memory when actually executed. It runs completely isolated from the host environment by default, only accessing host files and ports if configured to do so.

Containers run apps natively on the host machine’s kernel. They have better performance characteristics than virtual machines that only get virtual access to host resources through a hypervisor. Containers can get native access, each one running in a discrete process, taking no more memory than any other executable.


sudo apt-get update
sudo apt-get install python-dev python-pip git
sudo pip install virtualenv

If you haven’t installed Docker-CE, you can otherwise sudo apt-get install docker.io

Set up the Flask project

  1. Make a new empty directory for your project.
  2. Set up a virtual environment in a directory called venv with the command virtualenv venv.
  3. Activate the virtual environment with source venv/bin/activate.
  4. Make a file requirements.txt that has all your dependencies in it. For the simplest flask app, all you need is the line: Flask==0.11.1
  5. Install your dependencies with pip install -r requirements.txt
  6. Make a flask app at app/main.py. For the Docker image we will use, you need to do two important things:
    • Make sure the app is really called main.py
    • Make sure the Flask app variable is really called app. The Flask app variable is declared like app = Flask(..blah blah..). If you call this variable something else like my_app = Flask(..blah blah..), the Docker image won’t find it and it will not work!

If you have a complex Flask app with many files, that’s fine. Just make sure the file with the line app = Flask(..blah blah..) is called main.py and is in the app/ directory.

Here’s an example Flask app:

from flask import Flask

# the all-important app variable:
app = Flask(__name__)

def hello():
return "Oh, Hello World"

if __name__ == "__main__":
app.run(host='', debug=True, port=80)

The app folder together with Dockerfile, requirements.txt and venv folder should be in the top project directory

Make a Dockerfile


Deploying Flask Apps Easily with Docker and Nginx
Docker tutorial