Running Docker and Docker Compose on a Cloud VPS: Production Setup, Storage & Security

Deploying containerized microservices on cloud infrastructure often begins smoothly in local development, but transitioning to a production Cloud VPS frequently reveals critical performance bottlenecks in Linux kernel namespaces, storage I/O amplification, and unmanaged container log accumulation. Without purposeful daemon tuning and host kernel configuration, default bridge networking degrades packet throughput while unconstrained container privileges expose internal host ports to external exploits. Enterprise web architectures hosted on MeraHost eliminate these production vulnerabilities by pairing high-performance NVMe storage with hardened Linux kernel parameters and predictable resource guarantees.

Production Architecture Blueprint: Docker and Compose on Cloud VPS

Direct Answer: A production Docker and Docker Compose deployment on a Cloud VPS requires a dedicated non-root execution model, optimized overlay2 storage on enterprise NVMe, strict log rotation in daemon.json, custom internal bridge networks, and sysctl kernel parameter hardening. This architecture delivers deterministic sub-millisecond container latency and zero-downtime rolling updates without the complexity of Kubernetes.

While multi-node orchestration platforms like Kubernetes dominate enterprise conversations, single-node and multi-service workloads running on a dedicated Cloud VPS represent the vast majority of web application deployments. A properly configured VPS running modern Docker Engine and Docker Compose V2 delivers remarkable performance, ultra-low resource overhead, and straightforward maintenance. However, running containers in production demands architectural rigor far beyond running docker compose up -d with default settings.

The standard Docker installation assumes an unconstrained development environment. By default, container logs write to unbounded JSON files on the root filesystem, container processes execute as root, network packets traverse non-optimized user-space proxy daemons, and storage layers suffer write amplification over conventional virtual disks. Transforming this development runtime into a resilient, production-ready infrastructure platform requires a holistic approach spanning host kernel tuning, storage subsystem configuration, daemon optimization, and declarative service manifests.

Cloud VPS vs. Default Docker Runtime: Architectural & Cost Matrix

Understanding the performance and security disparity between an out-of-the-box Docker installation and a hardened, tuned production deployment on high-speed infrastructure is vital for infrastructure planning. The table below outlines key behavioral differences across vital operational dimensions:

Feature / Metric Standard / Default Tuned / Production
Storage Driver & I/O Standard loopback / unconstrained overlay2 Direct NVMe overlay2 with metadatacopy
Logging Mechanism Uncapped json-file (disk exhaustion risk) Rotated JSON (max-size: 10m, max-file: 3)
Container Restart Policy no (manual recovery required) unless-stopped with healthcheck hooks
Host Network Security Docker bypasses UFW rules automatically Explicit DOCKER-USER chain & 127.0.0.1 bindings
Daemon Restart Resilience Containers terminate on daemon restarts Live-Restore enabled (zero container downtime)
Privilege Model Root user default inside container namespace Non-root UID/GID + cap_drop: [ALL]

Linux Kernel & Host Operating System Hardening for Docker

Before launching your first container, the underlying Linux operating system must be tuned to handle high-frequency socket allocations, heavy connection tracking, and containerized memory limits. When multiple services communicate concurrently over virtual bridge networks, standard Linux kernel defaults quickly exhaust file descriptors and TCP connection backlogs, causing silent request drops.

Deploy the following configuration to /etc/sysctl.d/99-docker-production.conf to optimize network socket limits, enhance memory overcommit handling, and expand connection tracking capacity:

# /etc/sysctl.d/99-docker-production.conf
# Production Linux Kernel Tuning for Docker on Cloud VPS

# Enable IPv4 forwarding for Docker bridge networks
net.ipv4.ip_forward = 1
net.ipv4.conf.all.forwarding = 1

# Maximize socket connection backlog and SYN queues
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 16384
net.core.netdev_max_backlog = 16384

# Expand TCP buffer limits for high-bandwidth web traffic
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216

# Prevent socket exhaustion under burst traffic
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15

# Elevate system-wide file descriptor allocations
fs.file-max = 2097152
fs.inotify.max_user_instances = 8192
fs.inotify.max_user_watches = 524288

