Docker For Beginners
You're building an app that works perfectly on your computer. But when you send it to your friend or deploy it on a server, it crashes. Sound familiar? 😩 In today’s fast-paced world of app development, consistency is everything. That’s where Docker comes in. Like a magic box 📦 that packs your app with everything it needs to run anywhere, without surprises. Docker has completely changed the game in how we develop, ship, and deploy applications using something called containerization.
What is Docker?
Docker is a tool that packages applications into containers, self-contained units that include:
- Your application code
- Dependencies (libraries, frameworks)
- Configuration files
Think of Docker like a shipping container:
- Standardizes deployment across environments
- Runs identically on laptops, servers, or the cloud
Why Use Docker?
Benefit | Description |
---|---|
Consistency | No more works on my machine issues |
Portability | Run anywhere Docker is installed |
Efficiency | Lightweight (shares OS resources) |
Scalability | Spin up multiple containers in seconds |
Isolation | Apps run separately without conflicts |
Containers vs. Virtual Machines
Docker Containers | Virtual Machines (VMs) |
---|---|
🚀 Lightweight & fast | 🐢 Heavier & slower |
⚡ Shares host OS kernel | 🔄 Requires full OS per VM |
📦 Process-level isolation | 🛡️ Hardware-level isolation |
💾 Minimal resource usage | 💽 High storage/memory consumption |
Core Docker Components
- Image
- Blueprint for containers (e.g.,
python:3.11-slim
)
- Blueprint for containers (e.g.,
- Container
- Running instance of an image
- Dockerfile
- Recipe to build images (see example below)
- Docker Hub
- Public registry for sharing images
Dockerfile Example
Dockerfiles are simple text files that define how to build Docker images. Here's an example Dockerfile for a Python app:
1# Start from a base image
2FROM python:3.11-slim
3
4# Set working directory
5WORKDIR /app
6
7# Install dependencies
8COPY requirements.txt .
9RUN pip install -r requirements.txt
10
11# Copy app code
12COPY . .
13
14# Start command
15CMD ["python", "app.py"]
Basic Workflow
- Build Image
1docker build -t myapp .
- Run Container
1docker run -p 8000:8000 myapp
- Manage Containers
1docker ps # List running containers 2docker stop <CONTAINER_ID> # Stop a container 3docker rm <CONTAINER_ID> # Delete a container
Essential Commands Cheatsheet
Command | Description | Example |
---|---|---|
docker build |
Build image | docker build -t myapp . |
docker run |
Start container | docker run -d -p 8000:8000 myapp |
docker ps |
List containers | docker ps -a |
docker images |
List images | docker images |
docker pull |
Download image | docker pull ubuntu |
docker exec |
Run command in container | docker exec -it myapp bash |
Real-World Use Case
Problem:
Your Python app works locally but crashes on your colleague's machine due to missing dependencies.
Docker Solution:
- Create a Dockerfile with Python + dependencies
- Build an image and share it
- Colleague runs the same container → identical environment!