Sunday 29 October 2017

Dockerize Flask App

 Run Flask Application in Docker Container

   Dockerize Flask Application for Seamless Development Environment

As we know, Flask is a easy to use but very powerful framework for web development in Python. So in this tutorial we are going to learn to run Flask Application inside a Docker container.
Here are some prerequisites to complete, before proceeding ahead:
  • You must have Docker running on your machine.
  • Any browser to test the application.
  • Basic knowledge of Flask and Docker.
So let's start with dockerization process.
First we will create a simple “Hello World” Flask application. If you have your own developed Flask application, then skip this step else continue.
Let’s create a project directory named “example”, to keep all the Application and Docker related files at one place. for this run:
mkdir example

Now inside this folder, create a file called requirements.txt, that will contain required dependency for our Application. For this “Hello World” project, there is only single dependency that is Flask. If you have other dependencies as well for your own project then append those inside this file. This file will help us to install all the required packages inside Docker Container.
So create it and add the below code:
cd example
vim requirements.txt

Add,
flask==0.10.1

Now create a file called app.py and paste below code:
vim app.py

from flask import Flask
app = Flask(__name__)

@app.route('/', methods=['GET'])
def hello_world():
   return 'Hello World, Excited to see my App in a Docker container!'


if __name__ == '__main__':
   app.run(debug=True, host='0.0.0.0')

Note: Remember, to provide host=’0.0.0.0’, else by default Flask will run it on container’s localhost that is ‘127.0.0.1’ and you will not be able to access the application from your host machine.
Our “Hello World” Flask Application is ready now. To run it inside a Docker Container, we need to make a file called Dockerfile, as it will instruct the Docker machine to do required stuff. For this first create it.
touch Dockerfile

Now, add following code inside this file and save it.
FROM ubuntu:16.04

MAINTAINER <<Your Name>>
RUN apt-get update -y && \
   apt-get install -y python-pip python-dev
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip install -r requirements.txt
COPY . /app
ENTRYPOINT [ "python" ]
CMD [ "app.py" ]

As Dockerfile is ready, let’s build our customized Docker image using it. For this run:
docker build -t flask-image:latest .

Note: Remember to run this command from your project directory, else error will occur.
After running this command, you will have ready to run flask docker-image. You can see it by running command:
docker images

Now, let's do the final step to run our app inside the container that is running the image. For this run:
docker run -d -p 5000:5000 --name flask-container flask-image

You can see the container running by issuing following command:
docker ps

Now, just browse following link in your browser:
Hurrey! Our app is running. That’s all about this tutorial. Keep Learning, Keep Sharing :)

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel