Complete Guide to Setting Up Docker on a Linux VPS
Step-by-step tutorial for installing and configuring Docker on a VPS. Learn Docker basics, container management, and best practices for production environments.
Complete Guide to Setting Up Docker on a Linux VPS
Docker has become the industry standard for containerization, enabling developers to package applications with all dependencies and run them consistently across any environment. This guide walks you through installing Docker on a Linux VPS, configuring it for production use, and deploying your first containerized application.
Why Docker on a VPS?
Running Docker on a VPS provides several advantages: reduced resource consumption compared to virtual machines, faster deployment cycles, simplified dependency management, and seamless scaling. Whether you're running Node.js APIs, database services, or full-stack applications, Docker ensures consistency between development and production environments.
Prerequisites
- A Linux VPS (Ubuntu 20.04+ or Debian 11+ recommended)
- SSH access with sudo privileges
- At least 2GB RAM and 20GB disk space
- A package manager (apt, yum, or equivalent)
Step 1: Update System Packages
Before installing Docker, ensure your system is up to date:
sudo apt-get update
sudo apt-get upgrade -y
For RHEL-based systems (CentOS, Fedora):
sudo yum update -y
Step 2: Install Docker Engine
Using the Official Docker Repository (Ubuntu/Debian)
Add Docker's GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Add Docker repository:
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
Install Docker:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y
For RHEL-based Systems
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y
Step 3: Start and Enable Docker Service
Start the Docker daemon:
sudo systemctl start docker
Enable Docker to start on boot:
sudo systemctl enable docker
Verify installation:
docker --version
docker run hello-world
Step 4: Configure User Permissions
Add your user to the docker group to avoid using sudo for every command:
sudo usermod -aG docker $USER
Apply group membership:
newgrp docker
Step 5: Install Docker Compose (Optional but Recommended)
For managing multi-container applications:
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
Step 6: Configure Docker Daemon
Create or edit /etc/docker/daemon.json for production settings:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
},
"storage-driver": "overlay2",
"live-restore": true,
"userland-proxy": false
}
Apply configuration:
sudo systemctl restart docker
| Step | Component | Purpose |
|---|---|---|
| 1 | Docker Engine | Core containerization runtime |
| 2 | Docker CLI | Command-line interface for container management |
| 3 | Docker Daemon | Background service managing containers |
| 4 | Docker Compose | Multi-container orchestration tool |
Step 7: Deploy Your First Container
Create a simple Node.js application Dockerfile:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Build the image:
docker build -t my-node-app:latest .
Run the container:
docker run -d -p 3000:3000 --name my-app my-node-app:latest
Step 8: Docker Compose Multi-Container Setup
Create a docker-compose.yml for a Node.js + PostgreSQL stack:
version: '3.8'
services:
app:
build: .
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgresql://user:password@db:5432/appdb
depends_on:
- db
restart: unless-stopped
db:
image: postgres:15-alpine
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
- POSTGRES_DB=appdb
volumes:
- postgres_data:/var/lib/postgresql/data
restart: unless-stopped
volumes:
postgres_data:
Deploy:
docker-compose up -d
Step 9: Container Management
View running containers:
docker ps
View all containers:
docker ps -a
View container logs:
docker logs -f container_name
Stop a container:
docker stop container_name
Remove a container:
docker rm container_name
Step 10: Production Security Best Practices
- Keep Docker updated: Regularly run
sudo apt-get update && sudo apt-get upgrade - Use specific image tags: Avoid
latesttag in production; use versioned tags - Implement resource limits: Use
--memoryand--cpusflags - Run containers as non-root: Create dedicated user in Dockerfile
- Use secrets management: Implement Docker Secrets or environment-based solutions
- Enable restart policies: Use
--restart=unless-stoppedfor critical services - Monitor container health: Implement health checks in docker-compose
Example health check:
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
CMD node healthcheck.js
Useful Resources
- Docker Official Documentation
- Docker Hub - Container Registry
- Docker Compose Documentation
- Best Practices for Docker
Conclusion
You now have a fully functional Docker environment on your VPS. Start small with single-container applications, then progress to multi-container setups using Docker Compose. Monitor your containers regularly, keep images small with minimal base images like Alpine, and always implement proper logging and health checks for production reliability.