# Expand netfilter connection tracking table
net.netfilter.nf_conntrack_max = 262144
net.netfilter.nf_conntrack_tcp_timeout_established = 86400

# Memory management & Virtual Memory overcommit rules
vm.max_map_count = 262144
vm.swappiness = 10
vm.overcommit_memory = 1

Apply these parameters immediately without rebooting the server by executing:

sudo sysctl --system

Architecture Note: When hosting high-throughput databases or search indices like PostgreSQL, MongoDB, or Elasticsearch in Docker containers, setting vm.max_map_count = 262144 and vm.overcommit_memory = 1 is mandatory. Failing to set these on the host will cause containerized memory allocators to crash during sudden transaction spikes.

Enterprise Docker Daemon Tuning: /etc/docker/daemon.json

The Docker daemon configuration file controls critical operational behaviors, including disk-space protection, process isolation, and container uptime during host maintenance. One of the most prevalent causes of Cloud VPS outages is disk exhaustion driven by unrotated container stdout and stderr logs. In default setups, Docker writes logs indefinitely until the host filesystem reaches 100% capacity, halting the entire operating system.

Create or update /etc/docker/daemon.json with enterprise-grade defaults:

{
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3",
    "compress": "true"
  },
  "live-restore": true,
  "userland-proxy": false,
  "no-new-privileges": true,
  "default-ulimits": {
    "nofile": {
      "Name": "nofile",
      "Hard": 65535,
      "Soft": 65535
    },
    "nproc": {
      "Name": "nproc",
      "Hard": 4096,
      "Soft": 4096
    }
  },
  "metrics-addr": "127.0.0.1:9323",
  "experimental": false
}

Key architectural directives explained:

  • live-restore: true: Ensures containers remain running even when the Docker daemon is updated, restarted, or experiences a transient crash. This prevents service outages during routine host OS updates.
  • log-opts (max-size & max-file): Enforces strict log rotation. Each container log is capped at 10 MB with 3 rotated archives, guaranteeing predictable, bounded disk utilization.
  • userland-proxy: false: Disables the legacy docker-proxy process, routing container network traffic directly via host iptables/nftables NAT. This reduces CPU and memory overhead while lowering packet routing latency.
  • no-new-privileges: true: Prevents child processes inside containers from gaining additional privileges via setuid or setgid binaries, neutralizing common privilege escalation exploits.

After saving the configuration, reload the systemd manager and restart Docker safely:

sudo systemctl daemon-reload && sudo systemctl restart docker

Persistent Storage Architecture: Named Volumes vs. Host Bind Mounts

Containerized workloads require distinct persistent storage tiers. Choosing the wrong storage mechanism can severely degrade I/O throughput or introduce file ownership collisions across host and container namespaces.

In a production Cloud VPS environment, adhere to the following storage hierarchy:

  • Docker Named Volumes (Managed by Docker): Best for transactional state (PostgreSQL, MySQL, Redis, MinIO). Named volumes reside under /var/lib/docker/volumes/. When provisioned on enterprise NVMe storage like that on MeraHost Enterprise Cloud, named volumes bypass virtual filesystem translation layers, providing near-native raw disk write speeds.
  • Host Bind Mounts (Mapped Explicit Host Paths): Best for static configuration files (e.g., ./nginx.conf:/etc/nginx/nginx.conf:ro) and static assets generated on the host. Always mount host configuration files with the :ro (read-only) flag to prevent compromised containers from altering host configurations.
  • Temporary Filesystems (tmpfs): Best for ephemeral session tokens, scratch caches, or in-memory queues. Using tmpfs mounts ensures sensitive transient data is written strictly to RAM and wiped upon container restart.

Architecture Note: Never run active database storage over networked network-attached block storage or NFS mounts with high latency. Always pair transactional databases with high-speed local NVMe volumes to prevent lock contention, transaction delays, and write starvation during backup cycles.

Production Docker Compose V2 Architecture: Multi-Tier Web Stack

