This site contains affiliate links — we may earn a commission if you sign up. Disclosure

Docker on VPS: Complete Installation and Usage Guide

Docker on VPS: Complete Installation and Usage Guide

What is Docker and Why Use It on VPS

Docker is a containerization technology that allows you to package your application along with all its dependencies, libraries, and configuration into a standardized unit called a container. Unlike traditional deployments where applications run directly on the operating system, Docker containers provide isolated environments where each application runs independently without interfering with others.

Using Docker on VPS brings significant advantages for modern development and deployment workflows:

Docker differs fundamentally from virtual machines. While a VM requires a complete operating system for each instance, consuming gigabytes of storage and significant CPU resources, Docker containers share the host OS kernel. This makes Docker significantly more efficient and cost-effective for running multiple applications on a single VPS.

VPS Requirements and System Specifications for Docker

Running Docker successfully requires a VPS with adequate system resources and proper configuration. The specific requirements depend on how many containers you plan to run and their resource demands, but here are the baseline recommendations:

If you plan to run just one or two lightweight containers, a 2GB RAM VPS with 2 CPU cores might work. However, for production use or multiple services, investing in a VPS with 4+ GB RAM, 4+ CPU cores, and 100+ GB SSD storage provides better stability and room for growth.

Installing Docker Engine on Ubuntu Step by Step

Docker installation on Ubuntu is straightforward using the official Docker repository. Follow these steps to get Docker up and running on your VPS:

Step 1: Update System Packages

First, update your system to ensure all packages are current:

sudo apt-get update
sudo apt-get upgrade -y

Step 2: Install Prerequisites

Install required packages that Docker needs:

sudo apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release

Step 3: Add Docker GPG Key and Repository

Add Docker's official GPG key and repository:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Step 4: Install Docker Engine

Update package index and install Docker:

sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io

Step 5: Verify Installation and Run First Container

Test Docker installation by running the hello-world image:

sudo docker run hello-world

To manage Docker without sudo, add your user to the docker group (optional but recommended):

sudo usermod -aG docker $USER
newgrp docker

Verify Docker is running by checking its status:

sudo systemctl status docker

Enable Docker to start automatically on system boot:

sudo systemctl enable docker

Docker Compose: Managing Multi-Container Applications

Docker Compose is a tool that simplifies running multiple interconnected Docker containers as a single application. Instead of managing each container individually, Docker Compose uses a YAML configuration file (docker-compose.yml) to define all services, networks, and volumes in one place.

Installing Docker Compose

Install Docker Compose using pip or download the binary directly:

sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version

Creating a docker-compose.yml File

A typical docker-compose.yml for a web application with a database might look like:

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./html:/usr/share/nginx/html
    depends_on:
      - db
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: password
    volumes:
      - db_data:/var/lib/mysql
volumes:
  db_data:

Starting and Managing Services

Start all services defined in the compose file:

docker-compose up -d

View running services:

docker-compose ps

View logs from a specific service:

docker-compose logs web

Stop all services:

docker-compose down

Docker Compose automatically creates a network connecting all services, so containers can communicate by service name. This eliminates the need to manually manage networking and makes multi-container setups much more maintainable.

Managing Docker Containers, Images, and Persistent Data Volumes

Effective container management includes understanding how to work with images, containers, and data volumes. These are fundamental concepts for running Docker in production.

Docker Images and Containers

A Docker image is a read-only blueprint that contains everything needed to run an application. A container is a running instance of an image. You can think of images as classes and containers as objects in programming.

docker images - List all images on your system
docker ps -a - List all containers (running and stopped)
docker run -d --name mycontainer nginx:latest - Create and start a container

Data Persistence with Volumes

Containers are ephemeral by default; when stopped, all data inside is lost. Volumes provide persistent storage that survives container restarts and can be shared between containers:

docker volume create mydata - Create a named volume
docker run -v mydata:/app/data nginx:latest - Mount volume into container
docker volume ls - List all volumes

Container Lifecycle Management

Cleaning Up Unused Resources

Docker accumulates unused images and containers over time. Clean up with:

docker system prune -a - Remove all unused containers, images, and volumes
docker image prune - Remove dangling images
docker volume prune - Remove unused volumes

Using Nginx as a Reverse Proxy with Docker Containers

Running Nginx as a reverse proxy in front of Docker containers provides benefits like load balancing, SSL/TLS termination, and routing traffic to the correct container. This is a common pattern in production deployments.

Setting Up Nginx Reverse Proxy

Create an Nginx configuration file for reverse proxying to Docker containers:

upstream backend {
  server web1:3000;
  server web2:3000;
}

