How to Dockerize a Python App?—? with real job scenario/DevOps ticket

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:

  1. Access to App Server 2: You should have SSH access to App Server 2, where you intend to deploy your Docker container.
  2. Docker Installed: Docker should be installed on App Server 2. You can follow the installation instructions on the Docker website.
  3. 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:

  1. Create a Dockerfile for your Python app.
  2. Build a Docker image using the Dockerfile.
  3. Create a Docker container from the image.
  4. Map the container’s port to the host server’s port.
  5. 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 [email protected]        

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:

  1. Regular Updates: Keep your Docker host and containers up to date to ensure the latest security patches are applied.
  2. Use Official Images: Whenever possible, use official Docker images as your base image to benefit from continuous security updates.
  3. 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

要查看或添加评论,请登录

Gabriel Okom的更多文章

  • HOW TO CREATE A SIMPLE DOCKER NETWORK?—?with job scenario/ticket

    HOW TO CREATE A SIMPLE DOCKER NETWORK?—?with job scenario/ticket

    A docker image A step by step guide create a docker network Introduction: In the fast-paced world of DevOps, where…

  • HOW TO CREATE A POD - with security and troubleshooting tips

    HOW TO CREATE A POD - with security and troubleshooting tips

    Creating and Securing Kubernetes Pods: Best Practices and Troubleshooting Tips In the world of Kubernetes, Pods are the…

  • SIMPLE WAYS TO CONFIGURE K8S APP

    SIMPLE WAYS TO CONFIGURE K8S APP

    Pre-requisites: Previous knowledge of Kubernetes and Kubernetes Cluster Already have basic knowledge on how to create a…

  • BITCOIN

    BITCOIN

    Dear connection, the 3rd and last round of our Initial Coin Offer starts today. From now on you'll receive a special…

  • STATIC ROUTING

    STATIC ROUTING

    STATIC ROUTING (Using Cisco Packet Tracer) what is routing? Routing is the process of selecting a path for traffic in a…

社区洞察

其他会员也浏览了