How to Dockerize a Python App?—? with real job scenario/DevOps ticket
Docker and Deploy a Python Flask App to an App?Server?
Docker is a leading containerization technology that can help us package Python applications for easy deployment. By the end of this read, we should have successfully deployed a python app to an existing server.?
Prerequisites
Before you begin, ensure you have the following prerequisites:
Objectives
The objectives of this guide are as follows:
Job Scenario
“python app needed to be Dockerized, and then it needs to be deployed on App Server 2. We have already copied a requirements.txt file (having the app dependencies) under /python_app/src/ directory on App Server 2. Further complete this task as per details mentioned below:
Once deployed, you can test the app using curl command on App Server 2.
curl https://localhost:8096/”
Step-by-Step Guide
NOTE: App Server 2 = 172.16.238.11 [this is a fictional server)
Before we begin, here are some things to take note
cd into the /python_app/src/ folder as mentioned in the task: here I have both files already present as shown below.
I am going to catboth files so we can have a working knowledge of what is within the files.
for server.py?; I have this.
server.py
from flask import Flask
# the all-important app variable:
app = Flask(__name__)
@app.route("/")
def hello():
return "Welcome to xFusionCorp Industries!"
if __name__ == "__main__":
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.run(host='0.0.0.0', debug=True, port=8086)
for requirements.txt I have this output flask; you can create yours by simply running the commandtouch flask > requirements.txt
Step 1: Create a Dockerfile
Create a Dockerfile within the /python_app directory on App Server 2.?
The task mentioned that we run the app in the /python_app dir, so we have to go back into it and open the Dockerfile using vi
领英推荐
cd /python_app
vi Dockerfile
Configure the Dockerfile as specified in the task.
FROM python:3.8 # Use official Python image as the base
WORKDIR /python_app # Set the working dir inside the con
COPY src/requirements.txt . # Copy the .txt file into the con
RUN pip install -r requirements.txt # Install Python dependencies
EXPOSE 8086 # Expose port 8086
CMD ["python", "src/server.py"] # Run the server.py script using CMD
Step 2: Build the Docker?Image
Build a Docker image named nautilus/python-app using the following command within the /python_app directory:
docker build -t nautilus/python-app .
Step 3: Create a Docker Container
Create a Docker container named pythonapp_nautilus and map port 8086 of the container to host port 8096:
docker run -d -p 8096:8086 --name pythonapp_nautilus nautilus/python-app
Confirm the image build docker imagesand the container using docker ps
Step 4: Test the Dockerized Python?App
Now that my Docker container is deployed on App Server 2, I can test the Python app using the following curl command:
curl https://localhost:8096/
If you followed through you with the exact same code should receive a response from your Flask application:
Welcome to xFusionCorp Industries!
Security Tips
Maintaining security while working with Docker containers is crucial. Here are some security tips:
Ref:?
KodeKloud
#docker #python #flask #dockerimages #GabrielOkom #kubernetes #containers
ython #flask #dockerimages #GabrielOkom #kubernetes #containers