Docker Security: The Lost Guide for Developers
Learn how to protect your network from threats and dangerous configuration!
Table of Contents
- ⚠️ Local Networks at Risk
- 🛡️ Firewall Configuration
- 🔐 Secrets Management for Local Development
- 🕵️ Credential Leaks and Side-Channel Attacks
- 🔍 Monitoring & Canary Tokens
- ❌ Common Misconceptions
⚠️ Local Networks at Risk
Let’s be honest, we’ve all done it. You’ve connected to a random coffee shop Wi-Fi or let someone use your home network without a second thought. Maybe you even trust your smart fridge not to compromise your network. The reality? These casual decisions can expose your local development setup to unnecessary risks. Attackers don’t just target production systems—local environments are often softer targets, offering a way to access sensitive projects.
Attack Scenarios
- Intercepted Traffic: Unencrypted traffic can easily be captured and read.
- Unprotected Services: Local databases or APIs exposed on
0.0.0.0
. - Network Spoofing: Redirects traffic to an attacker’s device.
Quick Fixes
- Prefer private docker networks over firewalls to limit network exposure.
- Avoid public or shared Wi-Fi; prefer using your phone’s hotspot.
- Monitor your local network for unknown devices using tools like
arp-scan
andnmap
.
🛡️ Firewall Configuration
UFW with Docker (Ubuntu)
⚠️ Warning: By default Docker on Ubuntu/Debian will bypass UFW/iptables rules, potentially exposing your system to attacks. It doesn’t matter if you bind ports to local IP addresses (e.g.
-p 127.0.0.1:8080:80
.)
This surprises me every time I learn about it! Docker bypasses UFW rules by default, allowing containers to communicate with the host and other containers without restriction.
Best Practice
- 🥇 Use Docker Networks to isolate and control what can connect to each container or network.
- 🥉 Update iptables if you must use a
host
network, or cannot use custom networks, you can mitigate the risk by configuring iptables. Not for the faint-hearted, check out utility below.
Docker Network Isolation
UFW Configuration (for host
networks)
There’s a lot of bad advice on fixing this out there. configure UFW to work with Docker using UFW largely like you might expect.
I’ve used ufw-docker
to configure a self hosted system and it seems to works well.
This command performs the following:
- Backs up the file
/etc/ufw/after.rules
. - Appends Docker-related rules at the end of the file to integrate properly with UFW.
Source: ufw-docker GitHub
Example Usage:
Note: Most “fixes” for Docker-UFW conflicts involve manual iptables rules, which can be error-prone and fragile during updates.
macOS Firewall
- Go to System Preferences > Security & Privacy > Firewall.
- Enable the firewall and click “Firewall Options.”
- Block all incoming connections except essential services.
Note: You may need to lookup configuration for your firewall to allow certain smart devices you use - e.g. Google Cast/AirPlay and other services.
Commands for Advanced Users (macOS and Linux)
macOS:
Linux (ufw):
Pro Tip: Use tools like Little Snitch on macOS and ufw on Linux for more user-friendly configurations.
🔐 Secrets Management for Local Development
Proactive Placeholder Validation
💡 Ensure secrets are properly setup with real values before running your application.
If you use placeholders like __WARNING_REPLACE_ME__
in your secrets, greaet, maybe someone will notice. Just in case, you can also add a little validation to provide safety at runtime.
You wouldn’t believe how easy it is to completely hack (modify & re-sign) a JWT token when attackers can guess the secret!
Generating and Storing Secrets
Never hardcode secrets in your codebase. Prefer environment variables and secure vaults.
Instead of .env.example
, use .env.generate.sh
to make it easy for users to get a .env
file with secure “defaults.”
Example .env.generate.sh
🕵️ Monitoring & Double-checking
nmap
Examples
Testing Inside Your Network
Testing Outside Your Network
To can lookup your current (public) IP easily with services like ifconfig.me
: curl https://ifconfig.me
.
Use an external network or remote server to test your public IPs:
Why Test Both? Testing from inside reveals internal exposure, while external tests identify services accessible to attackers.
🛡️ Common Misconceptions
- My local environment isn’t a target.
- Fact: Attackers can pivot from your machine to your production systems.
- Firewalls block everything.
- Fact: They only block what you configure them to.
- Private IPs are secure.
- Fact: Exploits like NAT bypasses can still affect your network.