Cloud Native Daily

A blog for Devs and DevOps covering tips, tools, and developer stories about all things cloud-native

Follow publication

Build and Deploy Flask App Using Docker

Dipto Chakrabarty
Cloud Native Daily
Published in
4 min readMay 14, 2023

Let us build a simple Flask API with two methods to handle get and post requests.

The link for the complete code can be found at the end of the blog.

Install Flask in the work directory.

pip3 install flask

Write the code to perform a simple get request in Flask.

from flask import Flask,jsonify,request

app = Flask(__name__)

@app.route("/")
def home():
return jsonify({
"Message": "app up and running successfully"
})

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

The endpoint ‘/’ defines the path which is required, performing a curl request to the path returns the response JSON.

Run the code using the following command

python3 app.py
API running on port 8080

The API is running on port 8080 with debug set to true(this must be ignored for the production environment).

curl http://localhost:8080/
Response from curl request

Add the endpoint to handle a post request.

@app.route("/access",methods=["POST"])
def access():
data = request.get_json()
name = data.get("name", "dipto")
server = data.get("server","server1")

message = f"User {name} received access to server {server}"

return jsonify({
"Message": message
})

In this route, we provide a name and a server name, we are trying to simulate an API call to request access to a server and return a message with the request name and server.

Run a curl command to check the response.

curl -X POST -H "Content-Type: application/json" \
-d '{"name": "ezio", "server": "dchost"}' \
http://localhost:8080/access
Response from post request

Complete API code:

from crypt import methods
from flask import Flask,jsonify,request

app = Flask(__name__)

@app.route("/")
def home():
return jsonify({
"Message": "app up and running successfully"
})

@app.route("/access",methods=["POST"])
def access():
data = request.get_json()
name = data.get("name", "dipto")
server = data.get("server","server1")

message = f"User {name} received access to server {server}"

return jsonify({
"Message": message
})


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

RUNNING the API on DOCKER

Create a Docker file to run the API.

FROM python:3.8-slim
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
EXPOSE 8080
CMD ["python3", "app.py"]

python:3.8-slim is a smaller version of Python3.8.

Copy the requirements.txt file and install all dependencies, this will ensure that if any code changes are made to the application the docker layer of packages installation will not occur again.

Expose the API port and run the command to start the API in the CMD section.

Build the docker image using the following command:

docker build -t {image name} .
Building the docker image

Run the API using the following Docker command:

docker run -it --name {container name} -p 8080:8080 {image name}
Running the api

Hit the API requests using curl to check the response and validate the container running.

GET and POST request

The Flask app has been deployed to docker successfully.

To run it in background mode run the following command:

docker run -itd --name {container name} -p 8080:8080 {image name}

The complete code can be found here -> Click on this link.

Further Reading:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Cloud Native Daily
Cloud Native Daily

Published in Cloud Native Daily

A blog for Devs and DevOps covering tips, tools, and developer stories about all things cloud-native

Dipto Chakrabarty
Dipto Chakrabarty

Written by Dipto Chakrabarty

MS @CMU , Site Reliability Engineer , I talk about Cloud Distributed Systems. Tech Doctor making sure to diagnose and make your apps run smoothly in production.

No responses yet

Write a response