Dockerize Simple Flask App

In this tutorial you will learn how to create a simple Flask App and run it inside a docker container

Required Software

  • docker (1.6.0 or above)
  • python 2.7 or above
  • Linux VM - (We used ubuntu 14.04 64 bit)

Create the Flask App and Deployment files

Create a folder web. All our files will be inside this directory

Flask Application

Create a new file inside web and add the following python code

From flask import Flask
app = Flask(__name__)

def hello_world():
    return 'Flask Dockerized'

if __name__ == '__main__':,host='')

Requirements File

Requirements file states the software required to be installed in the container. Create a file requirements.txt inside web folder



This file is needs to create a docker image and deploy it

FROM ubuntu:latest
MAINTAINER Rajdeep Dua ""
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
COPY . /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD [""]

Build the docker Image

Run the following command to build the docker image flask-sample-one from web directory

$ docker build -t flask-sample-one:latest .

Run the Docker Container

$ docker run -d -p 5000:5000 flask-sample-one

You can find the container runtime details as shown below

$ docker ps -a
CONTAINER ID        IMAGE                              COMMAND                CREATED             STATUS                             PORTS                    NAMES
92fb4d65c7cd        flask-sample-one:latest            "python"        22 minutes ago      Up 22 minutes            >5000/tcp   clever_blackwell