Docker
Docker-Compose
Volume-Mount
User-Permissions
Group-Permissions

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:

  1. Run the container with a specific UID/GID that has the correct permissions on the volume.
  2. Change ownership of the volume with an entrypoint script.
  3. 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:

yaml
1version: "3.7"
2
3services:
4  app:
5    image: your-image:latest
6    user: "1000:1000" # Set the UID and GID
7    volumes:
8      - data:/app/data
9
10volumes:
11  data:

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:

bash
1#!/bin/bash
2
3# Modify the ownership and permissions on the mounted volume
4chown -R user:group /app/data
5
6# Proceed with the application startup
7exec "$@"

Make the script executable and point ENTRYPOINT at it in your Dockerfile:

dockerfile
1FROM base-image:latest
2
3COPY entrypoint.sh /usr/local/bin/entrypoint.sh
4RUN chmod +x /usr/local/bin/entrypoint.sh
5
6ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
7CMD ["your-main-process"]

Setting USER in the Dockerfile

Placing the USER instruction before any command that touches the mounted volume also addresses the problem:

dockerfile
1FROM base-image:latest
2
3# Specify non-root user
4USER 1000:1000

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

AspectSolutionExample
User and Group IDsUse explicit UID and GIDuser: "1000:1000" in Docker Compose
Volume OwnershipsModify with scriptschown -R in entrypoint
Dockerfile ConfigurationSet USER in DockerfileUSER 1000:1000
Permission ErrorsCheck matching host and container UIDs/GIDsEnsure host paths are accessible by the UID

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design