Docker on VPS: Complete Installation and Usage Guide
Table of Contents
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:
- Isolation and Stability: Each container runs in its own isolated environment. If one application crashes or has issues, it does not affect other containers or the host system.
- Resource Efficiency: Containers are much lighter than virtual machines, typically using just a few MB of RAM compared to hundreds of MB or GB for VMs. This means you can run many more applications on a single VPS.
- Consistency Across Environments: Docker images ensure that your application runs identically on your local machine, staging server, and production VPS. This eliminates the "but it works on my machine" problem.
- Scalability: Docker makes it easy to scale applications horizontally by running multiple container instances and using load balancing.
- Faster Deployment: Docker images can be started in seconds, enabling rapid deployment and updates.
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:
- CPU: A minimum of 2 CPU cores is recommended for basic Docker workloads. For production environments with multiple containers, 4+ cores provide better performance. Docker containers can share CPU resources, so the total number of cores limits how many containers can run simultaneously.
- RAM: At least 2 GB of RAM is necessary for the host OS and Docker daemon, plus additional RAM for your containers. For a single small application, 2 GB total might suffice, but 4 GB is more comfortable, and 8+ GB is recommended for production setups with multiple services.
- Storage: Docker images and container data require disk space. SSD storage is highly recommended for Docker workloads because it significantly improves image pulling and container startup times. A minimum of 20 GB is needed, but 50+ GB is better for storing multiple images and persistent data volumes.
- Operating System: Docker works best on modern Linux distributions. Ubuntu 20.04 LTS, 22.04 LTS, and newer versions are ideal. CentOS 7+ and Debian 10+ also support Docker. Windows VPS can run Docker but with higher resource overhead due to Hyper-V.
- Linux Kernel: Docker requires Linux kernel 3.10 or newer (5.10+ is recommended for better performance). Check your kernel version with the command
uname -r. - Network: A stable internet connection for pulling Docker images from registries. Ensure your VPS has good bandwidth, especially for large image downloads during deployment.
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 systemdocker 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 volumedocker run -v mydata:/app/data nginx:latest - Mount volume into containerdocker volume ls - List all volumes
Container Lifecycle Management
- Start and Stop:
docker start container_idanddocker stop container_idgracefully shut down containers. - Restart:
docker restart container_idstops and restarts a container. - Remove:
docker rm container_iddeletes a stopped container. Use-fflag to force remove running containers. - View Logs:
docker logs container_idshows container output for debugging. - Resource Limits:
docker run -m 512m --cpus=0.5 image_namerestricts memory and CPU usage.
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 volumesdocker image prune - Remove dangling imagesdocker 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 networkdocker 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
- Use images from official registries and trusted sources like Docker Hub official images.
- Scan images for vulnerabilities:
docker scan image_name - Keep base images updated and rebuild containers regularly with the latest patches.
- Minimize image size by using smaller base images like Alpine Linux.
- Avoid storing secrets (passwords, API keys) in images; use environment variables and secrets management tools instead.
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.
Choosing the Right VPS for Docker Hosting in Thailand
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
- SSD Storage: This is essential. SSD significantly improves Docker image pulling, container startup times, and overall application performance compared to traditional HDD storage.
- CPU and RAM: Choose a plan with enough resources for your containers. Start with 4 CPU cores and 4 GB RAM minimum for any serious Docker workload.
- Uptime Guarantee: Reliable hosting with 99% or higher uptime ensures your Docker containers stay online.
- Control Panel: DirectAdmin provides a user-friendly interface for managing your VPS, making it easier to configure Docker and manage applications.
- Support Quality: 24-hour support in your local language is invaluable when issues arise.
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.