Most of the Dockerfile you will find on Internet build and run softwares as root ! This must scare everybody, right ? ... but it doesn't seems to be the case ...
So the pb is that running a server as root, even in a container, IS DANGEROUS, because root inside a container is quite the same as root outside the container.
One of the solution is to build a Dockerfile properly by using "USER" instruction like this example for a tor relay.
Another solution is to use the "linux user namespaces" to "map" UID/GID inside container to UID/GID outside a container. for exemple root (uid=0) inside a container can be mapped to your personal user account inside the host, so files created in a shared volume has good permissions.
So my question is :what is the best practice when it comes to security with Docker ? run code as non root (i.e. USER instruction in a Dockerfile) ? Or by using "user namespaces" ? Or eventually (or additionnally) by using selinux and/or AppArmor ?
Thanks :)
Quoting Solomon Hykes
So as far as best practices go yes to the namespaces and apparmor or selinux, if you are serious about security. That being said a lot of people don't care enough to go to the extra trouble (For better or worse) so you see a lot of people don't go to the trouble. Setting permissions for users on files inside the container (specially ones mounted as volumes) gets tricky sometimes and that is way a lot of people skip the extra overhead.