How Can You Bottle a Web App? Let’s Dockerize a Flask App and See the Magic!
Have you ever dreamed of packaging your web app like a lunchbox — ready to go, easy to open, and works anywhere? That’s exactly what Docker helps us do!
In this project, I took a simple Flask app and learned how to wrap it neatly inside a Docker container. It felt like turning my tiny code project into a portable superhero. 🦸♂️💻
Let me walk you through what I learned, step-by-step, as a complete beginner.
🐍 Step 1: Meet Flask — The Friendly Web Framework
Flask is a lightweight web framework in Python. It’s super easy to use when you want to build small web apps or APIs. Here’s a tiny Flask app:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello from Flask inside Docker!"
We saved this as app.py
. Simple, right?
🐳 Step 2: What is Docker and Why Should I Care?
Docker is like a magic box. You put your app inside it, close the lid, and now your app can run anywhere, no matter what OS or software is outside. 🧊
This makes sharing and deploying apps super easy — whether it’s your friend’s laptop, your college server, or the cloud.
📦 Step 3: Writing the Dockerfile (a Recipe for the Magic Box)
Here’s how I told Docker how to build the container:
# Use an official Python image
FROM python:3.9-slim
# Set working directory
WORKDIR /app
# Copy app files
COPY app.py requirements.txt ./
# Install Flask
RUN pip install -r requirements.txt
# Set environment variable (optional but cool!)
ENV FLASK_ENV=development
# Tell Docker how to run the app
CMD ["python", "app.py"]
I saved this as Dockerfile
—no file extension!
The line ENV FLASK_ENV=development
helped me pass configuration into the container. It’s like setting the app’s mood from outside!
🚀 Step 4: Build and Run the Flask Container
I used just two commands:
docker build -t flask-docker-app .
docker run -p 5000:5000 flask-docker-app
Boom! 🎉 My app was now running on http://localhost:5000
, and it was inside a container. No Flask installed on my machine—just Docker!
⚙️ CMD vs ENTRYPOINT — What’s the Difference?
This part confused me at first, but here’s what I figured out:
CMD
is like the default command. You can override it when you run the container.ENTRYPOINT
is the main command. It sticks no matter what.
Example:
ENTRYPOINT ["python", "app.py"]
CMD ["--port=8000"]
Here, CMD
adds extra options to the ENTRYPOINT
command.
💡 Bonus: Passing Input and Playing with Behavior
I tried passing command-line inputs like this:
docker run flask-docker-app --port=8000
Also, I experimented with changing the ENV
variable to switch between dev and production modes. It's fun seeing how flexible and powerful containers can be!
🔚 Wrapping It Up: Why This Matters
Before this project, Docker felt like a tech buzzword. But now? I actually get it. 😎
It’s not just about making things “cool” or “modern.” Docker makes your app portable, consistent, and deployment-ready. Whether you’re a solo dev, a student, or working in a team — it just makes life easier.
So yes, you can bottle up a web app — and I just did it with Flask and Docker!