

Picture by Editor | ChatGPT
# Introduction
You in all probability know the wrestle should you’ve tried working your app on a special machine, a teammate’s laptop computer, a take a look at server, or the cloud. One thing all the time breaks. Possibly 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’ll be able to bundle your complete app code, dependencies, and surroundings right into a neat little container that runs the identical all over the place. You’ll be able to publish that container to Docker Hub so anybody can pull it down and run it immediately.
On this information, I’ll stroll via how one can:
- Write a easy Python app
- Construct a Docker picture for it
- Check it regionally
- Push it to Docker Hub so it’s shareable
# Conditions
Earlier than we cowl Dockerizing your Python app, be sure to have the next arrange:
- Python Put in: Ensure Python is put in in your machine (ideally Python 3.7+). You’ll be able to examine this by working:
python --versionorpython3 --version - Docker Put in and Operating: You’ll want Docker put in and working in your machine. Should you haven’t put in it but, obtain it from Docker Desktop. After putting in, verify 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 should 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 internet app utilizing Flask, a light-weight internet framework.
This app may have a single route that claims howdy. For that, create a folder named docker-python-app, and inside it, create two recordsdata:
// 1. app.py
from flask import Flask
app = Flask(__name__)
@app.route("https://www.kdnuggets.com/")
def howdy():
return "Hiya 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 ready to 8000.
// 2. necessities.txt
Docker must know what Python packages your app requires, so let’s checklist them in a necessities.txt file:
# Step 2: Create a Dockerfile
Now that you just’ve bought a Python app, we have to educate Docker how one can construct and run it. That’s what the Dockerfile is for. It’s mainly a recipe that tells Docker:
“Right here’s what base picture to make use of, right here’s how one can set up dependencies, and right here’s how one can run the app.”
In your venture folder (docker-python-app), create a file known 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 mainly:
- Makes use of a small official Python picture
- Installs your app’s dependencies
- Copies your code contained in the container
- Runs
app.pywhen the container begins
That is all you could containerize your app. Now let’s construct it.
# Step 3: Construct the Docker Picture
In your terminal contained in the venture listing, run:
docker construct -t your_dockerhub_username/docker-python-app .
Don’t forget to switch your_dockerhub_username along with your precise username. On this command:
docker constructtells Docker to create a picture-tallows 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 make sure that it really 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 working)
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 <image_owner_username> refers back to the Docker Hub username of the individual or group who owns the picture, not yours (until you’re the proprietor). For instance, in case your username is john123 and also you need to pull this picture, you’d sort:
docker pull kanwal5119/docker-python-app
As a result of kanwal5119 owns the picture, you’ll be able to solely pull and run it, not modify or push to it until you’ve gotten entry.
Run it utilizing the command:
docker run -p 8000:8000 image_owner_username/docker-python-app
To your output, go to http://localhost:8000 or http://127.0.0.1:8000/
![]()
![]()
# Conclusion
On this article, you realized how one can create a Python app, containerize it utilizing Docker, take a look at it regionally, 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, attempt:
- Including model tags like: v1.0 to your photos.
- Making a
.dockerignorefile to optimize builds. - Organising automated builds with GitHub + Docker Hub.
- Operating 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 bought 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 knowledge science and the intersection of AI with drugs. She co-authored the e-book “Maximizing Productiveness with ChatGPT”. As a Google Era Scholar 2022 for APAC, she champions variety and educational excellence. She’s additionally acknowledged as a Teradata Range in Tech Scholar, Mitacs Globalink Analysis Scholar, and Harvard WeCode Scholar. Kanwal is an ardent advocate for change, having based FEMCodes to empower girls in STEM fields.
