🐳 Docker Tutorial for Beginners: Step-by-Step with a Simple Example
Docker Explained Using a Very Simple App (Beginner Friendly)
Goal: Build a tiny Python app, run it normally, then run the same app using Docker, and understand why Docker helps.
Step 1: Create a Very Small Python App
Create a file named app.py:
print("Hello I am running inside Docker!")
Step 2: Run the App WITHOUT Docker
Run this on your machine:
python app.py
Common real-life problems: Python not installed, wrong Python version, missing dependencies, different OS behavior this is where the “works on my machine” issue starts.
Step 3: Introduce Docker
Instead of relying on your system, Docker lets you package the runtime (Python) together with your app. We’ll do that using a Dockerfile.
Step 4: Create a Dockerfile
Create a file named Dockerfile (no extension):
FROM python:3.11
WORKDIR /app
COPY app.py .
CMD ["python", "app.py"]
What each line means
FROM python:3.11→ Downloads Python inside the image (no need to install Python locally)WORKDIR /app→ Sets the working directory inside the containerCOPY app.py .→ Copies your app into the containerCMD ["python", "app.py"]→ Runs the app when the container starts
Step 5: Build a Docker Image
Run this command in the same folder as your Dockerfile:
docker build -t hello-docker .
Image = blueprint. Docker reads the Dockerfile, downloads the base image,
and creates a reusable image named hello-docker.
Step 6: Run the Container
Now run the image as a container:
docker run hello-docker
You should see output like:
Hello I am running inside Docker!
You just ran your app inside a Docker container.
What Just Happened?
- Python was inside the container
- Your system Python did not matter
- The app runs the same on any machine that has Docker
Image vs Container (Using This Example)
- Image:
hello-dockerbuilt once, reusable, read-only layers - Container: created by
docker run— runs the app, can be stopped/deleted
Simple analogy: Image = recipe, Container = cooked dish.
Why Docker is Better Than “Run It Normally”
- Without Docker: depends on local setup → easy to break
- With Docker: app + runtime bundled → predictable everywhere
Key Takeaway
Docker packages your app with its runtime so it runs the same everywhere.
Want to Learn More?
This post covered only the basics of Docker. Advanced topics are explained in a separate blog.
Read: Docker – Advanced Concepts (Internals & More)
Recommended after you are comfortable with Docker basics.
Next Post Ideas
- A tiny Flask web app in Docker (with ports)
- Docker image layers and caching (explained simply)
- Running the same container in Kubernetes
Comments
Post a Comment