使用特定的UID在主机系统和Docker容器之间共享文件 [英] Share files between host system and docker container using specific UID

查看:152
本文介绍了使用特定的UID在主机系统和Docker容器之间共享文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用卷共享在Docker guest虚拟机中共享文件。为了获得相同的UID,从而与这些文件具有互操作性,我想在Docker guest虚拟机中创建一个与我自己的用户具有相同UID的用户。

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.

为了验证这个想法,我编写了以下简单的Dockerfile:

In order to test out the idea, I wrote the following simplistic Dockerfile:

FROM phusion/baseimage

RUN touch /root/uid-$UID

使用 docker build测试它-t = docktest。然后 docker运行ockertest ls -al / root 显示该文件简称为 uid-

Testing it with docker build -t=docktest . and then docker run docktest ls -al /root reveals that the file is simply named uid-.

在来宾构建过程中是否可以与Docker共享主机环境变量?

Is there a means to share host environment variables with Docker during the guest build process?

推荐答案

不共享环境,可以使用-e,--env选项在容器中设置环境变量。

The environment is not shared, you could use -e, --env options to set env variables in container.

当我想拥有映射卷的相同所有者时,通常使用这种方法:我检查uid&容器中目录的gid,然后创建一个相应的用户。这是我的脚本(setuser.sh),它为目录创建用户:

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

这是包装脚本,以用户身份运行命令,其中具有权限的目录指定为$ USER_DIR或在/ etc / user_dir

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

PS Alleo建议使用不同的方法:将用户和组文件映射到容器中,并指定uid和gid。因此,您的容器不依赖于内置的用户/组,您可以在没有其他脚本的情况下使用它。

P.S. Alleo suggested different approach: to map users and groups files into container and to specify uid and gid. So your container does not depend on built-in users/groups you could use it without additional scripts.

这篇关于使用特定的UID在主机系统和Docker容器之间共享文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