Get in Touch
Back to Documentation

Docker Setup Guide

A comprehensive guide to installing and configuring Docker for containerized application deployment. Learn the fundamentals of Docker, docker-compose, and essential best practices for production environments.

Introduction to Docker

Docker is a platform that enables developers to package applications and their dependencies into standardized containers. These containers can run consistently across different computing environments, eliminating the common "it works on my machine" problem.

Containers are lightweight, portable, and self-sufficient units that include everything needed to run an application: code, runtime, system tools, libraries, and settings. Unlike virtual machines, containers share the host system's kernel, making them more efficient and faster to start.

Why Use Docker?

  • Consistency: Applications run the same way in development, testing, and production environments
  • Isolation: Each container runs independently, preventing conflicts between applications
  • Efficiency: Containers use fewer resources than traditional virtual machines
  • Scalability: Easy to scale applications up or down based on demand
  • Version Control: Docker images can be versioned and rolled back if needed

Prerequisites

Before installing Docker, ensure your system meets the following requirements:

Operating System Requirements

Windows

  • Windows 10 64-bit: Pro, Enterprise, or Education (Build 19041 or higher)
  • Windows 11 64-bit: Pro, Enterprise, or Education
  • WSL 2 feature enabled (Windows Subsystem for Linux)
  • Hardware virtualization enabled in BIOS

macOS

  • macOS 10.15 or newer (Catalina, Big Sur, Monterey, Ventura, or Sonoma)
  • At least 4GB of RAM
  • VirtualBox prior to version 4.3.30 must NOT be installed

Linux

  • 64-bit version of one of these distributions:
    • Ubuntu 20.04 LTS or newer
    • Debian 10 or newer
    • CentOS 8 or newer
    • Fedora 33 or newer
  • Kernel version 3.10 or higher

Installation

Installing Docker on Windows

Windows users should install Docker Desktop, which includes Docker Engine, Docker CLI, and Docker Compose.

Step 1: Enable WSL 2

Open PowerShell as Administrator and run:

wsl --install

Restart your computer when prompted.

Step 2: Download Docker Desktop

  1. Visit docker.com/products/docker-desktop
  2. Download Docker Desktop for Windows
  3. Run the installer (Docker Desktop Installer.exe)
  4. Follow the installation wizard
  5. Restart your computer if prompted

Step 3: Verify Installation

Open Command Prompt or PowerShell and run:

docker --version
docker compose version

Installing Docker on macOS

Step 1: Download Docker Desktop

  1. Visit docker.com/products/docker-desktop
  2. Download Docker Desktop for Mac (choose Intel or Apple Silicon based on your Mac)
  3. Open the downloaded .dmg file
  4. Drag Docker to your Applications folder
  5. Open Docker from Applications

Step 2: Verify Installation

Open Terminal and run:

docker --version
docker compose version

Installing Docker on Linux (Ubuntu)

Step 1: Update Package Index

sudo apt-get update
sudo apt-get install ca-certificates curl gnupg

Step 2: Add Docker's Official GPG Key

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

Step 3: Set Up Repository

echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Step 4: Install Docker Engine

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Step 5: Add Your User to Docker Group

This allows you to run Docker commands without sudo:

sudo usermod -aG docker $USER

Log out and back in for this to take effect.

Step 6: Verify Installation

docker --version
docker compose version

Basic Concepts

Docker Images

An image is a read-only template with instructions for creating a Docker container. Images are built from a Dockerfile and can be stored in registries like Docker Hub. Think of an image as a snapshot of your application and its environment.

Docker Containers

A container is a runnable instance of an image. You can create, start, stop, move, or delete containers using the Docker CLI. Containers are isolated from each other and from the host system, but can be configured to communicate when needed.

Dockerfile

A Dockerfile is a text file containing instructions to build a Docker image. Each instruction creates a layer in the image.

Example Dockerfile:

# Use an official Node.js runtime as base image
FROM node:18-alpine

# Set working directory
WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm install --production

