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:
- Access to App Server 2: You should have SSH access to App Server 2, where you intend to deploy your Docker container.
- Docker Installed: Docker should be installed on App Server 2. You can follow the installation instructions on the Docker website.
- Python App and Dependencies: You should have a Python application ready, and its dependencies listed in a requirements.txt file. These dependencies will be installed in the Docker container.
Objectives
The objectives of this guide are as follows:
- Create a Dockerfile for your Python app.
- Build a Docker image using the Dockerfile.
- Create a Docker container from the image.
- Map the container’s port to the host server’s port.
- Test the Dockerized Python app on App Server 2.
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:
- Create a Dockerfile under /python_app directory:
- Use any python image as the base image.
- Install the dependencies using requirements.txt file.
- Expose the port 8086.
- Run the server.py script using CMD.
- Build an image named nautilus/python-app using this Dockerfile.
- Once image is built, create a container named pythonapp_nautilus:
- Map port 8086 of the container to the host port 8096.
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
ssh steve@172.16.238.11
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:
- Regular Updates: Keep your Docker host and containers up to date to ensure the latest security patches are applied.
- Use Official Images: Whenever possible, use official Docker images as your base image to benefit from continuous security updates.
- Minimize Privileges: Avoid running containers as the root user; instead, use non-root users.
Ref:?
KodeKloud
#docker #python #flask #dockerimages #GabrielOkom #kubernetes #containers
ython #flask #dockerimages #GabrielOkom #kubernetes #containers