HiveBrain v1.2.0
Get Started
← Back to all entries
patterndockerMinor

Docker mounts files as uid 1001

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
uiddockermounts1001files

Problem

I have a Docker image that runs a PHP application. Everything works fine on my machine and the ones of my colleagues.

However, some colleagues got a new computer that runs on Ubuntu 20.04 and by default the user on these computers have the user id 1001. If I run the id command on these computers I get 1001 for the user id and group id. So, when they start the container, the files are chown with 1001:1001 and the code cannot be executed and no files can be written in the document root of the project.

How to fix this issue?

Solution

Sounds like you are using a host volume. Docker doesn't perform any uid/gid mapping on the files mounted into the container, this is how bind mounts work in Linux. On Windows and Mac, Docker Desktop may do a convenience mapping to eliminate permission issues, but that's a feature of those platforms that doesn't exist in Linux. You'll either need to configure the container to work with the volume mount, make the image match the uid/gid on the host, or avoid host mounts.

For the first option, in development I often start containers as root, have them adjust the container user to match the volume owner, and then use gosu to drop from root to the container user to run the app. I implement this and include an example in my base image repo. See the bin/fix-perms script that adjusts the container uid/gid, bin/entrypointd.sh that looks for the RUN_AS variable to call gosu, and examples/nginx/entrypoint.d/10-fix-perms.sh that runs the fix-perms script.

Context

StackExchange DevOps Q#12713, answer score: 3

Revisions (0)

No revisions yet.