- Ansible beginner tutorial - Wed, Nov 15 2023
- Nested Docker containers: Run Docker in a Docker container - Fri, Nov 10 2023
- Docker networking: Connect a Docker container - Fri, Oct 27 2023
Prerequisites
- A Linux system with Docker installed and running
- A root user or a user account with sudo privileges
Preparing a Python environment
Before starting, you will need to install a Python package and additional Python dependencies on your system.
Let's use the apt command to install all required packages.
apt install python3 python3-pip python3.10-venv -y
Then, create a directory to store your application.
mkdir app
Next, change the directory to your app directory.
cd app
Next, create a Python virtual environment to isolate all dependencies.
python3 -m venv python-app
After that, activate the virtual environment.
source python-app/bin/activate
Then, install the Flask package using the PIP command.
pip3 install Flask
Creating a Python application
Your Python virtual environment is now ready to create and deploy a Python application.
First, create an app.py for your Python application.
nano app.py
Add the following Python code:
# Import the Flask module for your application and define your app. from flask import Flask, render_template app = Flask(__name__) # Define the basic route and the request handler. @app.route('/') def home(): return render_template('index.html') # Run the application. if __name__ == '__main__': app.run(debug=True,host='0.0.0.0')
Save and close the file.
Then, create a templates directory and an HTML file for your application.
mkdir templates nano templates/index.html
Add the following HTML code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <style> .multicolor-text { text-align: center; font-size: 50px; background: linear-gradient(to left, violet, indigo, blue, green, yellow, orange, red); -webkit-background-clip: text; color: transparent; } </style> </head> <body> <div class="multicolor-text"> Dockerizing a Python application: A step-by-step tutorial! </div> </body> </html>
Save and close the file. Then, create a requirements.txt file to define any dependency that your application needs.
nano requirements.txt
Add the following dependency:
flask Jinja2
Save and close the file.
Finally, run your Python application locally.
python app.py
If everything is OK, you will see the following screen:
Your application is now running and listening on Port 5000. You can access it using the URL http://your-server-ip:5000 in your web browser.
Dockerizing a Python application
A Dockerfile is a text file that contains a set of instructions for building a Docker image from your local application.
Let's create a Dockerfile in your application directory.
nano Dockerfile
Add the following configuration to build a docker image from your application code.
FROM python:3.8 WORKDIR /app COPY . . RUN pip install -r requirements.txt CMD ["python","app.py"]
Save and close the file when you are done.
Here is a brief explanation of each command used in the above file.
- FROM: Specify a Python base image that matches your application's requirements.
- WORKDIR: Define a working directory for your application.
- COPY: Copy your application's source code and files into the container.
- RUN: Specify a command to install all required dependencies inside the container.
- CMD: Specify a command to run your Python application inside the container.
Build and run a Python app in a container
At this point, your Dockerfile is ready to build the Docker image.
Let's build the docker container image using the docker build command.
docker build -t python-app .
This will build the image named python-app using the specified Dockerfile.
Verify your built Docker image using the following command:
docker images
You will see your python-app Docker image on the following screen.
Finally, create and run python-app containers from your built image.
docker run -dit --name python-app -p 8080:5000 python-app
The above command will create a new container from the python-app docker image and map Port 8080 on your host to Port 5000 in the container.
You can also verify your running container using the below command:
docker ps
You will see the status of your python-app container on the following screen.
Now, open your web browser and verify your Python application is running inside a container using the URL http://your-server-ip:8080. You will see the following screen:
Subscribe to 4sysops newsletter!
Conclusion
In this post, you learned how to package a Python application into a Docker image and run it in an isolated environment. Dockerizing Python applications will improve your deployment process and streamline all your deployment workflows.
Read the latest IT news and community updates!
Join our IT community and read articles without ads!
Do you want to write for 4sysops? We are looking for new authors.