Docker volume permissions

I want to start using Docker for Rails development, so I am trying to put together a skeleton that I can use for all my applications.

However, I ran into an issue with Docker volumes and permissions.

I want to bind my app directory mount to the container so that any changes propagate to the container without having to rebuild it.

But if I define it as volumes in mine docker-compose.yml

, I can no longer chown

catalog. I need the directory and all its contents to be owned by the user app

in order for the Passenger to work properly.

I read that it is impossible to chown

volumes.

Do you know about detours?

+3


source to share


2 answers


I am using a hacky solution to solve this problem in my development environments. Use in development environments only !

The images I use for development environments contain a script that looks like this:

#!/bin/sh

# In usr/local/bin/change-dev-id
# Change the "dev" user UID and GID

# Retrieve new ids to apply
NEWUID=$1
NEWGID=$1
if [ $# -eq 2 ]
then
    NEWGID=$2
elif [ $# -ne 1 ]
then
    echo "Usage: change-dev-id NEWUID [NEWGID]"
    echo "If NEWGID is not provided, its value will be the same as NEWUID"
    exit 1
fi

# Retrieve old ids
OLDUID=`id -u dev`
OLDGID=`id -g dev`

# Change the user ids
usermod -u ${NEWUID} dev
groupmod -g ${NEWGID} dev

# Change the files ownership
find / -not \( -path /proc -prune \) -user ${OLDUID} -exec chown -h ${NEWUID} {} \;
find / -not \( -path /proc -prune \) -group ${OLDGID} -exec chgrp -h ${NEWGID} {} \;

echo "UID and GID changed from ${OLDUID}:${OLDGID} to ${NEWUID}:${NEWGID} for \"dev\""
exit 0

      

In the Dockerfile of my base image, I add it and make it executable:

# Add a script to modify the dev user UID / GID
COPY change-dev-id /usr/local/bin/change-dev-id
RUN chmod +x /usr/local/bin/change-dev-id

      



Then, instead of changing the owner of the mounted folder, I change the container's user ID to match my user ID on the host machine:

# In the Dockerfile of the project development environment, change the ID of
# the user that must own the files in the volume so that it match the ID of
# the user on the host
RUN change-dev-id 1234

      

This is very hacky, but it can be very handy. I can store the project files on my machine while the user in the container also has the correct permissions.

You can update the script code to use the username you want (mine is always "dev") or change it to pass the username as an argument.

+3


source


You chown

can run instead chown

. How:

CMD chown -R app:app /home/app/webapp && /sbin/my_init

      



Operators

RUN

are performed only during the embedded time of your image. But there are no volumes installed yet.

CMD

instead it is executed at runtime of the container when the volumes are already mounted. This way it will have the effect you want.

+4


source







All Articles