DevOpsbeginner15 min read

Docker Fundamentals

Get started with Docker containers. Learn images, containers, volumes, networking, and how to write a Dockerfile from scratch.

What is Docker?

Docker packages applications and their dependencies into lightweight, portable containers. Unlike virtual machines, containers share the host OS kernel, making them fast to start and efficient with resources.

Key concepts: - Image — a read-only template with your app, runtime, and dependencies - Container — a running instance of an image - Registry — a repository for images (Docker Hub, GitHub Container Registry)

Writing a Dockerfile

A Dockerfile defines how to build an image step by step:

FROM node:22-alpine
WORKDIR /app

# Copy dependency manifests first for better caching
COPY package.json package-lock.json ./
RUN npm ci --production

# Copy application code
COPY . .

EXPOSE 3000
CMD ["node", "server.js"]

Essential Docker Commands

These commands cover 90% of daily Docker usage:

# Build an image from a Dockerfile
docker build -t myapp:latest .

# Run a container
docker run -d -p 3000:3000 --name myapp myapp:latest

# List running containers
docker ps

# View container logs
docker logs -f myapp

# Stop and remove a container
docker stop myapp && docker rm myapp

# Remove unused images
docker image prune -a

Volumes and Persistence

Containers are ephemeral — when they stop, their filesystem changes are lost. Volumes solve this by mounting host directories or managed storage into containers:

  • Named volumes — Docker manages the storage location. Best for databases.
  • Bind mounts — maps a specific host path into the container. Best for development.
  • tmpfs mounts — stored in memory only, never written to disk.
# Named volume
docker run -v pgdata:/var/lib/postgresql/data postgres:16

# Bind mount for development
docker run -v $(pwd)/src:/app/src myapp:latest

Related Tutorials