Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Docker Setup

Overview

Docker is a platform for running applications in isolated containers. A container packages an application with all its dependencies, ensuring it runs the same way everywhere.

┌─────────────────────────────────────────────────────┐
│                    Your VPS                         │
│                                                     │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐  │
│  │ Container 1 │  │ Container 2 │  │ Container 3 │  │
│  │   Node.js   │  │  PostgreSQL │  │    Redis    │  │
│  │   :3000     │  │   :5432     │  │   :6379     │  │
│  └─────────────┘  └─────────────┘  └─────────────┘  │
│                                                     │
│  ┌───────────────────────────────────────────────┐  │
│  │              Docker Engine                    │  │
│  └───────────────────────────────────────────────┘  │
│                                                     │
│  ┌───────────────────────────────────────────────┐  │
│  │              Linux Kernel                     │  │
│  └───────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────┘

Benefits:

  • Consistent environments (no “works on my machine” issues)
  • Easy deployment and rollback
  • Isolation between applications
  • Simple dependency management

Prerequisites

Installation

Follow the official Docker installation guide: https://docs.docker.com/engine/install/

Run Docker Without sudo

By default, Docker requires root privileges. Add your user to the docker group to run commands without sudo:

sudo usermod -aG docker $USER

Log out and back in for the change to take effect, or run:

newgrp docker

Verify it works:

docker run hello-world

This downloads a test image and runs it. If you see “Hello from Docker!”, everything is working.

Docker Concepts

Images vs Containers

ConceptDescription
ImageA read-only template containing the application and dependencies
ContainerA running instance of an image

Think of an image as a class and a container as an object. You can run multiple containers from the same image.

Common Commands

Images:

docker images                    # List downloaded images
docker pull nginx                # Download an image
docker rmi nginx                 # Remove an image

Containers:

docker ps                        # List running containers
docker ps -a                     # List all containers (including stopped)
docker run -d nginx              # Run container in background
docker stop <container-id>       # Stop a container
docker rm <container-id>         # Remove a container
docker logs <container-id>       # View container logs
docker exec -it <id> bash        # Open shell inside container

Running a Container

Basic example - run nginx web server:

docker run -d -p 8080:80 --name my-nginx nginx
FlagPurpose
-dRun in background (detached mode)
-p 8080:80Map host port 8080 to container port 80
--name my-nginxGive the container a name
nginxThe image to use

Visit http://<vps-ip>:8080 to see the nginx welcome page.

Stop and remove when done:

docker stop my-nginx
docker rm my-nginx

Cleanup

Docker can accumulate unused data. Clean up periodically:

docker system prune              # Remove unused containers, networks, images
docker system prune -a           # Also remove unused images
docker volume prune              # Remove unused volumes

Check disk usage:

docker system df