A resilient multi-container production deployment requires service isolation, strict resource constraints, declarative health probes, and internal networking. Below is a comprehensive, production-grade docker-compose.yml file orchestrating a reverse proxy (Caddy/Traefik compatible), a hardened Node.js/Go backend API, a Redis cache layer, and a PostgreSQL database.

# docker-compose.yml - Production Multi-Tier Stack
version: "3.8"

networks:
  frontend_net:
    driver: bridge
    internal: false
  backend_net:
    driver: bridge
    internal: true

volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local
  caddy_data:
    driver: local
  caddy_config:
    driver: local

services:
  edge-proxy:
    image: caddy:2.8-alpine
    container_name: production_edge_proxy
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config
    networks:
      - frontend_net
    deploy:
      resources:
        limits:
          cpus: '1.0'
          memory: 512M
        reservations:
          cpus: '0.25'
          memory: 128M
    security_opt:
      - no-new-privileges:true
    cap_drop:
      - ALL
    cap_add:
      - NET_BIND_SERVICE

  app-api:
    image: ghcr.io/company/production-api:v2.4.1
    container_name: production_app_api
    restart: unless-stopped
    user: "1001:1001"
    read_only: true
    tmpfs:
      - /tmp:rw,noexec,nosuid,size=64m
    environment:
      - NODE_ENV=production
      - PORT=3000
      - DB_HOST=database
      - REDIS_HOST=cache
    env_file:
      - .env.production
    networks:
      - frontend_net
      - backend_net
    deploy:
      resources:
        limits:
          cpus: '2.0'
          memory: 1536M
        reservations:
          cpus: '0.50'
          memory: 512M
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://127.0.0.1:3000/health"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 20s
    security_opt:
      - no-new-privileges:true
    cap_drop:
      - ALL

  cache:
    image: redis:7.2-alpine
    container_name: production_cache
    restart: unless-stopped
    command: ["redis-server", "--requirepass", "${REDIS_PASSWORD}", "--appendonly", "yes", "--maxmemory", "384mb", "--maxmemory-policy", "allkeys-lru"]
    volumes:
      - redis_data:/data
    networks:
      - backend_net
    deploy:
      resources:
        limits:
          cpus: '0.75'
          memory: 512M
    security_opt:
      - no-new-privileges:true
    cap_drop:
      - ALL

  database:
    image: postgres:16-alpine
    container_name: production_database
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - backend_net
    deploy:
      resources:
        limits:
          cpus: '2.0'
          memory: 2048M
        reservations:
          cpus: '0.5'
          memory: 512M
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER} -d ${DB_NAME}"]
      interval: 10s
      timeout: 5s
      retries: 5
    security_opt:
      - no-new-privileges:true

Critical architectural highlights in this Compose configuration:

  • Network Partitioning: The backend_net is designated with internal: true. Neither Redis nor PostgreSQL can communicate with the external Internet or the host gateway, rendering direct external attacks impossible even if an attacker probes the VPS.
  • Zero Unmapped Ports: Notice that neither Redis nor PostgreSQL publish ports to the host (no ports: block). They are strictly reachable via container name resolution on backend_net.
  • Resource Constraints: Both limits and reservations prevent noisy-neighbor container starvation and shield the Linux host from Out-Of-Memory (OOM) killer panics.
  • Immutable Root Filesystem: The app-api container runs with read_only: true, disallowing malicious tampering or file modification inside running containers. Transient writes are restricted to an in-memory tmpfs mount.

The Docker and UFW Firewall Dilemma: Restoring Network Security

One of the most dangerous architectural pitfalls in Linux systems administration is the interaction between Docker and the Uncomplicated Firewall (UFW). By default, Docker injects rules directly into the host iptables prerouting chain before UFW rules execute. Consequently, if you publish a port using ports: - "5432:5432" in Docker Compose, that port becomes publicly accessible to the entire Internet, completely ignoring ufw default deny incoming.

To prevent unauthorized public exposure, use one of two robust patterns:

Method A: Localhost Binding (Recommended for Single-Host Proxies)

Explicitly bind all internal ports to the loopback interface in your Compose file:

