

Picture by Editor | ChatGPT
# Introduction
You in all probability know the battle in case you’ve tried operating your app on a special machine, a teammate’s laptop computer, a check server, or the cloud. One thing at all times breaks. Perhaps a bundle isn’t put in, or the Python model is off, or the surroundings simply is not fairly proper.
That’s the place Docker makes life simpler. With Docker, you may bundle your complete app code, dependencies, and surroundings right into a neat little container that runs the identical all over the place. You possibly can publish that container to Docker Hub so anybody can pull it down and run it immediately.
On this information, I’ll stroll by the best way to:
- Write a easy Python app
- Construct a Docker picture for it
- Check it domestically
- Push it to Docker Hub so it’s shareable
# Conditions
Earlier than we cowl Dockerizing your Python app, be sure you have the next arrange:
- Python Put in: Ensure that Python is put in in your machine (ideally Python 3.7+). You possibly can test this by operating:
python --version
orpython3 --version
- Docker Put in and Working: You’ll want Docker put in and operating in your machine. Should you haven’t put in it but, obtain it from Docker Desktop. After putting in, affirm Docker is working:
docker --version
- Docker Hub Account: To publish your picture on-line, you’ll want a free Docker Hub account. Enroll right here in case you don’t have already got one: Docker Hub.
# Step 1: Create a Easy Python App
Earlier than we get into Docker, we want one thing to really containerize. So let’s begin with a really primary Python net app utilizing Flask, a light-weight net framework.
This app may have a single route that claims hi there. For that, create a folder named docker-python-app, and inside it, create two information:
// 1. app.py
from flask import Flask
app = Flask(__name__)
@app.route("https://www.kdnuggets.com/")
def hi there():
return "Hi there World!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
On this code:
- We create a Flask app.
- We outline one route (/) that returns a pleasant message.
- We run the app on host “0.0.0.0” so Docker can expose it exterior the container.
- The port is about to 8000.
// 2. necessities.txt
Docker must know what Python packages your app requires, so let’s record them in a necessities.txt
file:
# Step 2: Create a Dockerfile
Now that you simply’ve obtained a Python app, we have to train Docker the best way to construct and run it. That’s what the Dockerfile is for. It’s principally a recipe that tells Docker:
“Right here’s what base picture to make use of, right here’s the best way to set up dependencies, and right here’s the best way to run the app.”
In your undertaking folder (docker-python-app), create a file referred to as Dockerfile (no file extension):
# 1. Begin with a light-weight Python base picture
FROM python:3.11-slim
# 2. Set the working listing within the container
WORKDIR /app
# 3. Copy the dependency file and set up packages
COPY necessities.txt .
RUN pip set up --upgrade pip && pip set up --no-cache-dir -r necessities.txt
# 4. Copy the remainder of your app code
COPY . .
# 5. Inform Docker which port the app will use
EXPOSE 8000
# 6. Outline the command to run your app
CMD ["python", "app.py"]
This file principally:
- Makes use of a small official Python picture
- Installs your app’s dependencies
- Copies your code contained in the container
- Runs
app.py
when the container begins
That is all it is advisable to containerize your app. Now let’s construct it.
# Step 3: Construct the Docker Picture
In your terminal contained in the undertaking listing, run:
docker construct -t your_dockerhub_username/docker-python-app .
Don’t forget to exchange your_dockerhub_username
along with your precise username. On this command:
docker construct
tells Docker to create a picture-t
permits you to tag (identify) the picture so it’s simple to reference later.
tells Docker to make use of the present listing (the place your Dockerfile lives)
After a minute or so, Docker will bundle your app into a picture. You will notice one thing in your terminal as:
# Step 4: Run and Check Your Picture Domestically
Let’s ensure that it truly works earlier than we publish it.
Run this command:
docker run -p 8000:8000 your_dockerhub_username/docker-python-app
This command tells Docker:
- “Run the container”
- Map port 8000 in your native machine to port 8000 contained in the container (the place Flask is operating)
You will notice one thing in your terminal as:
Now open your browser and go to http://localhost:8000
. It is best to see:
Should you see that, your picture works precisely as anticipated.
# Step 5: Push the Docker Picture to Docker Hub
Now push your picture to your Docker Hub repository utilizing the command:
docker push your_dockerhub_username/docker-python-app
If prompted, authenticate first with docker login
utilizing your Docker Hub credentials.
# Step 6: Pull and Run from Wherever
Anybody can now pull your Docker picture utilizing:
docker pull image_owner_username/docker-python-app
The john123
and also you wish to pull this picture, you’ll kind:
docker pull kanwal5119/docker-python-app
As a result of kanwal5119
owns the picture, you may solely pull and run it, not modify or push to it except you’ve entry.
Run it utilizing the command:
docker run -p 8000:8000 image_owner_username/docker-python-app
On your output, go to http://localhost:8000
or http://127.0.0.1:8000/
# Conclusion
On this article, you realized the best way to create a Python app, containerize it utilizing Docker, check it domestically, and push it to Docker Hub, making it transportable, shareable, and able to run wherever. This makes your growth workflow cleaner and extra scalable. If you wish to go additional, strive:
- Including model tags like: v1.0 to your pictures.
- Making a
.dockerignore
file to optimize builds. - Establishing automated builds with GitHub + Docker Hub.
- Working your picture on a cloud platform (like AWS, GCP, or Azure).
There’s much more you are able to do with Docker, however now you’ve obtained the fundamentals locked in. Should you get caught at any level or have any questions, go away a remark beneath.
Kanwal Mehreen is a machine studying engineer and a technical author with a profound ardour for information science and the intersection of AI with drugs. She co-authored the book “Maximizing Productiveness with ChatGPT”. As a Google Technology Scholar 2022 for APAC, she champions range and tutorial excellence. She’s additionally acknowledged as a Teradata Variety in Tech Scholar, Mitacs Globalink Analysis Scholar, and Harvard WeCode Scholar. Kanwal is an ardent advocate for change, having based FEMCodes to empower ladies in STEM fields.