2017-06-21 2 views
1

SO post about replicating UID/GID in container from hostと同様ですが、レプリケートUIDとGIDを持つユーザーでイメージをどのように構築しますか?できれば、ドッカーファイルでどうやってやるの?DockerfileはホストユーザーのUIDとGIDをイメージに複製します

私はbashスクリプトでそれを行うことができます。

#!/bin/bash 

# current uid and gid 
curr_uid=`id -u` 
curr_gid=`id -g` 

# create bb.dockerfile: 
cat <<EOF1> bb.dockerfile 
FROM ubuntu:xenial-20170214 
ARG UNAME=testuser 
EOF1 

echo ARG UID=${curr_uid} >> bb.dockerfile 
echo ARG GID=${curr_gid} >> bb.dockerfile 

cat <<EOF2>> bb.dockerfile 
RUN groupadd -g \$GID \$UNAME 
RUN useradd -m -u \$UID -g \$GID -s /bin/bash \$UNAME 
USER \$UNAME 
CMD /bin/bash 
EOF2 

docker build -f bb.dockerfile -t testimg . 

このbashは、次のようにドッキングウィンドウのファイルを生成し、その上に構築されます。私が求めている何

FROM ubuntu:xenial-20170214 
ARG UNAME=testuser 
ARG UID=1982 
ARG GID=1982 
RUN groupadd -g $GID $UNAME 
RUN useradd -m -u $UID -g $GID -s /bin/bash $UNAME 
USER $UNAME 
CMD /bin/bash 

は、dockerfileからハードコードされたホストUID 1982およびGID 1982を削除することです。

答えて

3

ビルドargとして渡すことができます。あなたのDockerfileは静的であることができます。

FROM ubuntu:xenial-20170214 
ARG UNAME=testuser 
ARG UID=1000 
ARG GID=1000 
RUN groupadd -g $GID $UNAME 
RUN useradd -m -u $UID -g $GID -s /bin/bash $UNAME 
USER $UNAME 
CMD /bin/bash 

次に、あなたがあなたのビルドコマンドのオプションを渡したい:それが動作

docker build --build-arg UID=$(id -u) --build-arg GID=$(id -g) \ 
    -f bb.dockerfile -t testimg . 
+0

を!どうもありがとう! – minghua

関連する問題