PRODUCTION DOCKER HOST

Run all your containers on one dedicated box with Compose v2, automatic TLS, healthchecks, backups and a firewall that actually holds.

Set up a production Docker host

 

Set up a production Docker host

A production Docker host is a single dedicated server that runs all your containerised apps under Docker Compose: web services, databases, background workers and a reverse proxy in front of them. For most small teams it is the sweet spot between a platform-as-a-service and a full Kubernetes cluster, one machine you understand end to end, at a fixed monthly price with no per-app fees.

This guide sets one up from scratch on Ubuntu 24.04 LTS. You get Docker Engine and Compose v2 from the official repository, a clean project layout, a Caddy reverse proxy with automatic HTTPS, a worked app-plus-database stack with resource limits, tested backups, and the one firewall gotcha that catches almost everyone. Every command is copy-and-run.

Why one dedicated box is the sweet spot

A platform-as-a-service is wonderful until the bill arrives. You pay per app, per add-on database, per background worker, and the meter never stops. A single Docker host flips that: you rent one machine, and everything you can fit on it runs for the same flat price. Ten small services cost the same as one. The hardware is yours alone, so an app gets real cores and real NVMe instead of a throttled slice of a shared node.

It is also far simpler than Kubernetes. Compose files are readable, the whole stack lives in a few text files under version control, and when something breaks you debug one host with ordinary Linux tools, not a control plane, an ingress controller and a service mesh. A capable developer can run this without a platform team.

Be honest about when the other options genuinely win. A PaaS is the right call when you want zero ops and are happy to pay for it, or when your traffic is spiky and you need instant autoscaling you do not manage. Kubernetes earns its complexity when you truly need multi-node high availability, rolling deploys across a fleet, or self-healing that reschedules workloads when a machine dies. If your app must survive a single server failing, one box is not enough on its own, and that is the honest limit of this pattern. For everything below that line, which is most small and mid-size workloads, one well-run Docker host is less to learn, less to pay for and less to break.

Harden the box, then install Docker

Before Docker, secure the machine. Follow our getting-your-server-online guide first: change the delivered root password, create an admin user with an SSH key, disable password login, apply all updates, and turn on unattended security patches. A production host holds your data and your customers' data, so treat it as sensitive from minute one.

Install Docker Engine and the Compose v2 plugin from Docker's official apt repository, not the older Ubuntu package, so you get current releases and the modern "docker compose" command. These commands are for Ubuntu 24.04.

Add Docker's GPG key and repository, then install the engine, CLI, containerd, Buildx and Compose v2.

shell
sudo apt-get update
sudo apt-get install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Confirm the engine and Compose v2 both work.

shell
sudo docker run --rm hello-world
docker compose version

You can add your admin user to the docker group to drop the sudo prefix, but understand what that means: the docker group is root-equivalent. Anyone in it can mount the host filesystem into a container and become root. Only add trusted administrators, never a shared or service account, and on a multi-user box keep using sudo instead.

Daemon hygiene with daemon.json

Two daemon settings save you from the two most common Docker host incidents: a full disk from unbounded logs, and every container dying when the daemon restarts. Set them once in /etc/docker/daemon.json.

Cap container logs and keep containers running across daemon restarts.

/etc/docker/daemon.json
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  },
  "live-restore": true
}

Apply the changes.

shell
sudo systemctl restart docker

By default json-file logs grow forever; a single chatty container can fill the disk and take the whole host down. The max-size and max-file pair caps each container at 30 MB of rotated logs. live-restore keeps your containers running while the Docker daemon itself is being upgraded or restarted, so a routine daemon update no longer means downtime for everything.

One thing you should not do: expose the Docker API over TCP. Guides sometimes suggest adding a "hosts": ["tcp://0.0.0.0:2375"] line so you can control Docker remotely. That opens an unauthenticated root shell on your server to the entire internet. Leave the daemon on its local Unix socket and manage it over SSH.

Project layout: one Compose project per app

Keep each app self-contained. One directory per app under /srv/apps, each with its own compose.yaml and its own .env file. This keeps projects isolated, makes backups obvious, and means you can pull and restart one app without touching the others.

The layout: one folder per Compose project.

shell
/srv/apps/
  caddy/
    compose.yaml
    Caddyfile
  myapp/
    compose.yaml
    .env

Create an app directory. Everything for that app lives inside it.

shell
sudo mkdir -p /srv/apps/myapp

Three rules make this layout reliable. Keep persistent state in named volumes, never in the container, so an image update never touches your data. Keep secrets in the .env file and keep that file out of git: commit the compose.yaml, add .env to .gitignore, and store the real values in your password manager. And set a restart policy of unless-stopped on every service so containers come back after a reboot but stay down when you deliberately stop them.

One reverse proxy for many apps

With several apps on one host you do not want each fighting over ports 80 and 443. Put one reverse proxy in front of all of them. Caddy is the easy choice: it obtains and renews TLS certificates automatically from Let's Encrypt, so HTTPS just works once your DNS points at the server. The pattern is a shared Docker network that the proxy and every web app join; the proxy reaches each app by its service name, and no app needs to publish a port.

