Build and Deploy Flask App Using Docker
Build a simple Flask API with two methods to handle get and post requests.

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

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

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

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

Run the API using the following Docker command:
docker run -it --name {container name} -p 8080:8080 {image name}

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

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.