🐳 Getting Started with Docker: Images, Containers & Dockerfile Basics (2025)
📌 What is Docker?
Docker is an open-source platform that lets you package your applications and dependencies into a standardized unit called a container. Containers are lightweight, fast, and portable — like mini virtual machines, but without the heavy baggage.
Think of Docker containers as isolated sandboxes where your app always behaves the same — no more “works on my machine” drama!
🆚 Docker vs Virtual Machines
| Aspect | Docker | Virtual Machines |
|---|---|---|
| Startup Time | ⚡ Milliseconds | ⏳ Minutes |
| Resource Usage | 💡 Low | 🔋 High |
| Isolation Level | Medium (OS shared) | High (Own OS) |
⚙️ Components of Docker
- Docker Engine: Core service that builds, runs, and manages containers.
- Docker CLI: Command-line tool to interact with Docker (e.g.,
docker run). - Docker Desktop: GUI + CLI for managing Docker on Windows/macOS (includes Docker Engine).
- Docker Hub: Public registry where you can find and pull container images.
🧪 Running Your First Docker Container
Let’s run the classic Hello World container:
docker run hello-world
This pulls the hello-world image from Docker Hub and runs it as a container. You should see:
Hello from Docker!
This message shows that your installation appears to be working correctly.
📦 Dockerfile: Building Your Own Image
Let’s say you have a simple Node.js app. Here's how you can write a Dockerfile:
# Dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]
To build this image:
docker build -t my-node-app .
To run the container:
docker run -p 3000:3000 my-node-app
🐍 Python Version (Bonus)
If you prefer Python, here’s a minimal Flask app app.py and Dockerfile:
# app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello from Docker!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
# Dockerfile
FROM python:3.11-alpine
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]
🧱 Image & Container Lifecycle
- Build Image:
docker build -t name . - Run Container:
docker run -d -p host:container name - List Running:
docker ps - Stop Container:
docker stop CONTAINER_ID - Remove Container:
docker rm CONTAINER_ID - Remove Image:
docker rmi IMAGE_ID
🔍 Pulling Images from Docker Hub
Docker Hub is like GitHub for containers. To pull a ready-to-use image:
docker pull nginx
docker pull mongo
docker pull python:3.11
Then run them like this:
docker run -d -p 8080:80 nginx
🧠 Pro Tips for Beginners
- Use
docker ps -ato see all containers (even stopped). - Add
--rmto auto-remove containers after they stop. - Use
.dockerignore(like.gitignore) to skip files during build. - Tag images with versions:
docker build -t myapp:v1 .
🎯 Summary
You’ve just walked through the basics of Docker — from containers and images to building your first Dockerfile. Whether you're building in Node.js or Python, Docker keeps your environment clean and consistent.
In our next post, we’ll dive into 🔗 (Post 2): Docker Volumes & Networks: Managing Data & Communication!
— Blog by Aelify (ML2AI.com)