DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Manage Microservices With Docker Compose
  • 5 Simple Tips to Keep Dockerized Apps Secure
  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector

Trending

  • MQTT, Kafka, NATS, or Zenoh? Choosing Edge-to-Cloud Messaging by Traffic Pattern
  • Evaluating RAG in Production — What the Numbers Actually Revealed
  • How Performance Engineers Find and Fix Hidden System Bottlenecks
  • Every Acquisition Promises One Company. Most Deliver Two.

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Build and Deploy a Flask Application Using Docker

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.

By 
subodh kureel
·
Dec. 20, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
34.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in


This 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.

Dockerfile
 




x
19


 
1
# start from base
2
FROM ubuntu:18.04
3
 
          
4
LABEL maintainer="Your Name <[email protected]>"
5
 
          
6
RUN apt-get update -y && \
7
    apt-get install -y python-pip python-dev
8
 
          
9
 
          
10
# We copy just the requirements.txt first to leverage Docker cache
11
COPY ./requirements.txt /app/requirements.txt
12
 
          
13
WORKDIR /app
14
 
          
15
RUN pip install -r requirements.txt
16
 
          
17
COPY . /app
18
 
          
19
CMD [ "python", "./app.py" ]



Step 2: Create a configuration file to install Flask framework of version 0.10.1

file name: requirements.txt

Plain Text
 




xxxxxxxxxx
1


 
1
Flask==0.10.1



Step 3: Create a sample JSON data file.

file name: file.json 

JSON
 




xxxxxxxxxx
1


 
1
'{ "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

Python
 




xxxxxxxxxx
1
17


 
1
import os
2
from flask import Flask, render_template, abort, url_for, json, jsonify
3
import json
4
 
          
5
app = Flask(__name__,template_folder='.')
6
 
          
7
# read file
8
with open('file.json', 'r') as myfile:
9
    data = myfile.read()
10
 
          
11
@app.route("/")
12
def index():
13
    return render_template('index.html', title="page", jsonfile=json.dumps(data))
14
 
          
15
 
          
16
if __name__ == '__main__':
17
    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 

HTML
 




xxxxxxxxxx
1
16


 
1
<!DOCTYPE html>
2
<html>
3
<body>
4
 
          
5
<h2>JSON Data</h2>
6
 
          
7
<p id="demo"></p>
8
 
          
9
<script>
10
 var jsonfile ={{ jsonfile|tojson }};
11
 var obj = JSON.parse(jsonfile);;
12
document.getElementById("demo").innerHTML = obj;
13
</script>
14
 
          
15
</body>
16
</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

Python
 




xxxxxxxxxx
1


 
1
---> 9d095b8eacb9
2
 
          
3
Successfully built 9d095b8eacb9
4
 
          
5
Successfully tagged flask_json:latest



$docker run -p 5000:5000 flask_json

log

Python
 




xxxxxxxxxx
1


 
1
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
2
 * Restarting with stat
3
 * Debugger is active!
4
 * 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

Python
 




xxxxxxxxxx
1


 
1
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                    NAMES
2
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/

checking the application is running

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.

application Docker (software) Flask (web framework) Build (game engine)

Opinions expressed by DZone contributors are their own.

Related

  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Manage Microservices With Docker Compose
  • 5 Simple Tips to Keep Dockerized Apps Secure
  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook