Build and Deploy a Flask Application Using Docker
These basic steps can get you started with the initial setup of a Flask application. This application can further scale to store data into a persistent source.
Join the DZone community and get the full member experience.
Join For FreeThis tutorial covers simple steps to build and deploy a Flask application using Docker on Ubuntu 18.04. The sample application process data stored in a JSON file format and displayed on the browser.
Introduction
Docker is an open-source platform to develop, manage, and deploy applications using containers. For Docker installation, refer to this article.
Flask is a web micro-framework built on Python.
Instructions
Step 1: Create a Dockerfile.
Dockerfile is a first step to containerize an application. Dockerfile contains a list of commands to assemble an image.
# start from base
FROM ubuntu:18.04
LABEL maintainer="Your Name <youremailaddress@provider.com>"
RUN apt-get update -y && \
apt-get install -y python-pip python-dev
# We copy just the requirements.txt first to leverage Docker cache
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip install -r requirements.txt
COPY . /app
CMD [ "python", "./app.py" ]
Step 2: Create a configuration file to install Flask framework of version 0.10.1
file name: requirements.txt
xxxxxxxxxx
Flask==0.10.1
Step 3: Create a sample JSON data file.
file name: file.json
xxxxxxxxxx
'{ "name":"John", "age":30, "city":"New York"}'
Step 4: Write Python code to process JSON file data and flush out output to index.html.
file name: app.py
xxxxxxxxxx
import os
from flask import Flask, render_template, abort, url_for, json, jsonify
import json
app = Flask(__name__,template_folder='.')
# read file
with open('file.json', 'r') as myfile:
data = myfile.read()
route("/") .
def index():
return render_template('index.html', title="page", jsonfile=json.dumps(data))
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
Step 5: Index.html file render and display extract on the browser.
file name: Index.html
xxxxxxxxxx
<html>
<body>
<h2>JSON Data</h2>
<p id="demo"></p>
<script>
var jsonfile ={{ jsonfile|tojson }};
var obj = JSON.parse(jsonfile);;
document.getElementById("demo").innerHTML = obj;
</script>
</body>
</html>
Step 1 through 5 setup code and configuration of a Flask application on docker. Now execute the below-mentioned docker command to install and run the sample application.
$docker build -t flask_json:latest
log
xxxxxxxxxx
---> 9d095b8eacb9
Successfully built 9d095b8eacb9
Successfully tagged flask_json:latest
$docker run -p 5000:5000 flask_json
log
xxxxxxxxxx
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 292-583-301
In this step, you have successfully deployed the sample Flask application on Docker. Below command to verify container run.
$docker ps -a
xxxxxxxxxx
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ba26dd499f3 flask_json "python ./app.py" About a minute ago Up About a minute 0.0.0.0:5000->5000/tcp admiring_goodall
Lastly, now check application is running; visit the IP address(0.0.0.0:0000) at your browser.
http://0.0.0.0:0000/
Conclusion
These basic steps can get you started with the initial setup of a Flask application. This application can further scale to store data into a persistent source and furthermore.
Opinions expressed by DZone contributors are their own.
Comments