VPS or managed platform?
A virtual private server (VPS) gives full control, predictable costs and no dependency on a closed platform. In return, security, updates and backups are your responsibility. A managed platform (PaaS) takes these tasks off your hands, at the cost of less control and often a bill that grows with usage.
If you choose a VPS, the steps below are a minimum baseline. It is the approach described in our Infrastructure & DevSecOps case study.
Harden the server
- Create a dedicated user with
sudoand disable directrootlogin. - Allow SSH with keys only, never with passwords.
- Close all inbound ports by default with UFW, then open only what is needed.
- Install Fail2Ban to ban repeated attempts.
- Enable automatic security updates (
unattended-upgradeson Ubuntu).
# /etc/ssh/sshd_config.d/10-hardening.conf
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin nosudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 80/tcp # HTTP (redirected to HTTPS)
sudo ufw allow 443/tcp # HTTPS
sudo ufw allow 51820/udp # WireGuard
sudo ufw limit OpenSSH # remove once SSH is reachable through the tunnel
sudo ufw enableBuild a clean Docker image
A multi-stage image only contains what is needed at runtime: no build tools, no development dependencies, no unnecessary source code. It runs as an unprivileged user and contains no secrets.
FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build && npm prune --omit=dev
FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build --chown=node:node /app/package.json ./
COPY --from=build --chown=node:node /app/node_modules ./node_modules
COPY --from=build --chown=node:node /app/dist ./dist
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]A .dockerignore file keeps .git, node_modules and .env files out of the build context.
Automatic HTTPS with Traefik
Traefik receives all web traffic, obtains and renews Let's Encrypt certificates automatically, then routes each request to the right container based on the domain name. Adding a service only takes a few labels, with no certificate reconfiguration.
services:
traefik:
image: traefik:v3.1
command:
- --providers.docker=true
- --providers.docker.exposedbydefault=false
- --providers.docker.network=proxy
- --entrypoints.web.address=:80
- --entrypoints.web.http.redirections.entrypoint.to=websecure
- --entrypoints.websecure.address=:443
- --certificatesresolvers.le.acme.email=${ACME_EMAIL}
- --certificatesresolvers.le.acme.storage=/letsencrypt/acme.json
- --certificatesresolvers.le.acme.tlschallenge=true
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- letsencrypt:/letsencrypt
networks: [proxy]
app:
image: ${APP_IMAGE}
env_file: .env
labels:
- traefik.enable=true
- traefik.http.routers.app.rule=Host(`${APP_DOMAIN}`)
- traefik.http.routers.app.entrypoints=websecure
- traefik.http.routers.app.tls.certresolver=le
- traefik.http.services.app.loadbalancer.server.port=3000
networks: [proxy, internal]
db:
image: postgres:17
env_file: .env.db
volumes:
- pgdata:/var/lib/postgresql/data
networks: [internal]
networks:
proxy:
name: proxy
internal:
internal: true
volumes:
letsencrypt:
pgdata:The database is only attached to the internal network: it can be reached neither from the Internet nor by Traefik. Values written as ${…} come from a .env file that is never committed.
Environment variables and secrets
.envfiles are excluded from the repository (.gitignore) and readable by their owner only (chmod 600).- Each environment (staging, production) has its own values: no secret is shared between them.
- Variables embedded in code that runs in the browser (for example
NEXT_PUBLIC_*with Next.js) are public: they must never contain a secret. - Secrets are rotated when someone leaves the project or whenever in doubt.
Private administration with WireGuard
Administration interfaces (deployment dashboard, monitoring, database) must not be exposed to the Internet. A WireGuard VPN tunnel connects authorised devices to the server, and these interfaces only listen on the tunnel address. Once the tunnel is in place, SSH access can be restricted to it as well, and the public SSH rule removed from the firewall.
Back up, test, monitor
The 3-2-1 rule remains a good baseline: three copies of the data, on two different media, one of them off the server. A backup is only worth something if restoring it has been tested.
# Daily PostgreSQL backup (compressed format, restorable with pg_restore)
docker compose exec -T db pg_dump -U "$POSTGRES_USER" -Fc "$POSTGRES_DB" > "backup-$(date +%F).dump"- Encrypt backups sent off the server and limit how long they are kept.
- Regularly restore a backup to a test environment.
- Monitor site availability, disk space, certificate expiry and error logs.
Checklist
- SSH with keys only, root login disabled.
- Firewall closed by default; only Traefik and WireGuard are exposed.
- No internal service port published by Docker.
- Multi-stage images, unprivileged user, no secrets in the image.
- Automatic certificates and HTTP → HTTPS redirect.
- Secrets kept out of the repository, separate per environment.
- Administration reachable only through the VPN.
- Off-site, encrypted backups with tested restores.
Key takeaways
- A firewall alone is not enough with Docker: only publish the reverse proxy's ports.
- Traefik automates HTTPS and routing; the database stays on an internal network.
- Administration goes through a VPN, never through a public interface.
- An untested backup is not a backup.
Keywords
- Docker
- Traefik
- VPS
- WireGuard
- Backups