使用Dockerfile的主机环境变量 [英] Using host environment variables with Dockerfile
问题描述
我正在尝试使用卷共享在Docker guest虚拟机中共享文件。为了获得相同的UID,因此与这些文件的互操作性,我想在Docker的客人中创建与我自己的用户相同的UID的用户。
为了测试这个想法,我写了以下简单的Dockerfile:
FROM phusion / baseimage
RUN touch / root / uid- $ UID
使用 docker build -t = docktest。
然后 docker run docktest ls -al / root
显示文件简单地命名为 uid-
。
在访客构建过程中有没有办法与Docker共享主机环境变量?
环境不共享,您可以使用-e,--env选项在容器中设置env变量。
我通常使用这种方法,当我想拥有相同的所有者的映射卷:我检查uid& gid容器中的目录,然后创建一个对应的用户。在这里我的脚本(setuser.sh)为目录创建一个用户:
#!/ bin / bash
setuser(){
if [-z$ 1];那么
echo用法:$ 0< path>
return
fi
CURRENT_UID =`id -u`
DEST_UID =`stat -c%u$ 1`
如果[$ CURRENT_UID = $ DEST_UID];然后
return
fi
DEST_GID =`stat -c%g$ 1`
如果[-e / home / $ DEST_UID];然后
返回
fi
groupadd -g $ DEST_GID $ DEST_GID
useradd -u $ DEST_UID -g $ DEST_GID $ DEST_UID
mkdir -p / home / $ DEST_UID
chown $ DEST_UID:$ DEST_GID / home / $ DEST_UID
}
setuser $ 1
这是作为用户运行命令的包装器脚本,其中具有权限的目录指定为$ USER_DIR或/ etc / user_dir
#!/ bin / bash
如果[-z$ USER_DIR];然后
如果[-e / etc / user_dir];那么
export USER_DIR =`head -n 1 / etc / user_dir`
fi
fi
如果[-n$ USER_DIR];那么
如果[! -d$ USER_DIR];然后
echo请在运行此脚本之前挂载$ USER_DIR
exit 1
fi
。 `dirname $ BASH_SOURCE` / setuser.sh $ USER_DIR
fi
如果[-n$ USER_DIR];然后
cd $ USER_DIR
fi
如果[-e / etc / user_script];那么
。 / etc / user_script
fi
如果[$ CURRENT_UID = $ DEST_UID];然后
$ @
else
su $ DEST_UID -p -c$ @
fi
I'm trying to share files within a Docker guest using the volume sharing. In order to get the same UID, and therefore interoperability with those files, I would like to create a user in the Docker guest with the same UID as my own user.
In order to test out the idea, I wrote the following simplistic Dockerfile:
FROM phusion/baseimage
RUN touch /root/uid-$UID
Testing it with docker build -t=docktest .
and then docker run docktest ls -al /root
reveals that the file is simply named uid-
.
Is there a means to share host environment variables with Docker during the guest build process?
The environment is not shared, you could use -e, --env options to set env variables in container.
I usually use this approach when I want to have the same owner of the mapped volume: I check uid & gid of directory in container and then create a corresponding user. Here my script (setuser.sh) which creates a user for a directory:
#!/bin/bash
setuser() {
if [ -z "$1" ]; then
echo "Usage: $0 <path>"
return
fi
CURRENT_UID=`id -u`
DEST_UID=`stat -c "%u" $1`
if [ $CURRENT_UID = $DEST_UID ]; then
return
fi
DEST_GID=`stat -c "%g" $1`
if [ -e /home/$DEST_UID ]; then
return
fi
groupadd -g $DEST_GID $DEST_GID
useradd -u $DEST_UID -g $DEST_GID $DEST_UID
mkdir -p /home/$DEST_UID
chown $DEST_UID:$DEST_GID /home/$DEST_UID
}
setuser $1
And this is the wrapper script which runs commands as the user, where the directory with permissions is specified either as $USER_DIR or in /etc/user_dir
#!/bin/bash
if [ -z "$USER_DIR" ]; then
if [ -e /etc/user_dir ]; then
export USER_DIR=`head -n 1 /etc/user_dir`
fi
fi
if [ -n "$USER_DIR" ]; then
if [ ! -d "$USER_DIR" ]; then
echo "Please mount $USER_DIR before running this script"
exit 1
fi
. `dirname $BASH_SOURCE`/setuser.sh $USER_DIR
fi
if [ -n "$USER_DIR" ]; then
cd $USER_DIR
fi
if [ -e /etc/user_script ]; then
. /etc/user_script
fi
if [ $CURRENT_UID = $DEST_UID ]; then
"$@"
else
su $DEST_UID -p -c "$@"
fi
这篇关于使用Dockerfile的主机环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!