brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: run a container with --read-only and a small tmpfs

Security
~/tips/tip-docker-read-only-tmpfs

docker run -d --read-only \

Makes a container's filesystem read-only, with writable memory-backed folders only where you allow them.

Most services never need to write to their own filesystem. Docker lets you enforce that, and it takes one flag.

--read-only mounts the container’s root filesystem read-only. Anything that really needs to write gets its own writable spot, such as a volume or a tmpfs.

docker run -d --read-only \
  --tmpfs /tmp:rw,noexec,nosuid,size=64m \
  -v appdata:/data \
  myimage

The tmpfs lives in memory and vanishes with the container. The options after it are the same ones you would give to mount -t tmpfs -o. In compose the matching keys are read_only: true and tmpfs: [/tmp].

It limits the damage if something in the container gets compromised, since it can no longer drop files into the filesystem. It is not a complete defence, but it is cheap, and it fits well with running as a non-root user.

The caveat is that apps are not always honest about where they write. Expect the first start to fail with a permission error on some path such as /run or a cache directory. Read the error, add that path as a tmpfs or a volume, and try again. I think the extra ten minutes are worth it for anything facing the internet.

Further reading: docker container run: read-only and tmpfs, On this site: docker update –restart changes the policy without recreating the container, On this site: docker cp works on stopped containers too

More Security tips tcpdump -nn -r capture.pcap port 53 mkcert -install Strict-Transport-Security: max-age=0 Referrer-Policy: strict-origin-when-cross-origin wp super-admin list All Security tips (79) All topics

Link