Docker on a VPS
Run containerized apps on your own server with Docker Compose, auto-updating containers, and Nginx proxying
Skills taught: deployment.docker-compose-production, deployment.watchtower, deployment.container-networking, deployment.nginx-docker
Docker Compose on a VPS
Docker Compose on a VPS combines the best of both worlds: the reproducibility and isolation of containers with the simplicity and cost of a single VPS. You get the same docker-compose.yml that runs locally to also run in production. No Kubernetes. No ECS. One file describes your entire stack — app, database, Redis, worker — and a single command starts everything.
# Install Docker via the official script (simplest method)
curl -fsSL https://get.docker.com | sh
# Add your user to the docker group (avoids sudo for docker commands)
sudo usermod -aG docker deploy
newgrp docker # apply group change without logout
# Verify
docker --version # Docker 27.x.x
docker compose version # Docker Compose v2.x.x
# Docker Compose v2 is built into Docker — no separate install needed
# Use 'docker compose' (with a space) not 'docker-compose' (hyphen)
Production Docker Compose file
services:
app:
image: ghcr.io/myorg/myapp:latest # Pull from GitHub Container Registry
restart: always
environment:
NODE_ENV: production
DATABASE_URL: postgresql://postgres:${POSTGRES_PASSWORD}@db:5432/myapp
REDIS_URL: redis://redis:6379
JWT_SECRET: ${JWT_SECRET}
depends_on:
db:
condition: service_healthy
redis:
condition: service_started
# Don't expose port externally — Nginx proxies to it
expose:
- "3000"
networks:
- app-net
db:
image: postgres:16-alpine
restart: always
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
POSTGRES_DB: myapp
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
networks:
- app-net
redis:
image: redis:7-alpine
restart: always
volumes:
- redis-data:/data
networks:
- app-net
networks:
app-net:
driver: bridge
volumes:
postgres-data:
redis-data:
# Clone repo to server
git clone https://github.com/myorg/myapp.git /home/deploy/myapp
cd /home/deploy/myapp
# Create .env file with secrets (NEVER commit this)
cat > .env << 'EOF'
POSTGRES_PASSWORD=supersecretpassword
JWT_SECRET=anothersecretvalue
EOF
chmod 600 .env
# Start the stack
docker compose -f docker-compose.prod.yml up -d
# View logs
docker compose -f docker-compose.prod.yml logs -f app
# Run database migrations (one-off command)
docker compose -f docker-compose.prod.yml exec app npm run db:migrate
# Update to latest image
docker compose -f docker-compose.prod.yml pull
docker compose -f docker-compose.prod.yml up -d
# Docker Compose performs a rolling update (stops old, starts new)
# View running containers
docker compose -f docker-compose.prod.yml ps
Nginx proxy to Docker containers
# Docker Compose containers are on a bridge network
# From the host, reach them via localhost + the exposed port
# (or container name if using host networking)
upstream app_backend {
server 127.0.0.1:3000; # Nginx is on the host, app is in a container
# If app exposes port 3000: ports: ["127.0.0.1:3000:3000"]
# Binding to 127.0.0.1 means ONLY Nginx can reach it, not the internet
}
server {
listen 80;
server_name myapp.com www.myapp.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name myapp.com www.myapp.com;
ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem;
location / {
proxy_pass http://app_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;
}
}
# In docker-compose.prod.yml, expose the app to localhost only:
services:
app:
image: ghcr.io/myorg/myapp:latest
ports:
# Bind to 127.0.0.1 — only Nginx on this host can reach port 3000
# 0.0.0.0:3000:3000 would expose it to the internet (don't do this)
- "127.0.0.1:3000:3000"
# ... rest of config
Watchtower — automatic container updates
services:
# ... your existing services ...
watchtower:
image: containrrr/watchtower
restart: always
volumes:
- /var/run/docker.sock:/var/run/docker.sock
# If pulling from GitHub Container Registry:
- /home/deploy/.docker/config.json:/config.json
environment:
WATCHTOWER_POLL_INTERVAL: 300 # check every 5 minutes
WATCHTOWER_CLEANUP: "true" # remove old images
WATCHTOWER_INCLUDE_STOPPED: "false"
WATCHTOWER_NOTIFICATIONS: "email" # optional notifications
command: --interval 300 app # only watch the 'app' service
Tip: Use Watchtower only for services where instant updates are safe. For databases and stateful services, update manually so you can run migrations first. For your application container, Watchtower is great — it detects when a new image is pushed to the registry and redeploys automatically. Combined with your CI/CD pipeline pushing to GitHub Container Registry on every merge, you get automatic deploys to your VPS without any extra orchestration.
Frequently Asked Questions
Should I run Nginx in Docker too, or on the host?
Either works. Running Nginx on the host is simpler for beginners — Certbot can manage certificates directly. Running Nginx in Docker (with jwilder/nginx-proxy or Traefik) is more portable. For a single VPS, host-installed Nginx is less moving parts. For multiple apps on the same VPS, Traefik as a reverse proxy container that auto-discovers other containers and handles SSL is worth learning.