Docker Compose set user and group on a mounted volume
Codemia Knowledge Hub ·
Set the user key on the service in docker-compose.yml (for example user: "1000:1000") so the container process runs with a UID and GID that have the right permissions on the mounted volume. Alternatively, change ownership of the mounted path with an entrypoint script, or set USER in the Dockerfile.
Why mounted volumes cause permission errors
When you mount a volume from the host into a container, Docker keeps the ownership and permissions as they exist on the host. On Linux, user and group IDs (UIDs and GIDs) control access to files, so if the IDs inside the container differ from those on the host, processes in the container hit permission denied errors when reading or writing the volume.
There are three ways to fix it:
- Run the container with a specific UID/GID that has the correct permissions on the volume.
- Change ownership of the volume with an entrypoint script.
- Set the user in the Dockerfile.
Running the service as a specific UID/GID
Specify the user that starts the container process in docker-compose.yml:
The container runs with UID 1000 and GID 1000, which must match the ownership of the data directory on the host to prevent access issues.
Changing ownership in an entrypoint script
An entrypoint script can fix ownership and permissions once the container starts:
Make the script executable and point ENTRYPOINT at it in your Dockerfile:
Setting USER in the Dockerfile
Placing the USER instruction before any command that touches the mounted volume also addresses the problem:
Example: a web app storing uploaded files
For a web application that stores user uploads on a mounted volume, either start the web server with a UID/GID matching the upload directory on the host, or use an entrypoint script that changes permissions at startup so each deployment works across environments.
Quick reference
| Aspect | Solution | Example |
| User and Group IDs | Use explicit UID and GID | user: "1000:1000" in Docker Compose |
| Volume Ownerships | Modify with scripts | chown -R in entrypoint |
| Dockerfile Configuration | Set USER in Dockerfile | USER 1000:1000 |
| Permission Errors | Check matching host and container UIDs/GIDs | Ensure host paths are accessible by the UID |
Related reading
- docker-compose up for only certain containers
- docker-compose up vs docker-compose up --build vs docker-compose build --no-cache
- Docker-compose volume mount before run
- docker-compose volumes_from equivalent with version 3
- Docker-compose with django could not translate host name db to address Name or service not known
- Docker-Desktop for MAC Kubernetes not creating secrets or token for serviceaccounts
- docker-exec failed cd executable file not found in PATH
- Docker-ized Consul, Zookeeper and Kafka in Amazon-ECS
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.