Managing File Permissions with Docker Containers
When running a Docker container in privileged mode (with root permissions), files created or modified in mounted directories may end up owned by root instead of your user account. This can cause permission issues when accessing these files outside the container.
Solution: Pass Host User/Group IDs to the Container
Step 1: Pass user and group IDs as environment variables
docker run -v $(pwd):/work \
-e HOST_UID=$(id -u) \
-e HOST_GID=$(id -g) \
image_name
Step 2: Reset permissions before container exit
Add this command in your container script or run it manually before exiting:
chown -R ${HOST_UID}:${HOST_GID} /work
This ensures all files in the mounted directory maintain the same ownership as your host user, avoiding permission problems when working with the files outside the container.