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.
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.
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.
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
sudo apt update && sudo apt upgrade -yStep 2: Install Prerequisites
sudo apt install -y ca-certificates curl gnupg lsb-releaseStep 3: 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.gpgStep 4: Add the Docker 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/nullStep 5: Install Docker Engine
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStep 6: Verify the Installation
sudo docker run hello-worldStep 7: Run Docker Without Sudo (Optional)
sudo usermod -aG docker $USERLog out and back in for this to take effect. Security note: Adding a user to the docker group grants root-equivalent privileges.
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.
Essential Docker Commands
Working with Images
# 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 nginxRunning Containers
# 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 mysqlManaging Running Containers
# 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 bashWorking with Volumes
# 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-dataCleanup Commands
# 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 dfPractical Examples
Example: Nginx Serving Custom Content
mkdir -p ~/my-website
echo "<h1>Hello from Docker on RamNode!</h1>" > ~/my-website/index.htmldocker 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
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:8Resource 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 dfto monitor anddocker system pruneto clean up. - CPU: Most containers are idle most of the time—a small VPS can run many.
Recommended VPS sizing:
| Workload | Minimum RAM | Recommended |
|---|---|---|
| Single small app | 1GB | 2GB |
| 2-3 applications | 2GB | 4GB |
| Database + web app | 2GB | 4GB |
| Multiple services + monitoring | 4GB | 8GB |
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.