ports:
  - "127.0.0.1:8080:8080"  # Only accessible to local host reverse proxy

Method B: The DOCKER-USER iptables Filter Chain

For fine-grained host firewalling, configure Docker’s dedicated filter chain in /etc/ufw/after.rules. Docker preserves rules placed in DOCKER-USER. Add the following block to the bottom of /etc/ufw/after.rules:

# /etc/ufw/after.rules
# Protect Docker published ports via DOCKER-USER chain
*filter
:DOCKER-USER - [0:0]
-A DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A DOCKER-USER -i eth0 -p tcp -m tcp --dport 80 -j ACCEPT
-A DOCKER-USER -i eth0 -p tcp -m tcp --dport 443 -j ACCEPT
-A DOCKER-USER -i eth0 -j DROP
COMMIT

Reload UFW to enforce the protection:

sudo ufw reload

Automated Maintenance & Container Lifecycle Operations

Over time, unused image layers, stopped builder stages, and dangling volumes accumulate on the host disk. Implement an automated maintenance schedule using a systemd timer or cron job to maintain consistent disk hygiene without interrupting active services.

Create a weekly maintenance script at /usr/local/bin/docker-maintenance.sh:

#!/usr/bin/env bash
# /usr/local/bin/docker-maintenance.sh
set -euo pipefail

# Purge stopped containers older than 72 hours
docker container prune -f --filter "until=72h"

# Remove untagged and dangling image layers
docker image prune -f

# Clean unused builder cache layers
docker builder prune -f --keep-storage 2GB

# Verify active volume integrity
docker system df

Make the script executable and schedule it to run weekly:

sudo chmod +x /usr/local/bin/docker-maintenance.sh
(crontab -l 2>/dev/null; echo "0 3 * * 0 /usr/local/bin/docker-maintenance.sh > /var/log/docker-prune.log 2>&1") | crontab -

Frequently Asked Questions

Why does Docker bypass my Linux UFW firewall rules by default?

Docker modifies the host iptables directly to enable container network address translation (NAT). Because Docker’s PREROUTING rules execute before UFW’s input filtering chain, any port mapped with a public port binding (such as -p 8080:8080) bypasses UFW blocks. To fix this, bind ports explicitly to 127.0.0.1 or configure the DOCKER-USER chain in /etc/ufw/after.rules.

What is the advantage of live-restore in production Docker setups?

Enabling "live-restore": true in /etc/docker/daemon.json allows running containers to remain active and serving traffic even when the Docker daemon process restarts, crashes, or is upgraded during operating system updates. Without live-restore, restarting the Docker daemon abruptly stops every running container on the host.

Should I use named volumes or host bind mounts for databases?

For transactional databases like PostgreSQL and MySQL, named volumes managed under /var/lib/docker/volumes are strongly recommended. They provide superior I/O throughput on enterprise NVMe disks, prevent UID/GID permissions conflicts between the container and host, and integrate cleanly with Docker’s volume backup and snapshot workflows.

How do I achieve zero-downtime container deployments using Docker Compose?

You can achieve zero downtime by deploying behind a dynamic reverse proxy (such as Caddy, Traefik, or Nginx) combined with container health checks. Run docker compose up -d --no-deps --scale app-api=2 --no-recreate app-api, wait for health probes to confirm the new container is healthy, and subsequently retire the outdated container.

Deploy Enterprise-Grade Production Infrastructure

Need guaranteed performance with zero price hikes? Host mission-critical workloads on MeraHost with pure Enterprise NVMe, LiteSpeed Web Server, and Same Renewal Price, Always (starting at ₹99/mo).

Rate this post
Enterprise Cloud Infrastructure

Ready for Blazing-Fast NVMe Cloud Hosting?

Tired of slow shared servers and unexpected renewal price hikes? Migrate to MeraHost with 100% free migration, pure Gen4 NVMe compute, 99.95% SLA uptime, and 24/7 human sysadmin support.

Pure Gen4 NVMe SSD LiteSpeed + LSCache 100% Free Migration 0% Renewal Hikes

Leave a Comment