Create the shared network once. Every web-facing app will attach to it.

shell
docker network create proxy

The Caddy stack. Caddy is the only container that publishes 80 and 443, and it stores its certificates in a named volume so renewals survive restarts.

/srv/apps/caddy/compose.yaml
services:
  caddy:
    image: caddy:2
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config
    networks:
      - proxy

volumes:
  caddy_data:
  caddy_config:

networks:
  proxy:
    external: true

The Caddyfile. Replace YOUR_DOMAIN with your real hostname; it must have an A record (and AAAA for IPv6) pointing at this server before Caddy can get a certificate. Here it forwards to a small demo web service named hello on port 80.

/srv/apps/caddy/Caddyfile
YOUR_DOMAIN {
    reverse_proxy hello:80
}

The demo app. It joins the proxy network and publishes nothing; Caddy reaches it by name. Start Caddy first, then the app.

/srv/apps/hello/compose.yaml
services:
  hello:
    image: nginxdemos/hello:plain-text
    restart: unless-stopped
    networks:
      - proxy

networks:
  proxy:
    external: true

Bring both up. Visit https://YOUR_DOMAIN and Caddy will already have a valid certificate.

shell
docker compose -f /srv/apps/caddy/compose.yaml up -d
docker compose -f /srv/apps/hello/compose.yaml up -d

A worked example: web app plus PostgreSQL

Here is a realistic stack: a web app that talks to a PostgreSQL database on a private network, with healthchecks so Compose knows when each service is actually ready, and resource limits so one app can never starve the whole box. Put this in /srv/apps/myapp/compose.yaml and the secrets in /srv/apps/myapp/.env alongside it.

The web app is on both networks (proxy for Caddy, backend for the database) and publishes no ports. The database is on the backend network only, so it is never reachable from outside the host. mem_limit and cpus cap what each service can take.

/srv/apps/myapp/compose.yaml
services:
  web:
    image: ghcr.io/YOUR_ORG/YOUR_APP:1.4.2
    restart: unless-stopped
    env_file: .env
    depends_on:
      db:
        condition: service_healthy
    mem_limit: 512m
    cpus: 1.0
    healthcheck:
      test: ["CMD", "curl", "-fsS", "http://localhost:8080/healthz"]
      interval: 30s
      timeout: 5s
      retries: 3
    networks:
      - proxy
      - backend

  db:
    image: postgres:16
    restart: unless-stopped
    environment:
      POSTGRES_USER: appuser
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: appdb
    volumes:
      - db_data:/var/lib/postgresql/data
    mem_limit: 1g
    cpus: 1.0
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U appuser -d appdb"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - backend

volumes:
  db_data:

networks:
  proxy:
    external: true
  backend:

The .env file holds the secret and stays out of git. Generate a strong password once.

/srv/apps/myapp/.env
DB_PASSWORD=replace-with-a-long-random-string

The healthcheck command has to exist inside the image: the web check uses curl, the database check uses pg_isready, which ships in the postgres image. If your app image has no curl, swap in wget or a tiny built-in health command. Because web declares depends_on db with condition service_healthy, Compose will not start the app until the database reports ready, which removes the classic race where the app crashes on boot because the database is not up yet. Add this app's hostname to your Caddyfile exactly as with the demo service, pointing reverse_proxy at web:8080.

Updates without surprises

Updating a stack is two commands: pull the new images, then recreate the containers. Compose only recreates services whose image actually changed, and with healthchecks in place it waits for each to come up.

Pull and apply, then clean up the images the update replaced.

shell
cd /srv/apps/myapp
docker compose pull
docker compose up -d
docker image prune -f

The single most important habit here is pinning image tags. Use a specific version like postgres:16 or your-app:1.4.2, never latest. With latest you have no idea which version is running, two hosts can silently diverge, and a pull can swap in a breaking release you never chose. Pinned tags make "docker compose pull" a deliberate, reviewable act.

Auto-updaters like Watchtower can watch your registry and pull new images for you. They are convenient for a homelab, but on a production host they are a liability: an unattended pull of a new major version can break your app at three in the morning with no one watching and no easy way to know what changed. If you use one, restrict it to pinned minor tags or digest updates you have vetted. For most teams a manual pull on your own schedule, right after you have read the release notes, is the safer default.

Backups you can actually restore

Two things need backing up: your named volumes (the raw data) and your databases (a consistent logical dump). Both are one command. For volumes, run a throwaway container that mounts the volume read-only and tars it out. Note that Compose prefixes volume names with the project directory, so myapp's db_data volume is actually myapp_db_data; check with "docker volume ls".

Back up a named volume by tarring it from a temporary container.

shell
sudo mkdir -p /srv/backups
docker run --rm \
  -v myapp_db_data:/data:ro \
  -v /srv/backups:/backup \
  alpine tar czf /backup/db_data-$(date +%F).tar.gz -C /data .

For PostgreSQL, take a logical dump instead: it restores cleanly across versions. The container is named project-service-number, so here it is myapp-db-1.

