DevOps18 min read·

Docker Tutorial for Beginners: Containerize Your First App in 10 Minutes

Learn Docker from scratch with this beginner-friendly tutorial. Build, run, and manage containers with hands-on examples and real terminal commands.

What Is Docker and Why Does It Matter?

Docker packages your application and everything it needs into a standardized unit called a container. The same container runs identically on your laptop, a colleague's machine, or a production server.

Before Docker, the phrase "it works on my machine" caused countless deployment failures. Docker eliminates that problem entirely.

Key advantages:

  • Consistent environments across development, testing, and production
  • Lightweight compared to virtual machines (share host OS kernel)
  • Fast startup (seconds vs. minutes for VMs)
  • Easy scaling and orchestration
  • Isolation between applications

Installing Docker

macOS — Download Docker Desktop from docker.com. Drag to Applications. Done.

Windows — Install Docker Desktop. Enable WSL 2 if prompted. Restart.

Linux — Run:

sudo apt update && sudo apt install docker.io -y
sudo usermod -aG docker $USER

Log out and back in for group changes to take effect.

Verify Installation:

docker --version
docker run hello-world

Your First Container

Run the hello-world container to verify your installation:

docker run hello-world

Docker downloads the image (if not cached), creates a container, runs it, and prints a success message.

Running Real Containers

Spin up an interactive Ubuntu container:

docker run -it ubuntu bash

You're now inside a full Ubuntu environment. Type exit to leave. Nothing persists — containers are ephemeral.

For a web server:

docker run -d -p 8080:80 nginx

Visit http://localhost:8080 and you'll see the Nginx welcome page.

Key flags:

  • -d — Detached mode (runs in background)
  • -p — Port mapping (host:container)
  • -it — Interactive mode with TTY

Building Custom Images with Dockerfiles

A Dockerfile is a recipe for your image. Here's one for a Node.js app:

FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]

Dockerfile instructions explained:

  • FROM — Base image to build from
  • WORKDIR — Working directory inside the container
  • COPY — Copy files from host to container
  • RUN — Execute commands during build
  • EXPOSE — Document the port the app uses
  • CMD — Default command to run when container starts

Build and run:

docker build -t my-app .
docker run -d -p 3000:3000 my-app

Multi-Stage Build Example:

# Build stage
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# Production stage
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
EXPOSE 3000
CMD ["node", "dist/server.js"]

Docker Compose for Multi-Container Apps

When your app needs multiple services (database, cache, API), Docker Compose manages them together:

services:
  api:
    build: .
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgres://user:secret@db:5432/mydb
      - REDIS_URL=redis://cache:6379
    depends_on:
      - db
      - cache

  db:
    image: postgres:15-alpine
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: mydb
    volumes:
      - pgdata:/var/lib/postgresql/data

  cache:
    image: redis:7-alpine
    ports:
      - "6379:6379"

volumes:
  pgdata:

Run with docker compose up. Both services start together.

Useful Compose commands:

docker compose up -d          # Start in background
docker compose down            # Stop and remove
docker compose logs -f api     # Follow logs
docker compose ps              # List services
docker compose exec db psql -U user mydb  # Shell into service

Essential Commands Cheat Sheet

CommandPurpose
docker psList running containers
docker ps -aList all containers
docker imagesList local images
docker stop <id>Stop a container
docker rm <id>Remove a container
docker rmi <image>Remove an image
docker logs <id>View container output
docker exec -it <id> bashShell into a container
docker volume lsList volumes
docker network lsList networks
docker system dfShow disk usage

Docker Networking

Docker creates virtual networks for container communication:

# Create a custom network
docker network create my-network

# Run containers on the network
docker run -d --name web --network my-network nginx
docker run -d --name app --network my-network my-app

# Containers can reach each other by name
# app can connect to web at http://web:80

Docker Volumes for Persistence

Containers are ephemeral — data is lost when they stop. Volumes persist data:

# Create a named volume
docker volume create my-data

# Use the volume
docker run -v my-data:/app/data my-app

# Backup a volume
docker run --rm -v my-data:/data -v $(pwd):/backup alpine tar czf /backup/backup.tar.gz /data

Next Steps

Start containerizing your own projects. Dockerize a simple app, then explore Docker Compose for multi-service architectures. From here, Kubernetes becomes the natural next step for orchestrating containers at scale.

Learning path: Docker basics → Docker Compose → Docker networking → Docker volumes → Kubernetes

Key Takeaways

  • Docker packages applications with dependencies into portable containers
  • Dockerfiles define how to build images with step-by-step instructions
  • Docker Compose manages multi-container applications with YAML configuration
  • Volumes persist data beyond container lifecycle
  • Docker networking enables container-to-container communication
  • Containers are the foundation for Kubernetes and modern cloud infrastructure

Try It Yourself

  • Build a Docker image for a simple Node.js or Python app
  • Create a Docker Compose file with a web server and database
  • Use Docker volumes to persist database data
  • Set up a custom Docker network for multi-container communication

Related Articles

FAQ

What is the difference between Docker and a virtual machine?

Docker containers share the host OS kernel, making them lightweight (MBs) and fast to start (seconds). Virtual machines include a full OS, making them heavy (GBs) and slow to start (minutes). Containers are better for most application deployment scenarios.

How do I persist data in Docker containers?

Use Docker volumes or bind mounts. Volumes are managed by Docker and stored in a dedicated directory. Bind mounts map a host directory into the container. Volumes are preferred for production use.

Can I run Docker on Windows?

Yes, via Docker Desktop which uses WSL 2 (Windows Subsystem for Linux). Docker Desktop provides a graphical interface and integrates with Windows development tools.

Is Docker secure by default?

Docker provides reasonable default security (namespace isolation, resource limits), but production deployments need additional measures: non-root users, read-only filesystems, network policies, and regular image updates. See Container Security Best Practices for details.

How do I reduce Docker image size?

Use multi-stage builds, choose minimal base images (alpine), combine RUN commands, and use .dockerignore to exclude unnecessary files. A typical Node.js app can go from 1GB+ to under 100MB with optimization.

Docker Basics

1 of 5

What is a Docker container?

Ready to practice?

Apply what you learned with free hands-on labs on XpertClass. Deploy real Docker sandboxes — no setup required.