Securing a Docker application on a self-managed server
On a self-managed server, a virtual or physical Linux machine rented from a hosting provider, the owner installs and runs all of the software. When the application runs in Docker, each part of it (the web application, the database, a queue) runs as a container, and the Docker daemon, a background service with root rights, starts and connects them.1 Its security is set at two levels: the host, where SSH, the firewall and system updates are configured, and Docker, which publishes ports, holds the control socket and decides what each container may do.
Steps
1. Put a provider firewall in front of the server
Ubuntu ships the host firewall ufw, but Docker and ufw are incompatible. Docker routes traffic for published container ports through the nat table of the Linux packet filter, so those packets are redirected before they reach the INPUT and OUTPUT chains where ufw applies its rules.2 A port that ufw lists as closed stays open if a container publishes it.
A firewall run by the hosting provider sits outside the server, where Docker's rules do not reach it. A Hetzner Cloud Firewall with no rules blocks all incoming traffic and allows all outgoing traffic, and it is free of charge.3 Allow incoming TCP on port 22 for SSH and ports 80 and 443 for the web, for both IPv4 and IPv6, and nothing else.
2. Publish container ports to the local address only
By default, a published port listens on all of the host's addresses, 0.0.0.0 and [::], and accepts connections from any source.45 A port published with the loopback address in front of it can be reached only from the host itself:
docker run -p 127.0.0.1:8080:80 nginxTo make loopback the default for the default bridge network, set it in /etc/docker/daemon.json and restart the daemon:4
{
"ip": "127.0.0.1"
}In Docker releases older than 28.0.0, machines on the same local network segment can still reach ports published to loopback.4 A database, a cache or an admin tool that only other containers use needs no published port; a public one is the case described in Publicly exposed database and Exposed administration interface. Host-level filtering for published ports belongs in the DOCKER-USER chain, which Docker processes before its own rules.5
3. Keep the Docker API off the network
The Docker command line talks to the daemon through a Unix socket, /var/run/docker.sock, which has no network listener by default.6 Starting the daemon with -H tcp://0.0.0.0:2375 or similar gives anyone who can reach that port unencrypted and unauthenticated control of Docker.7 Even behind a firewall, such an endpoint stays reachable from the containers on the same host.1 For remote control, the daemon accepts connections over SSH, or over TLS on port 2376 with client certificates; anyone holding those client keys has root access to the host.6
4. Treat access to the socket as root access
Whoever can send commands to the daemon can start a container that mounts the host's root directory, so access to the socket equals unrestricted root access to the host.7 Docker's own post-installation guide says the docker group "grants root-level privileges to the user".8 Add to that group only accounts that are already trusted with root, and do not mount /var/run/docker.sock into an application container. Mounting it read-only leaves the API fully writable.7 The attack is covered in Docker socket exposure.
5. Run containers with the fewest rights
An application that runs as root inside its container gives an attacker who breaks into it a shorter path to the host. Run it as an unprivileged user instead, set with USER in the Dockerfile or -u at run time. Drop every Linux capability, add back only the ones the application needs, and set no-new-privileges, which stops processes gaining rights through setuid or setgid programs.7 The --privileged flag grants every capability.7 The command below applies Principle of least privilege to a container:
docker run -u 4000 --cap-drop all --cap-add CHOWN \
--security-opt=no-new-privileges alpine6. Allow SSH logins with keys only
OpenSSH accepts password logins unless told otherwise: PasswordAuthentication defaults to yes.9 A public SSH port then receives the password guessing described in SSH brute-force attack. Once a login with an SSH key works, set the following in /etc/ssh/sshd_config and reload the SSH service:
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
AllowUsers deployprohibit-password, the default for root, turns off password and keyboard-interactive login for that account, and AllowUsers limits login to the listed names.9 deploy stands for the login account.
7. Install security updates automatically
Containers share the host's kernel, so a kernel flaw such as Dirty COW reaches every container, and protection against container escapes such as Leaky Vessels depends on updating both the kernel and Docker Engine.7 On Ubuntu Server the unattended-upgrades package is installed by default and applies security updates from the Ubuntu archive once a day.10
A repository added later is not updated until its origin is listed under Allowed-Origins in /etc/apt/apt.conf.d/50unattended-upgrades,10 and Docker's install guide sets up Docker Engine from Docker's own repository at download.docker.com.11 Some updates take effect only after a reboot, and Unattended-Upgrade::Automatic-Reboot is false by default; the file /var/run/reboot-required shows that one is due.10
Notable case
In 2024 Datadog Security Labs described a Cryptojacking campaign, linked to the earlier Spinning YARN campaign, that "targets Docker API endpoints publicly exposed without authentication".12 The attackers scanned the internet for hosts with port 2375 open and confirmed each one with a docker version request. They then created an Alpine container with the host's root directory bound to /mnt, used chroot to work on the host's own files, and wrote cron jobs to /etc/crontab and /etc/cron.d/zzh that fetched further payloads. One payload spread the malware to other machines over SSH.12 A provider firewall (step 1) or a daemon with no TCP listener (step 3) would have left the scanners nothing to find on port 2375.
In SecFlash
On the Cloud page, SecFlash reports a Hetzner server with no firewall as SF-CLD-101 and a DigitalOcean Droplet with none as SF-CLD-201. On the Server page, a port that answers from outside appears as SF-SRV-102, a database or Docker API that does as SF-SRV-101, and a port open on IPv6 only as SF-SRV-104. Password login over SSH is SF-SRV-201. Members of the docker group are reported as SF-SRV-304, and containers that reach the Docker socket, run privileged, hold powerful capabilities or run as root as SF-SRV-402, SF-SRV-401, SF-SRV-404 and SF-SRV-409. Waiting security fixes, a pending reboot and a release near its end of support appear as SF-SRV-503, SF-SRV-501 and SF-SRV-502.
See also
- Docker socket exposure
- SSH brute-force attack
- Publicly exposed database
- Principle of least privilege
- Responding to an unrecognised server login
- Category: Self-managed servers
References
- a b Docker (n.d.). "Docker Engine security". Docker Docs.
- back Docker (n.d.). "Packet filtering and firewalls". Docker Docs.
- back Hetzner (2021). "Firewalls: Overview". Hetzner Docs.
- a b c Docker (n.d.). "Port publishing and mapping". Docker Docs.
- a b Docker (n.d.). "Docker with iptables". Docker Docs.
- a b Docker (n.d.). "Protect the Docker daemon socket". Docker Docs.
- a b c d e f OWASP (n.d.). "Docker Security Cheat Sheet". OWASP Cheat Sheet Series.
- back Docker (n.d.). "Linux post-installation steps for Docker Engine". Docker Docs.
- a b OpenSSH (n.d.). "sshd_config(5)". OpenBSD manual pages.
- a b c Canonical (n.d.). "Automatic updates". Ubuntu Server documentation.
- back Docker (n.d.). "Install Docker Engine on Ubuntu". Docker Docs.
- a b Muir, M. (2024). "Attackers deploying new tactics in campaign targeting exposed Docker APIs". Datadog Security Labs.
External links
- Rootless mode, Docker Docs
- Docker Security Cheat Sheet, OWASP