【问题标题】:Dockerfile - Docker directive to switch home directoryDockerfile - 用于切换主目录的 Docker 指令
【发布时间】:2019-07-27 05:48:34
【问题描述】:

对于下面的 dockerfile:

FROM buildpack-deps:buster

RUN groupadd -r someteam --gid=1280 && useradd -r -g someteam --uid=1280 --create-home --shell /bin/bash someteam

# Update and allow for apt over HTTPS
RUN apt-get update && \
  apt-get install -y apt-utils
RUN apt-get install -y apt-transport-https
RUN apt update -y 
RUN apt install python3-pip -y

# switch user from 'root' to ‘someteam’ and also to the home directory that it owns 
USER someteam

RUN pwd 

USER只更改用户而不更改主目录

编辑:

Step 11/14 : WORKDIR $HOME
cannot normalize nothing

如何将主目录更改为/home/someteam

【问题讨论】:

  • “主目录”在 Docker 中通常不是一个概念。在/app 中安装您的应用程序很常见,即使那不是“正常”的 Linux 目录。

标签: docker dockerfile


【解决方案1】:

您可以使用 dockerfile 中的WORKDIR 更改用户目录,这将成为工作目录。因此,无论何时创建容器,工作目录都将是传递给 Dockerfile 中的WORKDIR 指令的目录。

工作目录

WORKDIR 指令的 Dockerfile 参考

为了清晰和可靠,您应该始终使用绝对路径 你的工作目录。此外,您应该使用 WORKDIR 而不是增殖 像 RUN cd … && do-something 这样难以阅读的指令, 故障排除和维护。

FROM buildpack-deps:buster

RUN groupadd -r someteam --gid=1280 && useradd -r -g someteam --uid=1280 --create-home --shell /bin/bash someteam

# Update and allow for apt over HTTPS
RUN apt-get update && \
  apt-get install -y apt-utils
RUN apt-get install -y apt-transport-https
RUN apt update -y 
RUN apt install python3-pip -y

# switch user from 'root' to ‘someteam’ and also to the home directory that it owns 
USER someteam
WORKDIR /home/someteam

使用 $HOME 会导致错误。

当您使用 USER 指令时,它会影响用于启动的用户 ID 容器内的新命令。最好的选择是设置 ENV HOME /home/aptly 在你的 Dockerfile 中,这将工作 dockerfile-home-is-not-working-with-add-copy-instructions

【讨论】:

  • 可以用$$HOME代替路径
  • 是的,这更有意义,更新答案。
  • 我添加了截图,请仔细检查拼写,它应该是大写或者你可以使用echo $HOME验证
  • 对不起,我没有仔细检查,这是不可能的,请检查答案中提供的链接。
  • 更改主目录的 docker 指令是什么?
猜你喜欢
  • 2015-05-23
  • 1970-01-01
  • 2018-05-26
  • 2020-04-21
  • 2018-08-29
  • 1970-01-01
  • 2017-09-06
  • 1970-01-01
  • 2015-05-12
相关资源
最近更新 更多