Build Your First Python App with Docker (Step-by-Step for Beginners)”

Build Your First Python App with Docker (Step-by-Step for Beginners)”

AI Reading

Quick summary of this article

This guide walks beginners through creating a simple Python web server and running it inside a Docker container using a terminal. It covers setting up a project folder, writing a basic Python app that displays "Hello World", creating a Dockerfile to define the container environment, building the Docker image, and running the container locally. The tutorial is designed for users on WSL or Ubuntu and ends with a preview of deploying the container to AWS.

  • Create a project folder using the command mkdir ~/python-docker-demo and navigate into it with cd.
  • Write a simple Python web server in a file named app.py that uses Python's built-in HTTP server to respond with "Hello World from Docker + Python!" on port 8000.
  • Define a Dockerfile that uses the official python:3.10-slim image, sets the working directory to /app, copies the app file, exposes port 8000, and runs the Python app.
  • Build the Docker image with docker build -t python-hello . and then run the container in detached mode using docker run -d -p 8000:8000 python-hello.
  • Test the app by opening http://localhost:8000 in a browser to see the "Hello World" message, confirming the containerized app works.

🐍 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!

Leave a Reply

Your email address will not be published. Required fields are marked *