Run Python + Flask Web app in Docker Container

Do you want to Dockerize your Python Flask web app. Well follow these steps. very often there will be difference of packages installed on Dev vs tester systems.
So running webapp using Docker can be easy for testing.
Follow this 10 step simple tutorial.

1. Create a Virtual Environment first .

# virtualenv dockerC
# cd dockerC
# source bin/activate

2. Install Flask
# pip install Flask

3. Install any other packages u want eg: memcached.
# pip install python-memcached

4. collect the requirements, so that we package all modules.
# pip freeze > requirements

5. create a DIR with webapp. move the requirements file to that dir.
# mkdir webapp

 #mv requirements webapp/

6. work on your Webapp using Flask.
app.py has code for entry points.

#cat app.py
from flask import Flask
app = Flask(__name__)


@app.route("/")
def get():
    return "Hello Docker"

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

7. Now create Docker File called "Dockerfile" in same webapp dir
The contents has to be like this.

FROM fedora:latest
MAINTAINER Maintainer redara
RUN dnf install -y python python-pip
COPY .   /webapp
RUN pip install -r /webapp/requirements
EXPOSE 8000
WORKDIR /webapp
CMD python app.py
#eof

The Structure of our webapp dir is here.

(dockerC) [vedara@localhost webapp]# ls -ltr
total 12
-rw-r--r--. 1 vedara vedara 151 Apr 10 16:18 requirements
-rw-r--r--. 1 vedara vedara 180 Apr 10 16:46 app.py
-rw-r--r--. 1 vedara vedara 187 Apr 10 16:49 Dockerfile

description of Docker file is here
FROM uses the fedora image from docker hub.
RUN means we are installing required packages . here we are installing python,pip first
COPY .   /webapp means we are copying our current dir(. dir) which is "webapp" to /webapp dir in destination
once we copied our webapp dir, lets install required packages for Flask using RUN pip install -r /webapp/requirements
EXPOSE will expose the port on which container listens to connections. our webapp uses 8000 as port.
WORKDIR sets the working dir , so that you dont need to specify absolute paths everytime
CMD python app.py shows the cmd needed to run the flask web app.

8. Lets Build the Docker Image now. give -t which can be used to refer your image. "." means looks for Dockerfile in curr dir.
# docker build -t flask_webapp .

9. Lets run the Docker Container now by using the cmd here. giving -p tells docker to publish the containers port to host.
   #docker run  -p 8000:8000 flask_webapp 

  If you want to run as daemon specify -d option in above cmd.

10. Lets hit the URL now using curl
  #curl -i http://0.0.0.0:80

 HTTP/1.0 200 OK
 Content-Type: text/html; charset=utf-8
 Content-Length: 12
 Server: Werkzeug/0.12.1 Python/2.7.13
 Date: Mon, 10 Apr 2017 12:15:39 GMT
 Hello Docker

Thats it. your web app is running. Push it your Docker Hub using your username/password
#docker push username/tagname

Note: This demo is based on Just "Hello Docker". copy your entire webapp files using COPY . if you use database you can ship different container for Database server or Ship in Same Container.

Use the Dockerfile to copy required packages and run in same container.

Contact me for any queries: smart.ram856@gmail.com

No comments: