Docker Preview
Docker packages applications with everything they need to run. No more "it works on my machine." Same container runs anywhere.
Why Containers?
Traditional approach:
- Install OS
- Install dependencies (exact versions!)
- Configure environment
- Hope nothing conflicts
- Repeat for every server
Container approach:
- Build container once
- Run anywhere
Install Docker
Run Your First Container
That pulled an image and ran it as a container.
Core Concepts
| Term | Meaning |
|---|---|
| Image | Blueprint/template (like a class) |
| Container | Running instance (like an object) |
| Dockerfile | Instructions to build an image |
| Registry | Image storage (Docker Hub) |
Run Useful Containers
Run nginx
-druns in background (detached)-p 8080:80maps host port 8080 to container port 80
List Containers
Stop and Remove
Build Your Own Image
Create a Dockerfile:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Build and run:
Docker Compose
Manage multi-container apps with docker-compose.yml:
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgres://db:5432/myapp
depends_on:
- db
db:
image: postgres:15
environment:
POSTGRES_DB: myapp
POSTGRES_PASSWORD: secret
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
Essential Commands
When to Use Docker
Good for:
- Consistent development environments
- Deploying applications
- Running databases locally
- CI/CD pipelines
- Microservices
Overkill for:
- Simple scripts
- Learning Linux basics first
- When native install is simpler
Learn Docker Properly
This is just a preview. Docker deserves its own course. Once you're comfortable with Linux basics, dive deeper into containers.
What's the difference between a Docker image and container?
Quick Reference
| Command | Purpose |
|---|---|
docker run -d -p 80:80 img | Run container |
docker ps | List running containers |
docker stop id | Stop container |
docker rm id | Remove container |
docker images | List images |
docker build -t name . | Build image |
docker compose up -d | Start compose services |
docker compose down | Stop compose services |
docker logs id | View logs |
docker exec -it id sh | Shell into container |
Key Takeaways
- Images are templates, containers are running instances
-dfor background,-pfor port mapping- Dockerfile defines how to build an image
- Docker Compose manages multi-container apps
- Containers solve "works on my machine" problems
- Learn Linux basics before diving deep into Docker
Next: security fundamentals.