DEV Community

Cover image for Docker Basics: Containerizing Your First Web App
A.Satya Prakash
A.Satya Prakash

Posted on • Edited on

Docker Basics: Containerizing Your First Web App

🐳Docker Basics: Containerizing Your First Web App

Ever had a web app work perfectly on your laptop but completely break on someone else’s machine? Welcome to the infamous "it works on my machine" problem. Docker helps you solve that — by letting you package your app, dependencies, and environment into a portable container. In this blog, I’ll walk you through how I containerized a simple Flask web app, step-by-step.

What is Docker and Why Should You Care?

Docker is a tool that makes it easy to create, deploy, and run applications by using containers. A container bundles everything your app needs to run — code, libraries, and settings — into one neat unit. This means it will behave exactly the same on any machine that supports Docker.

If you're a student or grad getting into development, DevOps, or data science, learning Docker is a massive win. It's widely used in industry and makes deployment way less painful.

🧰 What You Need First

3. Step 1: Create a Simple Web App
A very basic app, like:

# app.py (Flask)
from flask import Flask
app = Flask(__name__)

@app.route("/")
def home():
    return "Hello from Docker!"

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0')
Enter fullscreen mode Exit fullscreen mode

requirements.txt:

Flask==2.2.2
Enter fullscreen mode Exit fullscreen mode

4. Step 2: Write a Dockerfile
Dockerfile is: a recipe for building the container.

# Dockerfile
FROM python:3.9-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .

CMD ["python", "app.py"]

Enter fullscreen mode Exit fullscreen mode

5. Step 3: Build and Run the Docker Image
Terminal commands:

docker build -t my-flask-app .
docker run -p 5000:5000 my-flask-app

Enter fullscreen mode Exit fullscreen mode
  • -t is for tagging
  • -p maps container port to local port

Then: visit http://localhost:5000 to see it working.

6. Bonus: Docker Compose (Optional)
If you want to scale or use a DB, you can explore docker-compose.yml.

7. Common Errors and Fixes
ā€œPort already in useā€ → how to stop a running container
ā€œModule not foundā€ → missing requirements.txt?

Image description

Wrap-Up: What You’ve Learned

  1. You’ve containerized your first web app
  2. You learned about Dockerfiles, images, and containers
  3. You can now run your app anywhere, even in the cloud

9. Next Steps

  • Try containerizing a Node.js or Django app
  • Push your image to Docker Hub
  • Deploy to AWS, GCP, or Render

WORKFLOW

  _**Dockerizing a Web App – Step-by-Step Flow**_

Top comments (0)