我想开始使用Docker进行我的Rails开发,所以我正在尝试组合一个框架,我可以在所有应用程序中使用。
但是,我遇到了Docker卷和权限的问题。
我想将应用程序的目录绑定到容器中,这样任何更改都会传播到容器中,而无需重新构建它。
但是,如果我在docker-compose.yml
中将其定义为卷,我就无法再chown
该目录。我需要该目录及其所有内容都归app
用户所有,以便Passenger能够正确工作。
我读到说无法chown
卷。
你知道有什么解决方法吗?
我想开始使用Docker进行我的Rails开发,所以我正在尝试组合一个框架,我可以在所有应用程序中使用。
但是,我遇到了Docker卷和权限的问题。
我想将应用程序的目录绑定到容器中,这样任何更改都会传播到容器中,而无需重新构建它。
但是,如果我在docker-compose.yml
中将其定义为卷,我就无法再chown
该目录。我需要该目录及其所有内容都归app
用户所有,以便Passenger能够正确工作。
我读到说无法chown
卷。
你知道有什么解决方法吗?
您可以尝试通过CMD
运行chown
命令。例如:
CMD chown -R app:app /home/app/webapp && /sbin/my_init
RUN
语句仅在构建镜像时执行,但此时您尚未挂载卷。
CMD
语句在容器运行时执行,此时卷已经被挂载。因此,这将产生您想要的效果。
我使用了一个“hacky solution”来管理我的开发环境中的这个问题。只能在开发环境中使用!
我用于开发环境的映像包含一个脚本,看起来像这样:
#!/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
# 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