š³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
- A simple web app (weāll use Flask for this example)
- Docker Desktop installed: https://www.docker.com/products/docker-desktop
- Basic terminal knowledge
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')
requirements.txt:
Flask==2.2.2
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"]
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
- -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?
Wrap-Up: What Youāve Learned
- Youāve containerized your first web app
- You learned about Dockerfiles, images, and containers
- 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
Top comments (0)