Docker is becoming an increasingly popular tool for deploying applications thanks to its simplicity and flexibility. On a VPS (Virtual Private Server), Docker offers special advantages, allowing you to manage complex applications easily and optimize resource usage. This comprehensive guide covers three key aspects of working with Docker on a VPS: getting started, deploying multi‑container applications, and performance optimization. Each section includes practical tips and real‑world examples to help you confidently work with Docker on your Debian 12 VPS server.
Getting Started with Docker on VPS
Installing Docker on a Debian 12 VPS
Before you start working with containers, you need to properly install Docker on your Debian 12 VPS. The installation process on Debian differs slightly from Ubuntu but remains straightforward.
First, connect to your VPS via SSH:
ssh root@your-vps-ip
Update the system and install required packages:
sudo apt update
sudo apt install ca-certificates curl gnupg lsb-release -y
Add Docker’s official GPG key:
sudo mkdir -m 0755 -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
Add the Docker repository for Debian 12:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Install Docker and Docker Compose:
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
On Debian 12, Docker Compose is installed automatically as a plugin together with the main Docker package. This means you will use the `docker compose` command (with a space) instead of the old `docker-compose`.
Optionally, add your user to the docker group:
sudo usermod -aG docker $USER
Verify Docker is working correctly:
docker run hello-world
docker compose version
Running Your First Container on a Debian 12 VPS
After installing Docker, you can run your first container. Let’s start with a simple Nginx web server:
docker run -d -p 80:80 --name my-nginx nginx:latest
This command does the following:
- `-d` — runs the container in the background
- `-p 80:80` — maps VPS port 80 to container port 80
- `--name my-nginx` — assigns a readable name to the container
- `nginx:latest` — uses the latest Nginx image
Now your web server is available at your VPS IP address. Open a browser and enter your server’s IP — you should see the Nginx welcome page.
Using Docker Hub on Debian 12
Docker Hub is a public registry of Docker images where you can find ready‑made images for various applications. To search for images, use:
docker search nginx
To pull an image:
docker pull nginx:latest
List all images downloaded to your Debian 12 VPS:
docker image ls
Essential Commands for Managing Containers
Here are the most important commands you’ll use daily on a Debian 12 VPS:
**View containers:**
docker ps # Running containers
docker ps -a # All containers
**Manage containers:**
docker start container-name # Start
docker stop container-name # Stop
docker restart container-name # Restart
docker rm container-name # Remove
**View logs and resource stats:**
docker logs container-name # Container logs
docker stats # Resource usage stats
Make a Container Accessible via VPS IP
To expose your containers externally via the IP of your Debian 12 VPS, map ports properly using `-p`:
docker run -d -p 8080:80 nginx # VPS:8080 -> container:80
docker run -d -p 3000:3000 my-app # VPS:3000 -> container:3000
For multiple ports:
docker run -d -p 80:80 -p 443:443 nginx
On Debian 12, make sure your firewall allows traffic on the required ports:
sudo ufw allow 80
sudo ufw allow 443
sudo ufw status
Deploying Multi-Container Apps on VPS (Docker Compose)
Why Use a VPS with Docker for Complex Projects
Modern web applications rarely consist of a single service. You usually need a database, a web server, perhaps a cache or a message queue. Managing each container separately on a VPS quickly becomes cumbersome. Docker Compose solves this by letting you describe the entire architecture in one file.
On Debian 12, Docker Compose is installed automatically as a Docker plugin, which means you use the `docker compose` command instead of a separate utility.
Benefits of using Docker Compose on a Debian 12 VPS:
- Deploy the whole app with a single command
- Automatic network creation between services
- Easy scaling of individual components
- Version your configuration alongside the code
Verifying Docker Compose on Debian 12
Since Docker Compose ships with Docker on Debian 12, verify it’s available:
docker compose version
If the command works, you’re ready to create multi‑container applications on your VPS.
Example docker-compose.yml (WordPress + MySQL) on a Debian 12 VPS
Here’s a practical example of deploying WordPress with MySQL on a Debian 12 VPS:
version: '3.8'
services:
db:
image: mysql:5.7
container_name: wordpress-db
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: strong_password_123
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress_password
volumes:
- db_data:/var/lib/mysql
networks:
- wordpress-network
wordpress:
depends_on:
- db
image: wordpress:latest
container_name: wordpress-site
restart: unless-stopped
ports:
- "80:80"
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress_password
WORDPRESS_DB_NAME: wordpress
volumes:
- wordpress_data:/var/www/html
networks:
- wordpress-network
volumes:
db_data:
wordpress_data:
networks:
wordpress-network:
driver: bridge
Starting Multiple Containers at Once on a Debian 12 VPS
Save the configuration above to a `docker-compose.yml` on your Debian 12 VPS and run:
docker compose up -d
Note the use of `docker compose` (with a space) — this is the new command on Debian 12. The older `docker-compose` may not work.
This command:
- Creates a network between the containers
- Pulls the necessary images
- Starts the containers in the correct order
- Mounts volumes for data persistence
Check service status:
docker compose ps
View logs from all services:
docker compose logs
Verifying Services via VPS IP
After launching the containers on your Debian 12 VPS, verify they are working:
- Open your browser and enter your VPS IP
- The WordPress installation page should appear
- Check if the containers are running:
docker compose ps
- Inspect logs in case of issues:
docker compose logs wordpress
docker compose logs db
Scaling Containers in a Debian 12 VPS Environment
Docker Compose makes it easy to scale services on a Debian 12 VPS. For example, to run multiple instances of a web app:
docker compose up --scale web=3 -d
For a production environment on a Debian 12 VPS, add to `docker-compose.yml`:
services:
web:
image: my-app:latest
deploy:
replicas: 3
resources:
limits:
cpus: '0.50'
memory: 512M
reservations:
cpus: '0.25'
memory: 256M
Optimizing Containers Performance on VPS
How to Limit Container Resources on a Debian 12 VPS (CPU, RAM)
Proper resource limits are critical on a VPS, since resources are constrained. Without limits, a single container can consume the entire server.
**Limits via `docker run` on Debian 12:**
# Limit memory to 512MB and CPU to 1 core
docker run -d --memory="512m" --cpus="1.0" nginx
# Limit swap memory
docker run -d --memory="512m" --memory-swap="1g" my-app
**Limits via Docker Compose on Debian 12:**
services:
web:
image: nginx
deploy:
resources:
limits:
cpus: '1.5'
memory: 500M
reservations:
cpus: '0.5'
memory: 200M
**Monitoring resource usage on Debian 12:**
docker stats # Real time
docker stats --format "table {{.Container}} {{.CPUPerc}} {{.MemUsage}}"
Typical resource limits for Docker containers on a VPS
Optimizing Volumes and Caching on Debian 12
Proper volume usage on a Debian 12 VPS significantly affects performance:
**Named volumes for critical data:**
volumes:
db_data:
driver: local
app_cache:
driver: local
services:
db:
volumes:
- db_data:/var/lib/mysql
app:
volumes:
- app_cache:/app/cache
**Bind mounts for development on a Debian 12 VPS:**
services:
app:
volumes:
- ./app:/var/www/html
- /etc/localtime:/etc/localtime:ro # Time synchronization
**Optimizing Dockerfile for caching on Debian 12:**
FROM node:16-alpine
WORKDIR /app
# Copy package.json first to cache dependencies
COPY package*.json ./
RUN npm install
# Then copy the code
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
Network Configuration on a Debian 12 VPS for Stable Operation
Proper network configuration ensures stable container operation on a Debian 12 VPS:
**Create separate networks for security:**
networks:
frontend:
driver: bridge
backend:
driver: bridge
internal: true # Internal-only
services:
web:
networks:
- frontend
- backend
db:
networks:
- backend # Database is not accessible from outside
**DNS configuration for services on Debian 12:**
services:
app:
networks:
default:
aliases:
- api.local
Monitoring Containers on a Debian 12 VPS
Effective monitoring is crucial for a Debian 12 VPS. Here are several approaches:
Built-in Docker tools:
docker stats # Real-time stats
docker system df # Disk usage
docker system events # System events
Prometheus + Grafana for a Debian 12 VPS:
services:
prometheus:
image: prom/prometheus
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
grafana:
image: grafana/grafana
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin123
Netdata for simple monitoring on Debian 12:
docker run -d --name=netdata -p 19999:19999 -v netdataconfig:/etc/netdata -v netdatalib:/var/lib/netdata -v netdatacache:/var/cache/netdata -v /proc:/host/proc:ro -v /sys:/host/sys:ro -v /var/run/docker.sock:/var/run/docker.sock:ro --cap-add SYS_PTRACE --security-opt apparmor=unconfined netdata/netdata
Recommendations for Production Loads on Debian 12
Health checks for automatic recovery:
services:
web:
image: nginx
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
Restart policies for stability on Debian 12:
services:
app:
restart: unless-stopped # Recommended for production
Logging on a Debian 12 VPS:
services:
app:
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
Security in production on Debian 12:
services:
app:
user: "1000:1000" # Do not run as root
read_only: true # Read-only filesystem
tmpfs:
- /tmp
cap_drop:
- ALL
cap_add:
- NET_BIND_SERVICE
Regular cleanup on a Debian 12 VPS:
# Remove unused containers, networks, images
docker system prune -a --volumes
# Automate via cron on Debian 12
0 2 * * 0 docker system prune -f
Specific settings for Debian 12:
# Check Docker service
sudo systemctl status docker
sudo systemctl enable docker
# Autostart services after VPS reboot
sudo systemctl enable docker.service
sudo systemctl enable containerd.service
This comprehensive guide covers all the key aspects of working with Docker on a Debian 12 VPS — from the initial installation to complex production deployments. The main difference is using the built‑in Docker Compose plugin and the `docker compose` command instead of a separate utility. Proper use of these techniques will help you build a reliable, scalable, and efficient infrastructure for your applications on a Debian 12 VPS.
en