Docker-compose set user and group on mounted volume

The bad news is there's no owner/group/permission settings for volume 😢. The good news is the following trick will let you bake it into your config, so it's fully automated 🎉.

In your Dockerfile, create an empty directory in the right location and with desired settings.

This way, the directory will already be present when docker-compose mounts to the location. When the server mounts during boot (based on docker-compose), the mounting action happily leaves those permissions alone.

Dockerfile:

# setup folder before switching to user
RUN mkdir /volume_data
RUN chown postgres:postgres /volume_data
USER postgres

docker-compose.yml

volumes:
   - /home/me/postgres_data:/volume_data

source


To achieve the desired behavior without changing owner / permissions on the host system do the following steps.

  1. get the ID of the desired user and or group you want the permissions to match with executing the id command on your host system - this will show you the uid and gid of your current user and as well all IDs from all groups the user is in.

     $ id
    
  2. add the definition to your docker-compose.yml

     user: "${UID}:${GID}"
    

    so your file could look like this

     php: # this is my service name
         user: "${UID}:${GID}" # we added this line to get a specific user / group id
         image: php:7.3-fpm-alpine # this is my image
     # and so on
    
  3. set the values in your .env file

     UID=1000
     GID=1001
    

3a. Alternatively you can extend your ~/.bashrc file with:

    export UID GID

to define it globally rather than defining it in a .env file for each project. If this does not work for you (like on my current distro, the GID is not set by this, use following two lines:

    export UID=$(id -u)
    export GID=$(id -g)

Thanks @SteenSchütt for the easy solution for defining the UID / GID globally.

Now your user in the container has the id 1000 and the group is 1001 and you can set that differently for every environment.

Note: Please replace the IDs I used with the user / group IDs you found on your host system. Since I cannot know which IDs your system is using I gave some example group and user IDs.

If you don't use docker-compose or want to know more different approaches to achieve this have a read through my source of information: https://dev.to/acro5piano/specifying-user-and-group-in-docker-i2e

If the volume mount folder does not exist on your machine, docker will create it (with root user), so please ensure that it already exists and is owned by the userid / groupid you want to use.

I add an example for a dokuwiki container to explain it better:

version: '3.5'
services:
  dokuwiki:
    user: "${UID}" # set a specific user id so the container can write in the data dir
    image: bitnami/dokuwiki:latest
    ports:
      - '8080:8080'
    volumes:
      - '/home/manuel/docker/dokuwiki/data:/bitnami/dokuwiki/'
    restart: unless-stopped
    expose:
      - "8080"

The dokuwiki container will only be able to initialize correctly if it has write access to the host directory /home/manuel/docker/dokuwiki/data.

If on startup this directory does not exist, docker will create it for us but it will have root:root as user & group. --> Therefor the container startup will fail.

If we create the folder before starting the container

mkdir -P /home/manuel/docker/dokuwiki/data

and then check with

ls -nla /home/manuel/docker/dokuwiki/data| grep ' \.$'

which uid and gid the folder has - and check that they match the ones we put in our .env file in step 3. above.