如何以非root用户身份使用Docker COPY? [英] How do I Docker COPY as non root?

查看:150
本文介绍了如何以非root用户身份使用Docker COPY?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构建Docker映像时,如何将文件 COPY 映像到映像中,以便生成的文件由root以外的用户拥有?

While building a Docker image, how do I COPY a file into the image so that the resulting file is owned by a user other than root?

推荐答案

对于v17.09.0-ce及更高版本

将可选标志-chown =< user>:< group> ADD COPY 命令。

例如

COPY --chown=<user>:<group> <hostPath> <containerPath>

--chown标志的文档现在位于主 Dockerfile参考页面

The documentation for the --chown flag is now live on the main Dockerfile Reference page.

问题 34263 已被合并,并且可以在版本v17.09.0-ce

Issue 34263 has been merged and is available in release v17.09.0-ce.

对于低于v17.09.0-ce的版本

Docker不会以root以外的用户身份支持 COPY 。您需要 chown / chmod 文件之后 COPY 命令。

Docker doesn't support COPY as a user other than root. You need to chown / chmod the file after the COPY command.

示例Dockerfile:

Example Dockerfile:

from centos:6
RUN groupadd -r myuser && adduser -r -g myuser myuser
USER myuser
#Install code, configure application, etc...
USER root
COPY run-my-app.sh /usr/local/bin/run-my-app.sh
RUN chown myuser:myuser /usr/local/bin/run-my-app.sh && \
    chmod 744 /usr/local/bin/run-my-app.sh
USER myuser
ENTRYPOINT ["/usr/local/bin/run-my-app.sh"]

v17.09.0-ce之前的 COPY的Dockerfile参考命令说:

Previous to v17.09.0-ce, the Dockerfile Reference for the COPY command said:


所有新文件和目录的UID和GID为0。

All new files and directories are created with a UID and GID of 0.






历史记录
此功能已被多次跟踪GitHub问题: 6119 9943 13600 27303 28499 问题30110

问题34263 是实现了可选标志功能和问题467 更新了文档。

Issue 34263 is the issue that implemented the optional flag functionality and Issue 467 updated the documentation.

这篇关于如何以非root用户身份使用Docker COPY?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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