gvg*_*zio 6 dockerfile docker-copy
在Dockerfile中,将目录复制为非root用户(例如$ UID 1000)的常用方法如下:
COPY --chown=1000:1000 /path/to/host/dir/ /path/to/container/dir
Run Code Online (Sandbox Code Playgroud)
但是,我想改用变量。例如
ARG USER_ID=1000
ARG GROUP_ID=1000
COPY --chown=${USER_ID}:${GROUP_ID} /path/to/host/dir/ /path/to/container/dir
Run Code Online (Sandbox Code Playgroud)
但这是不可能的。有解决方法吗?
请注意,我知道一个可能的解决方法是将目录复制为root,然后在目录上运行chown(变量与可以很好地配合使用RUN)。但是,仅在单独的命令中使用chown时,图像的大小会增加。
You can create a user before running the --chown;
mkdir -p test && cd test
mkdir -p path/to/host/dir/
touch path/to/host/dir/myfile
Run Code Online (Sandbox Code Playgroud)
Create your Dockerfile:
FROM busybox
ARG USER_ID=1000
ARG GROUP_ID=1000
RUN addgroup -g ${GROUP_ID} mygroup \
&& adduser -D myuser -u ${USER_ID} -g myuser -G mygroup -s /bin/sh -h /
COPY --chown=myuser:mygroup /path/to/host/dir/ /path/to/container/dir
Run Code Online (Sandbox Code Playgroud)
Build the image
docker build -t example .
Run Code Online (Sandbox Code Playgroud)
Or build it with a custom UID/GID:
docker build -t example --build-arg USER_ID=1234 --build-arg GROUP_ID=2345 .
Run Code Online (Sandbox Code Playgroud)
And verify that the file was chown'ed
docker run --rm example ls -la /path/to/container/dir
total 8
drwxr-xr-x 2 myuser mygroup 4096 Dec 22 16:08 .
drwxr-xr-x 3 root root 4096 Dec 22 16:08 ..
-rw-r--r-- 1 myuser mygroup 0 Dec 22 15:51 myfile
Run Code Online (Sandbox Code Playgroud)
Verify that it has the correct uid/gid:
docker run --rm example ls -lan /path/to/container/dir
total 8
drwxr-xr-x 2 1234 2345 4096 Dec 22 16:08 .
drwxr-xr-x 3 0 0 4096 Dec 22 16:08 ..
-rw-r--r-- 1 1234 2345 0 Dec 22 15:51 myfile
Run Code Online (Sandbox Code Playgroud)
Note: there is an open feature-request for adding this functionality: issue #35018 "Allow COPY command's --chown to be dynamically populated via ENV or ARG"
| 归档时间: |
|
| 查看次数: |
880 次 |
| 最近记录: |