Docker Basics for Developers

What You Will Learn

Docker packages applications with their runtime environment so they run consistently across machines. This guide explains images, containers, Dockerfiles, volumes, networks, and Compose.

Prerequisites

  • Basic terminal commands
  • One application you can run locally
  • Docker Desktop or Docker Engine installed

Concept Overview

An image is a packaged application template. A container is a running instance of an image. A Dockerfile describes how to build the image.

Step-by-Step Explanation

  1. Write a Dockerfile for your application.
  2. Build an image using docker build.
  3. Run a container using docker run.
  4. Use environment variables for configuration.
  5. Mount volumes for persistent or local development data.
  6. Use Docker networks so containers can communicate.
  7. Use Docker Compose for multi-container local environments.

Code Example

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

FROM nginx:1.27-alpine
COPY --from=build /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Real-World Use Cases

  • Running Java APIs locally
  • Packaging React builds
  • Running databases for development
  • CI/CD build environments
  • Production container deployment

Best Practices

  • Use small base images.
  • Keep secrets out of images.
  • Use .dockerignore.
  • Run one main process per container.
  • Pin important image versions.
  • Scan images for vulnerabilities.

Common Mistakes

  • Copying node_modules or build output accidentally
  • Running containers as root without need
  • Hardcoding secrets
  • Making images too large
  • Confusing build-time and runtime variables

Interview Questions

  • What is the difference between an image and a container?
  • What is a Docker volume?
  • Why use Docker Compose?
  • What is the purpose of .dockerignore?
  • How do containers communicate?

Summary

Docker gives teams repeatable environments. Learn images, containers, volumes, networks, and Compose before moving into Kubernetes or ECS.