DevOps··8 min read

How to Install Docker: A Complete Beginner's Guide

A complete guide to installing Docker on Ubuntu, macOS, and Windows. Learn Docker Engine setup, Docker Compose, essential commands, and best practices for containerized development.

Ram

Docker logo with container illustration and terminal commands
Share

Docker has revolutionized how developers build, ship, and run applications. By packaging software into lightweight containers, Docker eliminates the "it works on my machine" problem and makes deployments consistent across environments. This guide walks you through installing Docker on every major platform and getting started with your first container.

What is Docker?

Docker is a platform that uses containerization to package applications and their dependencies into isolated, portable units called containers. Unlike virtual machines, containers share the host OS kernel, making them significantly lighter and faster.

Key benefits:
  • Consistency — same environment from development to production
  • Isolation — each container runs independently
  • Portability — runs anywhere Docker is installed
  • Efficiency — uses fewer resources than VMs
  • Speed — containers start in seconds

Prerequisites

Before installing Docker, ensure you have:

  • A 64-bit operating system
  • At least 4 GB RAM (8 GB recommended)
  • Virtualization enabled in BIOS (for Windows/macOS)
  • Admin/sudo access on your machine
  • A stable internet connection

Installing Docker on Ubuntu/Debian

Step 1: Remove Old Versions

# Remove any existing Docker packages
sudo apt remove -y docker docker-engine docker.io containerd runc 2>/dev/null

Step 2: Set Up the Repository

# Update package index
sudo apt update

Install prerequisites

sudo apt install -y ca-certificates curl gnupg lsb-release

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

Add the Docker 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 3: Install Docker Engine

# Update package index with the new repo
sudo apt update

Install Docker Engine, CLI, and plugins

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

Step 4: Post-Installation Setup

# Add your user to the docker group (so you don't need sudo)
sudo usermod -aG docker $USER

Apply the group change (or log out and back in)

newgrp docker

Enable Docker to start on boot

sudo systemctl enable docker sudo systemctl start docker

Step 5: Verify Installation

# Check Docker version
docker --version

Run a test container

docker run hello-world

You should see a message confirming Docker is working correctly.

Installing Docker on macOS

Using Docker Desktop

  1. Visit docker.com/products/docker-desktop
  2. Download Docker Desktop for Mac
- Choose Apple Silicon for M1/M2/M3/M4 Macs - Choose Intel for older Macs
  1. Open the .dmg file and drag Docker to Applications
  2. Launch Docker from Applications
  3. Follow the setup wizard and grant necessary permissions

Using Homebrew (Alternative)

# Install Docker Desktop via Homebrew
brew install --cask docker

Launch Docker Desktop

open /Applications/Docker.app

Verify on macOS

docker --version
docker compose version
docker run hello-world

Installing Docker on Windows

Prerequisites for Windows

  • Windows 10/11 (64-bit, Pro, Enterprise, or Education)
  • WSL 2 enabled (recommended backend)
  • Hyper-V and Containers Windows features enabled

Step 1: Enable WSL 2

Open PowerShell as Administrator:

# Enable WSL
wsl --install

Restart your computer

After restart, set WSL 2 as default

wsl --set-default-version 2

Step 2: Install Docker Desktop

  1. Download Docker Desktop for Windows
  2. Run the installer
  3. Select Use WSL 2 instead of Hyper-V (recommended)
  4. Follow the installation wizard
  5. Restart your computer when prompted

Step 3: Configure Docker Desktop

  1. Open Docker Desktop
  2. Go to SettingsGeneral
  3. Ensure Use the WSL 2 based engine is checked
  4. Under ResourcesWSL Integration, enable your distro

Verify on Windows

Open PowerShell or Command Prompt:

docker --version
docker compose version
docker run hello-world

Installing Docker Compose

Docker Compose is included with Docker Desktop (macOS/Windows). On Linux, it's installed as a plugin with the steps above. Verify it:

docker compose version

If you need to install it separately on Linux:

# Install the Compose plugin
sudo apt install -y docker-compose-plugin

Verify

docker compose version

Essential Docker Commands

Here's a quick reference of commands you'll use daily:

Container Management

# Run a container
docker run -d --name myapp -p 8080:80 nginx

List running containers

docker ps

