0

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 starts 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.

Do you know what is happening ?

Thanks for your answers!

0

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.

1

Ideally dont run docker as superuser(root). You should create a user within dockerfile and chown the file system with that user. You can create a user and then add access to that user on the file system. In the below example there is appuser , with ownership to src

FROM ubuntu:latest
RUN useradd -r -u 1001 -g appuser appuser
RUN chown -R appuser.appuser /src
USER appuser
CMD ["appuser", "/src/index.js"] 

Its not recommended to run as root You could also run as any user (Say root id = 0) if needed from externally

docker run -d --user 0 ubuntu:latest

You should never relay on gid/uid of the linux user outside the container.

1
  • Thank you for your answer. But I don't run the container with root. I have a user in my Docker file (application). But even with the chown, it mounts the files with 1001:1001 : i.imgur.com/AgzWEDG.png And my docker run -v ${PWD}:/app [...] set bad user/group permissions : i.imgur.com/Dr0uFds.png Any idea ? – Boris Nov 7 '20 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.