Docker卷的权限问题

我想开始使用Docker进行Rails开发,所以我正在尝试将所有应用程序都可以使用的框架放在一起。

不过,我遇到了Docker卷和权限的问题。

我想将应用程序的目录绑定到容器中,以便将所有更改传播到容器,而无需重新构build它。

但是,如果我在我docker-compose.yml其定义为卷,则无法再chown目录。 我需要该目录及其所有内容为app用户所有,以便乘客正常工作。

我读过,不可能chown音量。

你知道任何解决方法吗?

我使用hacky解决scheme来pipe理我的开发环境中的这个问题。 仅在开发环境中使用

我用于开发环境的图像包含一个如下所示的脚本:

 #!/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 

在我的基础镜像的Dockerfile中,我添加它并使其可执行:

 # 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 

然后,而不是更改装入的文件夹的所有者,我更改容器的用户的ID以匹配我的用户在主机上的ID:

 # In the Dockerfile of the project's 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 

这很不方便,但可以很方便。 我可以在我的机器上拥有项目的文件,而容器中的用户也具有正确的权限。

您可以更新脚本的代码,以使用您想要的用户名(我始终是“dev”)或修改它以传递用户名作为参数。

你可以尝试通过CMD来运行chown 。 喜欢:

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

RUN语句只在图像的build立时间内执行。 但是,你还没有装卷。

当卷已经安装时, CMD会在容器运行时执行。 所以这会有你想要的效果。