Chris Maes
Chris Maes

Reputation: 37722

docker container process running as non-root user cannot write to docker volume

TLDR

What is then good practice when running a container process as a non-root user and I want to write to a (cloudstor, aws-ebs) docker volume.


Long story

It is considered bad practice in (and outside) docker containers to run processes as root (see for example ref1, ref2, ...). This might have security implications.

But when we start using docker volumes and that non-root user tries to write to the volume, the trouble begins. I fail to find a clean solution that will work on cloud infrastructure, without manual intervention. The working solutions I found all seem to fall short on some point (security, maintainability, ...).

As a side-note, we are deploying on docker-swarm using cloudstor to provision aws-ebs volumes. We hope to move to kubernetes one day, but we don't have kubernetes yet, so we try to find an alternative solution for our setup.

Solutions / Workarounds considered

1. Pre-create the volume within the docker image

As proposed here, if docker-compose creates a new volume, the permissions on the directory inside the image will be propagated.

downsides:

2. Use volumes-provisioner

hasnat created a volumes-provisioner image which could set the correct folder permissions just before the real container starts.

downsides:

3. Use docker run to correct file permissions

Once the real container is running with the volume mounted (but still with the wrong permissions), we call

docker run --rm -u root -v ${MOUNT}:${TARGET} { real_image } chown -R user:group ${TARGET}

downsides:

4. Change ownership when starting the container

This implies:

downsides:

5. Just run as root

This is the easiest solution, but then what about security? And everybody recommending not to do this?

6. Use kubernetes

As suggested here, with kubernetes we can assign a group id to a volume. This seems confirmed in the kubernetes documentation for pods.

downsides:

7. create folders on the file system with correct permissions

Make sure directories exist on the file system with the correct owner/permissions.

downsides

Upvotes: 10

Views: 5429

Answers (1)

Cyril G.
Cyril G.

Reputation: 2017

I vote for the solution 4, there is no security issue to change the permissions as root then to start your application as non root. If there is a security hole in your application, the application is still not running as root whatever happened before it started. You can do this in a script use in the entrypoint.

Upvotes: 3

Related Questions