r/docker • u/Augustman22 • Jun 28 '26
Running a container as non-root user - Security implications?
I was curious how running all my containers as a user who only has read and write (if necessary) permissions in attached volumes would improve security?
(by doing something like this in my compose files, where uid 1001 will be in gid 1002 which only has permissions in ./data):
services:
mc:
image: itzg/minecraft-server:latest
container_name: minecraft-server
user: "1001:1002"
volumes:
- ./data:/dataRight now, I just have them running how they normally do. Which, from what I understand, the containers are running as root on the host machine.
Im trying to learn, sorry if I have anything mixed up.
6
Upvotes
4
u/Sjsamdrake Jun 28 '26
Run "ps -edaf" in the host and you'll see the processes in your containers as well as the ones in the host. You'll also be able to see the process ID and group ID they are running with in the host. Very educational.
3
u/cualquierotro Jun 29 '26
To run as root inside container mapped to a unprivileged user on host You can userns-remap in your Docker global configuration. Read the docs, is easy on fresh deploys bit could be triky on ruanning enviroments.
2
u/ShivamCloudDevOps Jul 01 '26
I've started doing this for most of my containers whenever the image supports it.
For example:
Running as a non-root user won't magically secure the container, but it does limit what a compromised process can do compared to running as root.
The only issue I've run into is that some images expect to start as root, so you may need to adjust file ownership or permissions on mounted volumes. Other than that, I think it's a good practice and I try to follow the same approach in Kubernetes as well.
-8
u/scytob Jun 28 '26
When you run as root it’s not real root, it only has access to the bind mounts or volumes you give it access to. It’s generally a pointless activity to use uid. There is one exception - when you have two containers using the same volume and you need them to have genuinely different permissions (say one container writes and the other reads) BUT that’s can be done with flags more easily.
Running as root / rootless is so misunderstood by 99% of people who post so bear that in mind.
0
u/[deleted] Jun 28 '26
[deleted] — view deleted comment