Docker for Developers: A Complete Tutorial

Docker for Developers: A Complete Tutorial

By Fatima Al-Hassan, Security & Privacy Editorial Desk · January 8, 2026 · Updated November 7, 2026 · 4 min read

Updated November 7, 2026: Added links to Docker's official documentation for multi-stage builds, volumes, layer caching and Compose.

Quick Answer

Docker packages an application and its dependencies into containers that run the same way on a laptop or a production server. Developers need four core ideas: images as blueprints, containers as running instances, Dockerfiles as build instructions, and docker-compose for multi-container apps. Volumes keep data persistent, and multi-stage builds shrink production images.

Introduction

Docker has become essential for modern software development. It packages applications and dependencies into containers that run consistently across any environment—from your laptop to production servers.

This tutorial covers Docker fundamentals through advanced patterns, giving you the knowledge to containerize applications and run them in production.

What Is Docker?

Docker is a containerization platform that:

  • Packages applications: Bundles code with all dependencies
  • Ensures consistency: Same behavior everywhere
  • Isolates processes: Containers are separate from each other
  • Enables portability: Run on any system with Docker

Key Concepts

Image: A blueprint containing application code, runtime, and dependencies

Container: A running instance of an image

Dockerfile: Instructions for building an image

Registry: Storage for images (like Docker Hub)

Installation

Install Docker Desktop for macOS or Windows. For Linux, install Docker Engine directly.

Verify installation by running the hello-world image.

Basic Commands

Running Containers

Run a container from an image. Use flags for common options:

  • -d: Run in background (detached)
  • -p: Map ports (host:container)
  • --name: Name your container
  • -v: Mount volumes

Managing Containers

List running containers to see what is active. List all containers including stopped ones.

Stop containers gracefully. Remove containers you no longer need.

Working with Images

Pull images from registries. List local images. Remove unused images to save space.

Dockerfile Basics

A Dockerfile defines how to build your image.

Basic Structure

Start with a base image using FROM. Set the working directory with WORKDIR. Copy files with COPY. Run commands with RUN.

Define the startup command with CMD.

Example Node.js Dockerfile

A typical Node.js Dockerfile:

  1. Uses a Node.js base image
  2. Sets the working directory
  3. Copies package files first (for caching)
  4. Installs dependencies
  5. Copies application code
  6. Exposes the port
  7. Defines the start command

Layer Caching

Docker caches each layer, and if a layer changes, all layers after it are also affected. Order commands to maximize cache hits:

  • Put rarely changing commands first
  • Put frequently changing commands last
  • Copy dependency files before source code

docker-compose

docker-compose orchestrates multi-container applications.

Basic Configuration

Create a docker-compose.yml file defining your services. Each service can specify:

  • Image or build context
  • Port mappings
  • Volume mounts
  • Environment variables
  • Dependencies on other services

Common Commands

Start all services with up. Use -d for background mode. Stop services with down. View logs with logs.

Rebuild images with build.

Example: Web App with Database

Define a web service and database service. The web service depends on the database and connects via the service name. Use volumes to persist database data.

Volumes

Volumes persist data outside containers, and Docker documents them as its preferred mechanism for doing so.

Types of Volumes

Named volumes: Docker-managed, best for databases

Bind mounts: Map host directories, good for development

tmpfs: In-memory, for sensitive temporary data

Using Volumes

Create named volumes in docker-compose or with docker volume create. Mount volumes in your container configuration.

For development, bind mount your source code for live reloading.

Networking

Containers communicate through networks.

Default Network

docker-compose creates a default network. Services can reach each other by service name.

Custom Networks

Create custom networks for isolation. Specify which network each service uses.

Port Mapping

Map container ports to host ports for external access. Only expose ports you need publicly.

Multi-Stage Builds

Multi-stage builds create smaller production images.

The Problem

Development images include build tools and dev dependencies. Production only needs the runtime.

The Solution

Use multiple FROM statements. Build in the first stage. Copy only necessary files to the final stage.

Benefits:

  • Smaller images
  • Faster deployments
  • Fewer vulnerabilities

Best Practices

Security

  • Use official base images
  • Run as non-root user
  • Scan images for vulnerabilities
  • Do not include secrets in images
  • Keep images updated

Performance

  • Minimize layers
  • Use .dockerignore to exclude files
  • Leverage layer caching
  • Choose appropriate base images

Development

  • Use docker-compose for local development
  • Bind mount source code for live reloading
  • Use consistent environments with team
  • Document your Docker setup

Common Patterns

Hot Reloading

Mount source code as a volume and run the development server for automatic reloading when code changes.

Database with Seed Data

Create an initialization script that runs on first container start. Mount it in the appropriate directory for your database.

Multiple Environments

Use environment variables to configure different environments. Create separate compose files or use override files.

Debugging

View Container Logs

Check logs for errors and output. Follow logs in real time with -f flag.

Execute Commands in Container

Open a shell in a running container. This is useful for debugging and inspection.

Inspect Configuration

View container details including network settings, mounts, and environment. If a container uses far more memory than expected, see our guide to fixing Docker memory leaks.

Conclusion

Docker simplifies development, testing, and deployment by providing consistent, isolated environments. Start with simple Dockerfiles, use docker-compose for local development, and apply multi-stage builds for production.

The learning curve is worth it—Docker skills are essential for modern development and operations. Practice with your own projects to build proficiency.

Sources

The primary sources this article cites, checked in November 2026:

Key Takeaways

  • Containers package applications with all dependencies
  • Dockerfile defines how to build your application image
  • docker-compose orchestrates multi-container applications
  • Volumes persist data outside containers
  • Use multi-stage builds for smaller production images
  • Layer caching speeds up builds significantly

Frequently Asked Questions

What is the difference between Docker and virtual machines?

Virtual machines run a complete operating system with their own kernel, which consumes significant resources. Docker containers share the host kernel, so they are much lighter and faster to start. Containers suit application deployment, while VMs are better when you need to run different operating systems. Containers are also less isolated than VMs.

Should I use Docker for development?

Yes. Docker gives you consistent environments across development, testing and production, which eliminates works-on-my-machine problems and makes onboarding easier. Use docker-compose to run databases and services locally, bind mount your source code for live reloading, and document your Docker setup so the whole team shares the same environment.

Is Docker secure?

Docker isolates containers from each other, but containers are not as isolated as virtual machines. Follow the basics: use official base images, run as a non-root user, scan images for vulnerabilities, keep images updated and never include secrets in images. For sensitive workloads, limit container capabilities and consider additional isolation measures.

What is the difference between a Docker image and a container?

An image is a blueprint containing application code, runtime and dependencies, while a container is a running instance of that image. Images are built from a Dockerfile and stored in registries like Docker Hub. You can start many containers from the same image, each running independently and isolated from the others.

What is docker-compose used for?

docker-compose defines and orchestrates multi-container applications from a single docker-compose.yml file. Each service can specify an image or build context, port mappings, volumes, environment variables and dependencies on other services. You start everything with up and stop it with down, and services on the default network reach each other by service name.

About the Author

Fatima Al-Hassan avatar

Fatima Al-Hassan

Security & Privacy Editorial Desk

Security & Privacy Editorial Desk · Web3AIBlog

Fatima Al-Hassan is a pen name for our security and privacy editorial desk. Posts under this byline are written and reviewed by contributors with backgrounds in application security, smart contract auditing, threat modeling, and privacy-preserving cryptography. The desk specializes in attacker-perspective explainers — how exploits actually work, what real recoveries look like, and which defenses survive contact with sophisticated adversaries. We coordinate disclosures responsibly and publish nothing that helps active attackers.