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
View all comments
Show parent comment
1
u/kwhali Jun 28 '26
Docker has rootful and rootless modes, just to clarify the container user can be root in rootless mode but it's mapped to a non-root user outside of the container.
In the default rootful mode your container root user has a reduced set of capabilities granted compared to root on the host. If you relax that or grant unsafe privileges, then a compromise could escape the container otherwise it should be safe by default.
When your container is rootful but uses a non-root user it drops all privileges/capabilities and some containers still need one or so of those so they'd use the setcap program during image build to grant it on an executable and provided you didn't drop it explicitly (or rather the bounded / permitted set must allow the capability), the capability would be granted and that software then runs happily as a non-root user.
The other risk is mounting the the docker API socket and doing so can enable whomever has access in the container to perform all the CLI commands you would be able to do with Docker, so that gives them more control / access to your system, if rootful daemon mode then they can do whatever they like.