Docker Read-Only Sandbox: Cap-Drop, Allowlist & iptables
Trap risky tool runs in Australia with a read-only Docker sandbox (--cap-drop=ALL), Bash regex allowlists, sed API-key redaction, and iptables UID egress locks—plus when to book Fixwebnode.
This guide is for Australian homeowners, sole traders, and small-business operators who run CLI tools, deploy scripts, or let staff invoke helpers on Linux hosts and need those executions trapped before they leak keys or phone home.
By the end you will stand up a read-only Docker sandbox with --cap-drop=ALL, validate every command through a strict Bash regex allowlist, automatically redact leaked API keys with sed, and lock outbound traffic to a dedicated UID via iptables. Work assumes a recent Ubuntu or Debian host with Docker Engine installed, root or sudo access, and remote SSH. If you need hands-on remote help, start at Fixwebnode website repair Australia.
Why a trapped tool sandbox matters in Australia
Unrestricted tool runners on a VPS or office NUC often run as root, keep full Linux capabilities, write anywhere on the host, and open arbitrary outbound sockets. One compromised npm script or a pasted curl one-liner can exfiltrate .env secrets to a foreign host. Australian operators face the same failure modes as anyone else: noisy CI agents, shared jump boxes, and “just run this” vendor installers. A coherent control plane—read-only container rootfs, dropped capabilities, command allowlisting, secret scrubbing, and UID-scoped egress—cuts blast radius without blocking legitimate work.
Fixwebnode works as a direct remote specialist for individuals and local operators across Australia. We harden the sandbox path itself rather than handing you a generic checklist. Service coverage is listed under Fixwebnode service areas.
Why does my Docker tool sandbox still leak keys or open outbound sockets in Australia?
Most leaks happen because the container still has CAP_NET_ADMIN or a writable mount, the wrapper script accepts any argv, logs dump secrets before redaction, or outbound rules are bound to the wrong UID. Fix the four layers together: read-only rootfs with --cap-drop=ALL, a Bash regex allowlist, sed redaction on stdout/stderr, and iptables OUTPUT rules owned by the sandbox UID.
| Symptom | Quick fix | When to call Fixwebnode |
|---|---|---|
Container can still chmod or load modules | Add --read-only --cap-drop=ALL --security-opt=no-new-privileges | Host policy or AppArmor conflicts block the flags |
| Wrapper runs unexpected binaries | Enforce a strict [[ $cmd =~ ^…$ ]] allowlist | Need multi-user policy design |
| API keys appear in CI logs | Pipe all output through sed redaction | Keys already rotated poorly or spread across hosts |
| Sandbox still reaches the internet | iptables OWNER match on the tool UID | nftables/firewalld clash or cloud security groups |
Common issues with trapped tool executions
These four problems show up repeatedly when people first try to sandbox tools on Australian VPS and office Linux boxes.
- Read-only flag ignored or capabilities still present — tools create files under
/tmpon the host, orcapsh --printinside the container still lists CAP_SYS_ADMIN. - Allowlist is too loose or bypassed — operators use
eval, unquoted expansions, or allow.*, socurl|bashstill runs. - API keys leak in logs before redaction — wrappers tee raw stdout to journald; patterns miss
sk_live_, AWS keys, or Bearer tokens. - iptables UID rules never match — the process runs as root inside the container namespace, so OWNER uid rules on the host never fire, or Docker’s DOCKER-USER chain is skipped.
Trap tool runs in a read-only Docker sandbox with --cap-drop=ALL
Create a dedicated non-root host user, a tiny runner image, and a launch line that drops every capability, freezes the rootfs, and mounts only explicit scratch volumes.
Step 1 — Create the sandbox user and group
sudo groupadd --system toolbox
sudo useradd --system --gid toolbox --home-dir /var/lib/toolbox \
--shell /usr/sbin/nologin toolbox
sudo mkdir -p /var/lib/toolbox/work /var/lib/toolbox/logs
sudo chown -R toolbox:toolbox /var/lib/toolbox
id toolbox
Note the numeric UID from id toolbox; you will need it for iptables OWNER rules.
Step 2 — Minimal runner Dockerfile
mkdir -p ~/toolbox-image && cd ~/toolbox-image
cat > Dockerfile <<'EOF'
FROM debian:bookworm-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends bash coreutils sed ca-certificates \
&& rm -rf /var/lib/apt/lists/* \
&& useradd --create-home --uid 10001 --shell /bin/bash runner
USER runner
WORKDIR /work
ENTRYPOINT ["/bin/bash","/usr/local/bin/allowrun"]
EOF
Step 3 — Build and launch with hard locks
docker build -t local/toolbox:locked .
docker run --rm -i \
--name toolbox-run \
--read-only \
--cap-drop=ALL \
--security-opt=no-new-privileges:true \
--network=none \
--user 10001:10001 \
--tmpfs /tmp:rw,noexec,nosuid,size=64m \
--tmpfs /work:rw,noexec,nosuid,size=128m \
-v /var/lib/toolbox/work:/input:ro \
-v /var/lib/toolbox/logs:/logs:rw \
local/toolbox:locked
--network=none is the default deny; you will re-enable a filtered path later only after UID rules exist. Verify capabilities:
docker run --rm --cap-drop=ALL --read-only --user 10001:10001 \
local/toolbox:locked -c 'grep CapEff /proc/self/status'
Expect CapEff of 0000000000000000. If not, a parent runtime or Kubernetes securityContext is re-adding caps—stop and fix that before continuing.
Validate commands with a strict Bash regex allowlist
Never pass raw user strings to bash -c. Install an entrypoint that accepts only exact verb patterns.
Step 1 — Write the allowlist entrypoint on the image build context
cat > allowrun <<'EOF'
#!/bin/bash
set -euo pipefail
PATH=/usr/bin:/bin
cmd=${1:-}
shift || true
# Strict allowlist: fixed verbs + safe argv charset only
allowed='^(list|hash|stat|version)$'
if [[ ! "$cmd" =~ $allowed ]]; then
echo "deny: command not allowlisted" >&2
exit 126
fi
for arg in "$@"; do
if [[ ! "$arg" =~ ^[A-Za-z0-9._/:@+-]+$ ]]; then
echo "deny: illegal characters in argv" >&2
exit 126
fi
done
case "$cmd" in
list) exec /bin/ls -1 /input "$@" ;;
hash) exec /usr/bin/sha256sum "$@" ;;
stat) exec /usr/bin/stat "$@" ;;
version) exec /bin/bash --version ;;
esac
EOF
chmod 0755 allowrun
Copy it into the image (add COPY allowrun /usr/local/bin/allowrun before USER in the Dockerfile) and rebuild.
Step 2 — Host-side wrapper that never evals
sudo tee /usr/local/bin/toolbox-run >/dev/null <<'EOF'
#!/bin/bash
set -euo pipefail
cmd=${1:?usage: toolbox-run <list|hash|stat|version> [args]}
shift
exec docker run --rm -i \
--read-only --cap-drop=ALL \
--security-opt=no-new-privileges:true \
--user 10001:10001 \
--tmpfs /tmp:rw,noexec,nosuid,size=64m \
--tmpfs /work:rw,noexec,nosuid,size=128m \
-v /var/lib/toolbox/work:/input:ro \
-v /var/lib/toolbox/logs:/logs:rw \
local/toolbox:locked "$cmd" "$@"
EOF
sudo chmod 0755 /usr/local/bin/toolbox-run
Step 3 — Verify allow and deny paths
toolbox-run version
toolbox-run list
toolbox-run 'curl' https://example.com || echo "blocked as expected"
toolbox-run hash '/input/../etc/passwd' || echo "path charset blocked"
Any use of eval, unquoted $@ re-join, or an allow regex ending in bare .* re-opens injection. Keep the regex anchored with ^ and $.
Automatically redact leaked API keys using sed
Even allowlisted tools can print environment fragments. Scrub stdout and stderr before they hit disk or CI.
Step 1 — Shared redaction filter
sudo tee /usr/local/lib/toolbox-redact.sed >/dev/null <<'EOF'
s/AKIA[0-9A-Z]\{16\}/AKIA_REDACTED/g
s/ASIA[0-9A-Z]\{16\}/ASIA_REDACTED/g
s/sk_live_[0-9a-zA-Z]\{20,\}/sk_live_REDACTED/g
s/sk_test_[0-9a-zA-Z]\{20,\}/sk_test_REDACTED/g
s/ghp_[0-9A-Za-z]\{36\}/ghp_REDACTED/g
s/xox[baprs]-[0-9A-Za-z-]\{10,\}/SLACK_TOKEN_REDACTED/g
s/Bearer [A-Za-z0-9._~+\/-]\{20,\}/Bearer REDACTED/g
s/[0-9a-f]\{32,64\}/HEX_SECRET_REDACTED/g
EOF
Step 2 — Wire redaction into the host wrapper
sudo tee /usr/local/bin/toolbox-run >/dev/null <<'EOF'
#!/bin/bash
set -euo pipefail
cmd=${1:?usage: toolbox-run <cmd> [args]}
shift
log=/var/lib/toolbox/logs/run-$(date +%Y%m%d-%H%M%S).log
{
docker run --rm -i \
--read-only --cap-drop=ALL \
--security-opt=no-new-privileges:true \
--user 10001:10001 \
--tmpfs /tmp:rw,noexec,nosuid,size=64m \
--tmpfs /work:rw,noexec,nosuid,size=128m \
-v /var/lib/toolbox/work:/input:ro \
-v /var/lib/toolbox/logs:/logs:rw \
local/toolbox:locked "$cmd" "$@" 2>&1 \
| sed -f /usr/local/lib/toolbox-redact.sed
} | tee "$log"
EOF
sudo chmod 0755 /usr/local/bin/toolbox-run
sudo chown toolbox:toolbox /var/lib/toolbox/logs
Step 3 — Prove redaction
printf 'token=sk_live_ABCDEFghijklmnopqrstuv Bearer eyJhbGciOiJIUzI1NiJ9.payload.sig\n' \
| sed -f /usr/local/lib/toolbox-redact.sed
You should see only sk_live_REDACTED and Bearer REDACTED. Extend the sed file when you adopt new vendors; never log raw env dumps “just for debug” outside this pipe.
Lock down outbound network traffic with iptables UID rules
Containers that need limited egress should not inherit the host’s default ACCEPT. Bind OUTPUT drops to the sandbox UID on the host network namespace, and only punch narrow allows.
Step 1 — Confirm the host UID that will own egress
If you run the container with --user 10001:10001 and --network=host is never used, packet OWNER matches the host docker daemon path differently than a host-binary runner. Prefer a host-side proxy user: run a tiny egress helper as UID toolbox, or map the container to the toolbox UID on a custom bridge. Practical pattern for tool runners that must call one API:
TB_UID=$(id -u toolbox)
echo "toolbox uid=$TB_UID"
Step 2 — Baseline OUTPUT policy for that UID
TB_UID=$(id -u toolbox)
# Drop all new outbound from toolbox unless explicitly allowed
sudo iptables -I OUTPUT 1 -m owner --uid-owner "$TB_UID" -j DROP
# Allow established replies
sudo iptables -I OUTPUT 1 -m owner --uid-owner "$TB_UID" -m state --state ESTABLISHED,RELATED -j ACCEPT
# DNS to local resolver only (adjust IP to your resolv.conf)
sudo iptables -I OUTPUT 1 -m owner --uid-owner "$TB_UID" -p udp --dport 53 -d 127.0.0.53 -j ACCEPT
sudo iptables -I OUTPUT 1 -m owner --uid-owner "$TB_UID" -p tcp --dport 53 -d 127.0.0.53 -j ACCEPT
# Example: allow HTTPS only to one API endpoint IP (pin it)
sudo iptables -I OUTPUT 1 -m owner --uid-owner "$TB_UID" -p tcp -d 203.0.113.10 --dport 443 -j ACCEPT
sudo iptables -L OUTPUT -n -v --line-numbers
Step 3 — Run the tool process as that UID
# Host binary path (no Docker network escape):
sudo -u toolbox -- curl -sS -o /dev/null -w '%{http_code}\n' https://203.0.113.10/ || true
# Expect failure to other destinations:
sudo -u toolbox -- curl -sS --max-time 5 https://1.1.1.1/ || echo "egress blocked OK"
For Docker, keep --network=none unless you attach a custom bridge and run a userspace forwarder owned by toolbox. Do not open DOCKER-USER wide open. Persist rules with iptables-save:
sudo sh -c 'iptables-save > /etc/iptables/rules.v4'
sudo apt-get update && sudo apt-get install -y iptables-persistent
If the host uses nftables exclusively, translate the same OWNER logic rather than mixing tools blindly—mismatched stacks are a frequent Australian VPS footgun after cloud-image upgrades.
How to fix each issue (DIY runbooks)
Issue 1 — Capabilities or writes still escape the sandbox
Symptoms: files appear under host /var/lib/docker bind paths you did not mount; CapEff is non-zero; package installs inside the container succeed.
- Relaunch with explicit
--read-only --cap-drop=ALL --security-opt=no-new-privileges:true. - Replace writable binds with
--tmpfs …,noexec,nosuidand one ro input mount. - Re-check
grep CapEff /proc/self/statusand attempttouch /bin/evil(must fail). - If Kubernetes or rootless Docker strips flags, fix the runtime class before adding more tools.
Call Fixwebnode when the orchestrator securityContext fights Docker flags or when AppArmor profiles deny even the locked launch line.
Issue 2 — Allowlist bypass and unexpected binaries
Symptoms: toolbox-run accepts bash, pipes, or paths with spaces; audit logs show /proc/self/exe pointing at unexpected ELFs.
- Remove every
eval,bash -c "$user", and unanchored regex. - Keep a case/esac map from verb to absolute binary path only.
- Reject argv that fails
^[A-Za-z0-9._/:@+-]+$. - Test deny cases daily from cron and alert on exit 126 spikes.
Book a specialist when multiple teams need different verbs and you require signed policy packs rather than one shell file.
Issue 3 — Secrets still visible in logs
Symptoms: GitHub Actions annotations or journalctl show sk_live_, AKIA…, or long Bearers.
- Force all tool output through
sed -f /usr/local/lib/toolbox-redact.sedbeforetee. - Extend patterns for every vendor you use; test with known fake keys.
- Rotate any key that ever hit an unredacted log; treat the log as compromised.
- Disable debug env dumps in CI for the sandbox job.
Engage Fixwebnode if keys may have crossed multiple hosts or SaaS log drains and you need a structured rotation plan.
Issue 4 — iptables UID rules never match
Symptoms: curl as toolbox still reaches arbitrary IPs; counters on OWNER rules stay zero; Docker published ports bypass OUTPUT expectations.
- Confirm numeric UID with
id -u toolboxand that the process you test is that UID (ps -o uid,cmd -u toolbox). - Insert OWNER rules in OUTPUT with ESTABLISHED first, then narrow allows, then DROP.
- Avoid
--network=hostfor untrusted tools; prefer--network=noneplus an explicit proxy user. - Check cloud security groups separately—host iptables cannot override an open provider firewall the other way around, but provider allows do not replace UID drops.
Call Fixwebnode when firewalld, ufw, and raw iptables fight each other, or when you need a durable nftables translation on newer Debian clouds.
When DIY is enough vs when to book Fixwebnode
DIY is enough when you control a single Linux host, can rebuild one runner image, and the four layers above verify cleanly with the commands given. Stop DIY and book remote help when production CI must keep running during the change, when several UIDs and bridges interact, when keys already leaked into third-party logs, or when container runtimes ignore your capability drops.
Fixwebnode is a direct specialist provider for remote sandbox hardening—not a freelance marketplace. Australian sole traders and small teams use the same intake path as larger operators: describe the host OS, Docker version, and failure symptom, then schedule a remote session.
Book a remote sandbox hardening session
If your tool runner still writes to the host, accepts unsafe argv, prints live keys, or phones home despite iptables, bring the logs and the docker run line to a specialist session. Start the conversation at https://fixwebnode.com.au/website-repair-australia and outline whether you need allowlist design, redaction coverage, or UID egress locks. For geography and remote coverage notes, see all Fixwebnode service areas.
Lock the rootfs, drop every capability, allow only the verbs you mean, scrub secrets on the way out, and make outbound traffic earn its UID match. That is the full trap path—run it once carefully, then keep the tests in cron.