
Photo via editor Chatgpt
# Entry
You probably know the fight if you tried to run your application on another computer, a laptop of a team member, test server or cloud. Something will always break. Perhaps the package is not installed or the Python version is disabled or the environment is simply not appropriate.
This is where Docker makes life easier. Thanks to Docker, you can connect the entire application code, dependencies and the environment in a neat tiny container that works the same everywhere. You can publish this container to Docker Hub so that everyone can pull it and start it immediately.
In this guide I will go through:
- Write a straightforward Python application
- Build a picture of a dock for him
- Test it locally
- Push him to docker hub so that you can share
# Preliminary requirements
Before discussing the docing of the Python application, make sure you have the following configuration:
- Python installed: Make sure Python is installed on your computer (preferably Python 3.7+). You can check it by launching:
python --versionOrpython3 --version - Docker installed and launched: You will need installation and starting docker on your computer. If you haven’t installed it yet, download it from Docker Desktop. After installing, confirm that Docker works:
docker --version - Docker Hub account: To publish your online image, you need a free Docker Hub account. Register here if you don’t have it yet: Docker Hub.
# Step 1: Create a straightforward Python application
Before we enter Docker, we need something that they actually lay down. Let’s start with a very basic Python internet application using FlaskLightweight internet frame.
This application will have one route that says honor. To do this, create a folder called Docker-Python-App, and in the middle, create two files:
// 1. app.py
from flask import Flask
app = Flask(__name__)
@app.route("https://www.kdnuggets.com/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
In this code:
- We create a flask application.
- We define one route (/) that returns a cordial message.
- We run the application on the “0.0.0.0” host so that Docker can reveal it outside the container.
- The port is set to 8000.
// 2. requirements.txt
Docker must know what Python requires, so let’s replace them in requirements.txt file:
# Step 2: Create Docker file
Now that you have Python application, we must teach Docker how to build and start it. That’s what the Docker file is for. This is basically a recipe that Docker says:
“Here’s what basic images to use, here’s how to install dependencies, and here’s how to start the application.”
In the project folder (Docker-Python-APP), create a file called Dockerfile (no file extension):
# 1. Start with a lightweight Python base image
FROM python:3.11-slim
# 2. Set the working directory in the container
WORKDIR /app
# 3. Copy the dependency file and install packages
COPY requirements.txt .
RUN pip install --upgrade pip && pip install --no-cache-dir -r requirements.txt
# 4. Copy the rest of your app code
COPY . .
# 5. Tell Docker which port the app will apply
EXPOSE 8000
# 6. Define the command to run your app
CMD ["python", "app.py"]
This file basically:
- Uses a tiny official Python image
- Installs the dependencies of your application
- Copies the code inside the container
- He takes
app.pyWhen the container begins
That’s all you need to contain your application. Now let’s build it.
# Step 3: Build Docker image
In the terminal in the project catalog, launch:
docker build -t your_dockerhub_username/docker-python-app .
Don’t forget to replace your_dockerhub_username with the actual username. In this command:
docker buildHe tells Docker to create a picture-tallows you to mark (name) the image so that it is simple to appeal later.He tells Docker to apply the current catalog (where your docker file lives)
After about a minute, Docker will pack the application in the image. You will see something in your terminal as:


# Step 4: Start and test your image locally
Let’s make sure it actually works before we publish it.
Run this command:
docker run -p 8000:8000 your_dockerhub_username/docker-python-app
This command says Docker:
- “Start a container”
- Map Port 8000 on a local machine for the port of 8000 inside the container (where the flask operates)
You will see something in your terminal as:

Now open the browser and go to http://localhost:8000. You should see:
If you see it, your image works exactly as expected.
# Step 5: Pimulate Docker’s image to docker hub
Now lower your image to the Docker Hub repository using the following command:
docker push your_dockerhub_username/docker-python-app
If it is displayed, authenticate first with docker login Using Docker Hub’s certificates.


# Step 6: Pull and run from anywhere
Everyone can now pull out the Docker image with:
docker pull image_owner_username/docker-python-app
. john123 And you want to pull this picture, you will enter:
docker pull kanwal5119/docker-python-app
Because kanwal5119 He is the owner of the image, you can only pull it out and start, not modify or press it, unless you have access.
Start it using the following command:
docker run -p 8000:8000 image_owner_username/docker-python-app
To get out, go to http://localhost:8000 Or http://127.0.0.1:8000/


# Application
In this article, you learned how to create a Python application, contain it with Docker, testing it locally and press it to Docker Hub, making it portable, shared and ready to run anywhere. This makes your development program more clever and more scalable. If you want to go on, try:
- Adding tags: V1.0 to your paintings.
- Creation
.dockerignoreFile to optimize compilation. - Configuring automated compilation using GitHub + Docker Hub.
- Starting the image on the cloud platform (like AWS, GCP or Azure).
There is a lot more with Docker, but now you have bases blocked. If you get stuck at any time or have any questions, leave the comment below.
Canwal Mehreen He is a machine learning engineer and a technical writer with a deep passion for data learning and AI intersection with medicine. He is the co -author of the ebook “maximizing performance from chatgpt”. As a Google 2022 generation scholar for APAC, it tells diversity and academic perfection. It is also recognized as a variety of terradate at Tech Scholar, Mitacs Globalink Research Scholar and Harvard Wecode Scholar. Kanwalwal is a heated supporter of changes, after establishing FemCodes to strengthen women in the STEM fields.