# Copy application code
COPY . .

# Expose port
EXPOSE 3000

# Start application
CMD ["node", "server.js"]

Docker Volumes

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. Unlike bind mounts, volumes are completely managed by Docker and work on both Linux and Windows containers.

Docker Networks

Docker networks enable containers to communicate with each other and with the host system. By default, Docker provides several network drivers including bridge, host, and overlay networks.

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. You use a YAML file to configure your application's services, networks, and volumes.

Basic docker-compose.yml Structure

version: '3.9'

services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./html:/usr/share/nginx/html
    networks:
      - app-network

  database:
    image: postgres:15
    environment:
      POSTGRES_PASSWORD: secretpassword
      POSTGRES_DB: myapp
    volumes:
      - db-data:/var/lib/postgresql/data
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

volumes:
  db-data:

Docker Compose Commands

Start Services

# Start all services in background
docker compose up -d

# Start specific service
docker compose up -d web

# View logs
docker compose logs -f

Stop Services

# Stop all services
docker compose down

# Stop and remove volumes
docker compose down -v

Manage Services

# List running services
docker compose ps

# Restart services
docker compose restart

# Build or rebuild services
docker compose build

# Pull latest images
docker compose pull

Common Docker Commands

Container Management

# List running containers
docker ps

# List all containers (including stopped)
docker ps -a

# Start a container
docker start container_name

# Stop a container
docker stop container_name

# Restart a container
docker restart container_name

# Remove a container
docker rm container_name

# Remove all stopped containers
docker container prune

Image Management

# List images
docker images

# Pull an image from Docker Hub
docker pull nginx:latest

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

# Remove an image
docker rmi image_name

# Remove unused images
docker image prune

Container Inspection

# View container logs
docker logs container_name

# Follow log output
docker logs -f container_name

# View container processes
docker top container_name

# Inspect container details
docker inspect container_name

# Access container shell
docker exec -it container_name /bin/bash

System Management

# View Docker disk usage
docker system df

# Clean up unused resources
docker system prune

# Clean up everything (use with caution)
docker system prune -a --volumes

Best Practices

Image Creation

  • Use official base images from trusted sources
  • Keep images small by using alpine or slim variants
  • Minimize the number of layers in your Dockerfile
  • Use specific version tags instead of latest
  • Don't install unnecessary packages

Security

  • Never store secrets in images or environment variables
  • Use Docker secrets or external secret management tools
  • Run containers as non-root users when possible
  • Keep Docker and your images updated with security patches
  • Scan images for vulnerabilities regularly

Resource Management

  • Set memory and CPU limits for containers
  • Use health checks to monitor container status
  • Implement proper logging strategies
  • Clean up unused containers and images regularly

Tip: Use .dockerignore files to exclude files and directories from your Docker build context. This speeds up builds and reduces image size.

Development Workflow

  • Use docker-compose for local development environments
  • Mount source code as volumes during development
  • Use separate Dockerfiles for development and production
  • Tag images with meaningful version numbers

Troubleshooting

Container Won't Start

Check the container logs for error messages:

docker logs container_name

Verify the container configuration:

docker inspect container_name

Port Already in Use

If you see "port is already allocated" error:

  • Check if another container is using the port: docker ps
  • Check if a host process is using the port
  • Change the port mapping in your docker-compose.yml or docker run command

Permission Denied Errors

On Linux, if you encounter permission errors:

# Add your user to the docker group
sudo usermod -aG docker $USER

# Log out and back in for changes to take effect

Out of Disk Space

Clean up unused Docker resources:

# Remove unused containers, networks, and images
docker system prune -a

# View disk usage
docker system df

Container Networking Issues

If containers can't communicate:

  • Ensure containers are on the same network
  • Check firewall settings
  • Verify network configuration: docker network inspect network_name

Warning: The docker system prune -a --volumes command removes ALL unused containers, networks, images, and volumes. Use this with caution in production environments.

Getting Help

For additional support and documentation: