Server Management 3 min read Last updated: 2026-10-23

Docker Engine Deployment, Log Rotation & UFW Firewall Isolation

Install official Docker Engine and Compose v2 on Linux, configure daemon.json log rotation to prevent NVMe disk exhaustion, enable IPv6, and resolve the UFW firewall bypass flaw.

Containerization with Docker Engine and Docker Compose v2 is the standard method for deploying web applications, databases, and microservices on FlawlessNode KVM instances. However, default Docker installations carry two critical production pitfalls: unbounded JSON container logs that rapidly exhaust NVMe storage, and iptables NAT rules that bypass UFW/firewalld to expose internal ports directly to the public internet. This guide walks through a hardened, production-ready Docker setup.

1. Installing Official Docker Engine & Compose v2

Avoid outdated distribution-packaged docker.io or docker-compose v1 binaries. Install the upstream Docker CE and Docker Compose v2 plugin directly from Docker’s official repository:

1.1 Automated Official Installation (Ubuntu / Debian / Rocky / AlmaLinux)

# Download and execute the official Docker installation script
curl -fsSL https://get.docker.com | sudo sh

# Enable and start the Docker service immediately
sudo systemctl enable --now docker

Verify the installed versions of both the engine and the Compose plugin:

docker --version
# Expected output: Docker version 27.x.x or 28.x.x

docker compose version
# Expected output: Docker Compose version v2.x.x

[!TIP] Notice the space in docker compose (v2 plugin) as opposed to the deprecated docker-compose (v1 Python script). Compose v2 is written in Go, natively integrated into the Docker CLI, and supports modern Compose Specification features.


2. Production daemon.json Optimization (Log Rotation & Storage)

By default, Docker captures all stdout and stderr output from every container into /var/lib/docker/containers/<id>/<id>-json.log with no size limit. High-traffic web servers or verbose services can easily generate tens of gigabytes of logs and trigger disk exhaustion (No space left on device).

Configure /etc/docker/daemon.json immediately after installation to enforce automatic log rotation and efficient overlay2 storage:

sudo mkdir -p /etc/docker

cat << 'EOF' | sudo tee /etc/docker/daemon.json
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "50m",
    "max-file": "3",
    "compress": "true"
  },
  "storage-driver": "overlay2",
  "live-restore": true,
  "default-ulimits": {
    "nofile": {
      "Name": "nofile",
      "Hard": 1048576,
      "Soft": 1048576
    }
  }
}
EOF

# Reload configuration and restart Docker
sudo systemctl daemon-reload
sudo systemctl restart docker

Key Parameter Breakdown:

Parameter Purpose Production Impact
max-size: "50m" & max-file: "3" Caps each container’s log history at 3 × 50MB = 150MB max Prevents runaway logs from filling up your NVMe root partition
live-restore: true Keeps running containers alive while the dockerd daemon upgrades or restarts Minimizes service downtime during maintenance
default-ulimits.nofile Raises open file descriptor limits inside containers to 1,048,576 Prevents Too many open files errors under high concurrency

[!NOTE] Changes to /etc/docker/daemon.json only apply to newly created containers. If you already have running containers prior to adding log rotation, recreate them via docker compose up -d --force-recreate.


3. Critical Security Fix: Preventing Docker from Bypassing UFW

3.1 Why Does Docker Bypass UFW?

When you publish a port using -p 8080:8080 or ports: ["8080:8080"], Docker inserts rules directly into the Linux kernel’s nat (PREROUTING) and filter (DOCKER) chains before UFW’s ufw-before-input rules are evaluated. Even if you run ufw default deny incoming and never allow port 8080, anyone on the public internet can still access port 8080 directly!

If your container sits behind a host reverse proxy (such as Nginx or Caddy) or is only accessed locally (such as Redis, MySQL, or PostgreSQL), explicitly bind the host address to loopback 127.0.0.1:

# docker-compose.yml example
services:
  webapp:
    image: nginx:alpine
    restart: unless-stopped
    ports:
      # ✅ SAFE: Only accessible from localhost / host Nginx reverse proxy
      - "127.0.0.1:8080:80"
      # ❌ DANGEROUS: Bypasses UFW and exposes port 8080 to 0.0.0.0!
      # - "8080:80"

3.3 Solution B: Enforce UFW Rules on All Docker Containers (ufw-docker)

If you publish container ports on public interfaces (0.0.0.0) and want UFW to remain the single source of truth for firewall filtering, append the DOCKER-USER filter rules to /etc/ufw/after.rules:

sudo tee -a /etc/ufw/after.rules > /dev/null << 'EOF'
# BEGIN UFW AND DOCKER
*filter
:ufw-user-forward - [0:0]
:ufw-docker-logging-deny - [0:0]
:DOCKER-USER - [0:0]
-A DOCKER-USER -j ufw-user-forward

-A DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j RETURN
-A DOCKER-USER -m conntrack --ctstate INVALID -j DROP
-A DOCKER-USER -i eth0 -m set --match-set ufw-user-input dst -j RETURN

# Block external access to RFC1918 private container subnets by default
-A DOCKER-USER -i eth0 -d 10.0.0.0/8 -j ufw-docker-logging-deny
-A DOCKER-USER -i eth0 -d 172.16.0.0/12 -j ufw-docker-logging-deny
-A DOCKER-USER -i eth0 -d 192.168.0.0/16 -j ufw-docker-logging-deny

-A DOCKER-USER -j RETURN
-A ufw-docker-logging-deny -m limit --limit 3/min --limit-burst 10 -j LOG --log-prefix "[UFW DOCKER BLOCK] "
-A ufw-docker-logging-deny -j DROP
COMMIT
# END UFW AND DOCKER
EOF

sudo ufw reload

Once applied, you can explicitly allow public traffic to a specific container port using sudo ufw route allow proto tcp from any to any port 8080.


4. Setting Container Memory & CPU Limits

To prevent a memory leak inside a single container from triggering the host kernel’s OOM Killer and crashing your SSH session or database, always declare resource limits in docker-compose.yml:

services:
  api-service:
    image: node:22-alpine
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: "1.50"
          memory: 1024M
        reservations:
          memory: 256M

Monitor real-time container CPU, RAM, and network I/O utilization with:

docker stats

5. Reclaiming Disk Space from Unused Images & Build Cache

Over time, dangling image layers, stopped containers, and BuildKit caches accumulate on disk. Inspect and clean up Docker storage safely:

# 1. Inspect detailed disk usage breakdown (Images, Containers, Volumes, Build Cache)
docker system df

# 2. Remove stopped containers, unused networks, and dangling images
docker system prune -f

# 3. Remove all unused images (not attached to any running container) older than 7 days
docker image prune -a --filter "until=168h" -f

[!CAUTION] Avoid passing the --volumes flag to docker system prune unless you are certain no persistent database volumes (such as Postgres or MariaDB data directories) are currently detached!

Was this page helpful?