List all containers (including stopped)

docker ps -a

Stop a container

docker stop myapp

Start a stopped container

docker start myapp

Remove a container

docker rm myapp

View container logs

docker logs -f myapp

Execute a command inside a running container

docker exec -it myapp bash

Image Management

# Pull an image
docker pull node:20-alpine

List downloaded images

docker images

Remove an image

docker rmi node:20-alpine

Build an image from a Dockerfile

docker build -t myapp:latest .

Docker Compose Commands

# Start services defined in docker-compose.yml
docker compose up -d

Stop services

docker compose down

View logs

docker compose logs -f

Rebuild and start

docker compose up -d --build

Your First Dockerfile

Create a simple Node.js application with Docker:

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

Set the working directory

WORKDIR /app

Copy package files first (for better caching)

COPY package*.json ./

Install dependencies

RUN npm ci --only=production

Copy application code

COPY . .

Expose the port your app runs on

EXPOSE 3000

Start the application

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

Build and run it:

# Build the image
docker build -t my-node-app .

Run the container

docker run -d -p 3000:3000 --name my-app my-node-app

Check it's running

curl http://localhost:3000

Your First Docker Compose File

Create a docker-compose.yml for a multi-service application:

services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgres://user:pass@db:5432/mydb
    depends_on:
      - db

db: image: postgres:16-alpine environment: POSTGRES_USER: user POSTGRES_PASSWORD: pass POSTGRES_DB: mydb volumes: - pgdata:/var/lib/postgresql/data ports: - "5432:5432"

volumes: pgdata:

# Start both services
docker compose up -d

Check status

docker compose ps

View logs from all services

docker compose logs -f

Docker Best Practices

1. Use Specific Image Tags

# Bad — "latest" can change unexpectedly
FROM node:latest

Good — pinned version

FROM node:20-alpine

2. Use Multi-Stage Builds

# 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 CMD ["node", "dist/server.js"]

3. Use .dockerignore

Create a .dockerignore file to exclude unnecessary files:

node_modules
.git
.env
*.md
.DS_Store

4. Don't Run as Root

# Create a non-root user
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser

5. Use Health Checks

HEALTHCHECK --interval=30s --timeout=3s \
  CMD wget --quiet --tries=1 --spider http://localhost:3000/health || exit 1

Troubleshooting Common Issues

Permission Denied

# If you get "permission denied" errors
sudo usermod -aG docker $USER

Log out and back in, or run:

newgrp docker

Docker Daemon Not Running

# Check Docker service status
sudo systemctl status docker

Start Docker

sudo systemctl start docker

Enable auto-start

sudo systemctl enable docker

Port Already in Use

# Find what's using the port
sudo lsof -i :8080

Kill the process or use a different port

docker run -p 8081:80 nginx

Disk Space Issues

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

Check Docker disk usage

docker system df

Frequently Asked Questions

Docker Desktop vs Docker Engine — what's the difference?

Docker Engine is the core runtime (CLI + daemon) — it's free and open source, available on Linux. Docker Desktop is a GUI application that bundles Docker Engine with extras like Kubernetes, a dashboard, and easier setup — available for macOS, Windows, and Linux.

Is Docker free?

Docker Engine is completely free. Docker Desktop is free for personal use, education, and small businesses (fewer than 250 employees and less than $10M revenue). Larger organizations need a paid subscription.

Can I run Docker on Windows Home?

Yes! Docker Desktop supports Windows Home via WSL 2. Enable WSL 2 first, then install Docker Desktop normally.

How is Docker different from a virtual machine?

Containers share the host OS kernel and start in seconds. VMs run a full guest OS, require a hypervisor, and use more resources. Containers are lighter but less isolated than VMs.

Should I use Docker for development or production?

Both! Use Docker in development for consistent environments, and in production for reliable deployments. Pair it with orchestration tools like Kubernetes or Docker Swarm for production scale.

Conclusion

Docker is an essential tool for modern development. You've installed Docker, learned the core commands, created your first Dockerfile and Compose file, and picked up best practices. Start containerizing your projects and experience the consistency and portability that Docker brings to your workflow.

Happy containerizing! 🐳

Share

Related Articles

Stay Updated

Get the latest articles delivered straight to your inbox. No spam, ever.