Install Docker on Ubuntu VPS

📋 Table of Contents

What is Docker and Why Use It on a VPS?

Docker is a containerization platform that packages your application and all its dependencies into a lightweight, portable container. Each container runs in an isolated environment, eliminating the classic "it works on my machine" problem.

Docker vs Virtual Machine comparison:

FeatureDocker ContainerVirtual Machine
SizeLightweight (MB)Heavy (GB)
Startup timeSecondsMinutes
Resource sharingShared kernelSeparate kernel
IsolationProcess-levelHardware-level
OverheadVery lowHigh
PortabilityVery highMedium

System Requirements

💡 Note: AsiaGB VPS plans with 2GB RAM and above handle Docker workloads excellently, running multiple containers simultaneously on fast SSD storage.

Install Docker on Ubuntu 22.04/24.04

Step 1: Update packages

sudo apt update && sudo apt upgrade -y

Step 2: Install dependencies

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

Step 3: Add Docker 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 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
sudo apt update

Step 5: Install Docker Engine

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

Step 6: Verify installation

sudo docker run hello-world

Step 7: Run Docker without sudo

sudo usermod -aG docker $USER
newgrp docker

Essential Docker Commands

Managing Images

docker pull nginx          # Download image
docker images              # List all images
docker rmi nginx           # Remove image

Managing Containers

docker run -d -p 80:80 --name my-nginx nginx
docker ps                  # Running containers
docker ps -a               # All containers
docker stop my-nginx
docker start my-nginx
docker rm my-nginx

Logs and Shell Access

docker logs my-nginx
docker logs -f my-nginx    # Follow logs
docker exec -it my-nginx bash

Docker Compose

Docker Compose manages multi-container applications with a single YAML file. It is included with modern Docker CE:

docker compose version

Example docker-compose.yml for WordPress:

version: '3.8'
services:
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: wordpress
    volumes:
      - db_data:/var/lib/mysql
  wordpress:
    image: wordpress:latest
    ports:
      - "8080:80"
    depends_on:
      - db
    volumes:
      - wp_data:/var/www/html
volumes:
  db_data:
  wp_data:
docker compose up -d

Running Your First Container

docker run -d   --name my-web   -p 8080:80   --restart unless-stopped   -v /var/www/html:/usr/share/nginx/html:ro   nginx

curl http://localhost:8080
🔥 Tip: Always use --restart unless-stopped for production containers so they restart automatically after a VPS reboot.

Ports, Volumes and Networks

Port Mapping

docker run -d -p 3000:3000 my-app
docker run -d -p 127.0.0.1:5432:5432 postgres

Volumes

docker volume create my-data
docker run -d -v my-data:/data my-app
docker volume ls
docker volume prune

Networks

docker network create my-network
docker run -d --network my-network --name app1 my-app
docker run -d --network my-network --name app2 my-app

Docker Security Best Practices

⚠️ UFW + Docker: Docker bypasses UFW by modifying iptables directly. Bind containers to localhost and use Nginx as a reverse proxy for proper firewall control.

Troubleshooting Common Issues

Docker daemon not starting

sudo systemctl status docker
sudo systemctl start docker
sudo systemctl enable docker

Permission denied

sudo usermod -aG docker $USER
# Log out and back in

Port already in use

sudo lsof -i :80
docker stop CONTAINER_NAME

Disk space issues

docker system prune -a
docker volume prune

Summary

Docker dramatically simplifies application deployment on a VPS. The key steps are: install Docker Engine → add user to docker group → run containers with docker run → use Docker Compose for multi-container apps → apply security hardening. AsiaGB VPS plans provide fast SSD storage and 99% Uptime SLA for reliable Docker workloads.

Frequently Asked Questions

Q: What is the difference between Docker and Docker Desktop?
Docker Desktop is a GUI application for macOS and Windows with Docker Engine built in. Docker Engine (Docker CE) on Linux servers has no GUI and is designed for VPS and server environments via the command line.
Q: Can I run Docker on a 1GB RAM VPS?
Yes, but with limitations. You can run 2-3 lightweight containers. For WordPress or database workloads, 2GB RAM is recommended for good performance.
Q: What is the difference between a Container and a Virtual Machine?
Containers share the host OS kernel making them lightweight and fast to start. VMs have their own complete OS providing stronger isolation but consuming more resources. Docker containers excel in portability and efficiency.
Q: Will data inside a container be lost when I remove it?
Yes, container layer data is lost when the container is removed. Use Docker Volumes or Bind Mounts to persist data: -v /host/path:/container/path or named volumes.
Q: What is Docker Compose and do I need to install it separately?
Docker Compose manages multi-container applications via a YAML file. Modern Docker CE includes the Compose Plugin, so you can use docker compose without separate installation.
Q: How should I use Docker with Nginx as a reverse proxy?
Bind your container to localhost (-p 127.0.0.1:PORT:PORT), then configure Nginx to proxy from port 80/443 to that localhost port. This provides proper firewall control and enables SSL/TLS via Let's Encrypt.