shell
docker exec myapp-db-1 pg_dump -U appuser appdb | gzip > /srv/backups/appdb-$(date +%F).sql.gz

Run the dump on a schedule with a systemd timer and its service. Put your backup commands in /usr/local/bin/backup-apps.sh first and make it executable.

/etc/systemd/system/backup-apps.service
[Unit]
Description=Back up Docker app volumes and databases

[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup-apps.sh

The schedule: nightly, catching up if the machine was off.

/etc/systemd/system/backup-apps.timer
[Unit]
Description=Run app backups nightly

[Timer]
OnCalendar=*-*-* 03:30:00
Persistent=true

[Install]
WantedBy=timers.target

Enable the timer.

shell
sudo systemctl daemon-reload
sudo systemctl enable --now backup-apps.timer

A backup on the same disk as the data is not a backup; it dies with the machine. Copy the /srv/backups directory offsite, to a separate backup and storage server or object storage, on the same nightly schedule. And once, actually restore a dump into a scratch database to prove the file is good. An untested backup is a guess.

Housekeeping and monitoring

Docker accumulates cruft: old images, dangling layers and stopped containers pile up until a pull fails with no space left. Automate the cleanup with a systemd timer rather than trusting yourself to remember.

The prune service: remove unused images, networks and build cache older than a week.

/etc/systemd/system/docker-prune.service
[Unit]
Description=Prune unused Docker data

[Service]
Type=oneshot
ExecStart=/usr/bin/docker system prune -af --filter "until=168h"

The schedule: weekly.

/etc/systemd/system/docker-prune.timer
[Unit]
Description=Run docker system prune weekly

[Timer]
OnCalendar=Sun 04:00
Persistent=true

[Install]
WantedBy=timers.target

Enable the timer, then keep an eye on disk and Docker's own usage. Alert on the data volume from your monitoring well before it fills.

shell
sudo systemctl daemon-reload
sudo systemctl enable --now docker-prune.timer
df -h /
docker system df

For live per-container CPU, memory and network at a glance, ctop and lazydocker are both worth installing; they turn "docker stats" into a real dashboard in the terminal.

shell
docker run --rm -ti \
  -v /var/run/docker.sock:/var/run/docker.sock:ro \
  quay.io/vektorlab/ctop:latest

For proper metrics, run node exporter for host stats and cAdvisor for per-container stats, and scrape both with Prometheus. cAdvisor is a single container: bind it to 127.0.0.1 so only local Prometheus can reach it, then point Grafana at the pair for CPU, memory and disk graphs per container over time.

The firewall gotcha every Docker host hits

This one surprises almost everyone, so read it carefully. When you publish a port in Compose, Docker writes its own iptables rules straight into the DOCKER chain, and those rules are evaluated before ufw. The result: a port you published is open to the entire internet even though "ufw status" swears it is blocked. People bind a database to port 5432, trust ufw to keep it private, and find it exposed to the world.

The fix is simple and reliable: bind published ports to localhost. Instead of "5432:5432", which listens on every interface, write "127.0.0.1:5432:5432", which listens only on the host itself. Anything your reverse proxy fronts does not need a published port at all, because Caddy reaches it over the shared Docker network. Publish to 127.0.0.1 only for things you connect to locally, such as a metrics endpoint or a debug port.

The difference in one place. The first line is a mistake; the other two are safe.

compose.yaml
ports:
  - "5432:5432"             # WRONG: open to the internet, ufw is bypassed
  - "127.0.0.1:8080:8080"   # safe: only this host can connect
  # best of all: publish nothing, let the proxy reach it on the shared network

The absolute rule that follows from this: never publish a database port. In the worked example above the db service has no ports section at all; the web app reaches it over the private backend network, and nothing outside the host can. If you ever need to connect to it from your laptop, do it through an SSH tunnel, not a published port.

PaaS vs one Docker host vs Kubernetes

The three approaches side by side. None is wrong; the right one follows your team size, your budget and whether you truly need a single machine to be able to fail.

PaaS Single Docker host Kubernetes
Cost model Per app and add-on, climbs fast Flat monthly for the whole box Cluster plus your ops time
Ops burden Almost none You patch, back up and monitor one host High: control plane and networking
Performance A throttled shared slice Full dedicated cores and NVMe Dedicated, spread across nodes
Scaling Instant, managed for you Vertical: a bigger box, or add a second Horizontal across many nodes
Survives a host failure Yes, handled for you No, single machine Yes, reschedules workloads
Best for Zero-ops teams, spiky traffic Most small and mid-size workloads Large fleets needing HA

The short version

For most teams, one dedicated server running Docker Compose is the practical middle ground: flat cost, real performance, and little to learn. Harden the box first, install Docker from the official repo, cap your logs and turn on live-restore, keep one Compose project per app with named volumes and secrets out of git, front everything with Caddy for automatic TLS, set resource limits and healthchecks, automate backups and pruning, and bind published ports to localhost so the firewall gotcha can never bite you. Do that and you have a production Docker host you can trust.

Continue reading

Need help choosing?

Our experts can help you find the perfect server for your needs.

Contact us »