🐍 Build Your First Python App with Docker (Step-by-Step Guide)
In this guide, you’ll learn how to create a simple Python web server
and run it inside a Docker container — all from your WSL or Ubuntu terminal.
Follow these simple steps to get your first containerized Python app running!
🪜 Step 1: Create a New Project Folder
Open your WSL or Ubuntu terminal and create a new folder for your app:
mkdir ~/python-docker-demo
cd ~/python-docker-demo
🪜 Step 2: Create a Simple Python App
Create a file named app.py using nano editor:
nano app.py
Paste the following Python code inside:
from http.server import SimpleHTTPRequestHandler, HTTPServer
PORT = 8000
class HelloHandler(SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(b"Hello World from Docker + Python!")
server = HTTPServer(("", PORT), HelloHandler)
print(f"Server running on port {PORT}...")
server.serve_forever()
Save and exit:
Ctrl + O, Enter, Ctrl + X
🪜 Step 3: Create Your Dockerfile
Create a new file named Dockerfile:
nano Dockerfile
Paste this inside:
# Step 1: Use official lightweight Python image
FROM python:3.10-slim
# Step 2: Set working directory inside container
WORKDIR /app
# Step 3: Copy your app into container
COPY app.py .
# Step 4: Expose port 8000 for web access
EXPOSE 8000
# Step 5: Run Python app
CMD ["python", "app.py"]
Save and exit the file.
🪜 Step 4: Build the Docker Image
Now build your Docker image with the following command:
docker build -t python-hello .
🪜 Step 5: Run the Container
Start your container in detached mode and map port 8000:
docker run -d -p 8000:8000 python-hello
🪜 Step 6: Test It!
Now open your browser and visit:
👉 http://localhost:8000
You should see this message:
Hello World from Docker + Python!
🎉 Congrats! You’ve Built Your First Python Docker Container
You’ve just created and containerized your first Python web application!
This is the foundation for deploying apps to the cloud using Docker and AWS.
🪜 Step 7 (Next Time): Push to AWS
In the next tutorial, we’ll show how to push this Docker image to
AWS Elastic Container Registry (ECR)
and run it on an AWS EC2 instance or ECS cluster. Stay tuned!
