Part 1 of 7

    Docker Fundamentals on a VPS

    Master the basics of containerization—installation, images, containers, volumes, and essential commands for self-hosting applications.

    Containers
    Images
    Volumes

    Docker has become the standard way to package and deploy applications. Whether you're self-hosting a media server, running a web application, or experimenting with AI tools, understanding Docker gives you a consistent, reproducible way to manage software on your VPS.

    1

    What is Docker and Why Use It?

    Docker packages applications and their dependencies into isolated units called containers. Unlike virtual machines, containers share the host's kernel, making them lightweight and fast to start.

    Why this matters for VPS users:

    • Consistency: An app that works in a container on your laptop works identically on your VPS
    • Isolation: Each container runs independently—a broken app won't take down your server
    • Easy cleanup: Remove a container and everything goes with it, no leftover files
    • Resource efficiency: Containers use far less RAM and CPU than running separate VMs

    A 2GB RAM VPS can comfortably run multiple Docker containers, whereas you'd struggle to run even one VM with that much memory.

    2

    Prerequisites

    Before starting, you'll need:

    • A Linux VPS running Ubuntu 22.04 or 24.04 (Debian 11/12 also works)
    • SSH access with sudo privileges
    • At least 1GB of RAM (2GB+ recommended for running multiple containers)

    The examples in this guide were tested on a RamNode VPS with 2GB RAM running Ubuntu 24.04.

    3

    Installing Docker

    Docker offers two editions: Docker Engine (free, what we'll install) and Docker Desktop (GUI-based, not suitable for headless servers).

    Step 1: Update Your System

    Update packages
    sudo apt update && sudo apt upgrade -y

    Step 2: Install Prerequisites

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

    Step 3: Add Docker's Official GPG Key

    Add 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 4: Add the Docker Repository

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

    Step 5: Install Docker Engine

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

    Step 6: Verify the Installation

    Test Docker
    sudo docker run hello-world

    Step 7: Run Docker Without Sudo (Optional)

    Add user to docker group
    sudo usermod -aG docker $USER

    Log out and back in for this to take effect. Security note: Adding a user to the docker group grants root-equivalent privileges.

    4

    Core Docker Concepts

    Images

    An image is a read-only template containing everything needed to run an application: code, runtime, libraries, and configuration. Images are pulled from registries like Docker Hub.

    Containers

    A container is a running instance of an image. You can run multiple containers from the same image, each isolated from the others.

    Volumes

    Containers are ephemeral—when you remove one, data inside is lost. Volumes provide persistent storage that survives container restarts and removal.

    Networks

    Docker creates isolated networks for containers. Containers on the same network can communicate using container names as hostnames.

    5

    Essential Docker Commands

    Working with Images

    Image commands
    # Search for images on Docker Hub
    docker search nginx
    
    # Pull an image
    docker pull nginx
    docker pull nginx:1.25    # Pull a specific version (tag)
    
    # List downloaded images
    docker images
    
    # Remove an image
    docker rmi nginx

    Running Containers

    Container commands
    # Run a container (foreground)
    docker run nginx
    
    # Run in detached mode (background)
    docker run -d nginx
    
    # Run with a custom name
    docker run -d --name my-webserver nginx
    
    # Run with port mapping
    docker run -d -p 8080:80 nginx
    
    # Run with environment variables
    docker run -d -e MYSQL_ROOT_PASSWORD=secret mysql

    Managing Running Containers

    Management commands
    # List running containers
    docker ps
    
    # List all containers (including stopped)
    docker ps -a
    
    # Stop, start, restart a container
    docker stop my-webserver
    docker start my-webserver
    docker restart my-webserver
    
    # Remove a container
    docker rm my-webserver           # Must be stopped first
    docker rm -f my-webserver        # Force remove running container
    
    # View container logs
    docker logs my-webserver
    docker logs -f my-webserver      # Follow logs in real-time
    
    # Execute a command inside a running container
    docker exec -it my-webserver bash
    6

    Working with Volumes

    Volume commands
    # Create a named volume
    docker volume create my-data
    
    # Run a container with a volume
    docker run -d -v my-data:/var/lib/mysql mysql
    
    # Bind mount a host directory
    docker run -d -v /home/user/website:/usr/share/nginx/html nginx
    
    # List volumes
    docker volume ls
    
    # Remove a volume
    docker volume rm my-data

    Cleanup Commands

    Cleanup
    # Remove all stopped containers
    docker container prune
    
    # Remove unused images
    docker image prune        # Dangling images only
    docker image prune -a     # All unused images
    
    # Remove unused volumes
    docker volume prune
    
    # Remove everything unused
    docker system prune -a --volumes
    
    # Check disk usage
    docker system df
    7

    Practical Examples

    Example: Nginx Serving Custom Content

    Create website directory
    mkdir -p ~/my-website
    echo "<h1>Hello from Docker on RamNode!</h1>" > ~/my-website/index.html
    Run Nginx with your content
    docker run -d \
      --name my-site \
      -p 80:80 \
      -v ~/my-website:/usr/share/nginx/html:ro \
      --restart unless-stopped \
      nginx

    -d: Run in background

    --name my-site: Give it a memorable name

    -p 80:80: Map port 80 on the VPS to port 80 in the container

    -v ~/my-website:/usr/share/nginx/html:ro: Mount your content (read-only)

    --restart unless-stopped: Auto-restart on crash or reboot

    Example: MySQL with Persistent Storage

    Run MySQL
    docker run -d \
      --name my-database \
      -e MYSQL_ROOT_PASSWORD=your-secure-password \
      -e MYSQL_DATABASE=myapp \
      -v mysql-data:/var/lib/mysql \
      -p 3306:3306 \
      --restart unless-stopped \
      mysql:8
    8

    Resource Considerations for VPS

    Docker containers share the host's resources. Here's what to keep in mind:

    • Memory: Check usage with docker stats. On a 2GB VPS, budget ~512MB for the OS.
    • Disk space: Use docker system df to monitor and docker system prune to clean up.
    • CPU: Most containers are idle most of the time—a small VPS can run many.

    Recommended VPS sizing:

    WorkloadMinimum RAMRecommended
    Single small app1GB2GB
    2-3 applications2GB4GB
    Database + web app2GB4GB
    Multiple services + monitoring4GB8GB
    9

    Troubleshooting Common Issues

    Container exits immediately

    Check the logs: docker logs container-name. Common causes: missing environment variables or configuration errors.

    Port already in use

    Check with sudo lsof -i :PORT or choose a different port mapping.

    Permission denied on mounted volume

    The container user may not have permission to access the mounted directory. Check ownership and permissions on the host.

    Out of disk space

    Run docker system prune -a to remove unused data.

    Can't connect to container from outside

    Verify port mapping with docker ps and check your firewall (sudo ufw allow PORT).

    What's Next

    You now have Docker installed and understand the fundamental concepts: images, containers, volumes, and basic commands. In Part 2, we'll explore Docker Compose, which lets you define and run multi-container applications with a single configuration file—essential for deploying real-world applications.