server {
  listen 80;
  server_name example.com;
  
  location / {
    proxy_pass http://backend;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }
}

Docker Networking

Containers communicate via Docker networks. When using Docker Compose, services are automatically added to a network and can reach each other by service name. For manual Docker networks:

docker network create mynetwork - Create a custom network
docker run --network mynetwork --name web nginx:latest - Connect container to network

Docker Compose Setup with Nginx

In docker-compose.yml, define Nginx service with other containers on the same network:

services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - web1
      - web2
  web1:
    image: myapp:latest
  web2:
    image: myapp:latest

Health Checks and Load Balancing

Configure health checks to ensure traffic only goes to healthy containers:

healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
  interval: 30s
  timeout: 10s
  retries: 3

Nginx automatically handles load balancing across multiple backend servers, distributing requests evenly and skipping unhealthy servers.

Docker Security Best Practices and Hardening

Security should be a primary concern when running Docker in production. Container security involves securing the host system, images, containers, and network communications. Here are essential security practices:

Run Containers as Non-Root Users

Never run containers as root. Create a user in your Dockerfile:

RUN useradd -m -s /bin/bash appuser
USER appuser

This limits damage if a container is compromised.

Resource Limits and Security Constraints

Set resource limits to prevent containers from consuming excessive system resources and impacting other containers or the host:

docker run -m 512m --memory-swap 1g --cpus 1 image_name

Use the --cap-drop flag to remove unnecessary Linux capabilities that containers don't need for operation.

Image Security and Scanning

Network Security

Isolate containers using custom Docker networks instead of using host network mode. Define firewall rules on the VPS to restrict traffic to only necessary ports. Use HTTPS for all external communications and SSL/TLS certificates on your reverse proxy.

Container Registry Security

If using a private registry, ensure it's protected with strong authentication. Use image signing and verification to prevent unauthorized images from running. Regularly audit what images are in use and remove unnecessary ones.

Regular Updates and Monitoring

Keep Docker Engine, Docker Compose, and the host OS updated with latest security patches. Monitor container activity for suspicious behavior and implement logging to track container operations. Consider using AsiaGB.com for your VPS hosting, which provides secure infrastructure with SSD storage, DirectAdmin control panel, 99% uptime reliability, and 24-hour Thai support to help secure your Docker deployments.

Selecting a suitable VPS is crucial for a successful Docker deployment. The right VPS provider should offer adequate resources, reliable performance, and good support. Here are factors to consider when choosing a VPS for Docker:

Key Requirements for Docker VPS

Why AsiaGB.com is Ideal for Docker Hosting

AsiaGB.com is an excellent choice for hosting Docker applications in Thailand and Southeast Asia. Their VPS plans come with SSD storage for optimal container performance, DirectAdmin control panel for easy management, and reliable 99% uptime guarantee. All servers are located in Thailand and Singapore with 24-hour Thai support, ensuring you have help whenever you need it. Their infrastructure is designed for high performance with excellent network connectivity in the region.

Comparison Considerations

When evaluating VPS providers, compare not just price but also the quality of hardware, network infrastructure, backup options, and support responsiveness. For Docker workloads, a slightly more expensive plan from a reliable provider is better than a cheaper option that suffers from performance issues or poor support.

Start with a moderate plan and scale up as your application grows. Most providers allow easy upgrades, so you can begin with 4 GB RAM and 4 CPU cores, then move to larger configurations if needed.

RecommendedAsiaGB.com — the hosting & VPS we use and recommend: servers in Thailand and Singapore, SSD storage, managed through DirectAdmin, with 24-hour Thai support and 99% uptime.

Editor's pick from our hands-on testing.

Visit AsiaGB →

Frequently Asked Questions (FAQ)

What is the main difference between Docker containers and virtual machines?
Docker containers are lightweight and share the host OS kernel, typically using just a few MB of RAM. Virtual machines are heavier, each running a complete OS, using hundreds of MB or GB of resources. Containers start in seconds while VMs take minutes, making Docker more efficient for running multiple applications on a single VPS.
Do I need to be an expert to use Docker on a VPS?
No. While Docker has a learning curve, the basics are straightforward. Start with simple containers using existing images from Docker Hub, then progress to Docker Compose for multi-container setups. There are many tutorials and communities to help you learn.
Can I run Docker on a 2GB RAM VPS?
Yes, but only for lightweight applications or development/testing. A single small container might work, but production environments benefit from at least 4 GB RAM to ensure stability and room for growth. Consider the specific needs of your application before choosing a plan.
How do I update Docker containers in production?
Pull a new image version, stop the old container, and start a new one with the updated image. For zero-downtime updates with multiple containers, use a load balancer like Nginx and update containers one at a time. Docker Compose can automate this with the `docker-compose up -d` command after updating the image versions in your compose file.