Dockerfile - 用于切换主目录的 Docker 指令

标签 docker dockerfile

对于下面的 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

最佳答案

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

工作目录

WORKDIR 指令的 Dockerfile 引用

For clarity and reliability, you should always use absolute paths for your WORKDIR. Also, you should use WORKDIR instead of proliferating instructions like RUN cd … && do-something, which are hard to read, troubleshoot, and maintain.

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

enter image description here

使用 $HOME 会导致错误。

When you use the USER directive, it affects the userid used to start new commands inside the container.Your best bet is to either set ENV HOME /home/aptly in your Dockerfile, which will work dockerfile-home-is-not-working-with-add-copy-instructions

关于Dockerfile - 用于切换主目录的 Docker 指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57226929/

相关文章:

logging - 如何在 Bluemix 上启用对容器的监控和日志记录?

python - 无法从 Python 连接到 Docker Postgresql 实例

postgresql - 在 Dockerfile 中启动 postgres

git - 在构建/运行时将参数列表传递给 docker

Docker 容器在 'docker start' 后不断停止

docker - 在 Dockerfile 中访问 docker?

Angular:ng-serve 忽略证书

docker - 在 Docker 中运行 Gulp - 未找到

docker - Fluentd 容器未在 Openshift 上运行

docker - $GOPATH/go.mod 存在,但在构建 docker 容器时不应该存在,但如果我手动运行命令就可以工作