gpt4 book ai didi

docker - 使用 Dockerfile 构建 docker 镜像时无法更改目录

转载 作者:行者123 更新时间:2023-12-02 09:35:32 33 4
gpt4 key购买 nike

我正在尝试从 Dockerfile 创建图像。

# cat Dockerfile 
FROM ubuntu:16.04
COPY $pwd/intel_virtual_gateway_console64_1_9_0.tar /root/
COPY $pwd/login.exp /root/
RUN cd /root
RUN echo $PWD
RUN tar -xvf intel_virtual_gateway_console64_1_9_0.tar
RUN cd virtualgatewayconsole_package
RUN apt-get update && apt-get install expect \
expect-dev

构建镜像时,目录不会更改为/root/。我认为问题可能是 tar 文件丢失,以便确认将当前工作目录更改为/root 目录后打印当前工作目录。但我已经在容器中验证了包已成功复制到/root 目录。我什至还通过尝试其他目录来验证,即使对于那些目录也没有改变。由于此问题,后续步骤失败:

# docker build -t release:1.0 .
Sending build context to Docker daemon 633.2MB
Step 1/8 : FROM ubuntu:16.04
---> 6a2f32de169d
Step 2/8 : COPY $pwd/intel_virtual_gateway_console64_1_9_0.tar /root/
---> Using cache
---> 36e9ea407082
Step 3/8 : COPY $pwd/login.exp /root/
---> Using cache
---> 578f9f9481d9
Step 4/8 : RUN cd /root
---> Running in 07ccfc507888
---> ad60f9d31c7e
Removing intermediate container 07ccfc507888
Step 5/8 : RUN echo $PWD
---> Running in e0ec2df6a0dc
/
---> 979a42368814
Removing intermediate container e0ec2df6a0dc
Step 6/8 : RUN tar -xvf intel_virtual_gateway_console64_1_9_0.tar
---> Running in 0701db595e27
tar: intel_virtual_gateway_console64_1_9_0.tar: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
The command '/bin/sh -c tar -xvf intel_virtual_gateway_console64_1_9_0.tar' returned a non-zero code: 2

但能够更改容器内的目录。

# docker run -it 979a42368814 /bin/bash
root@100b02ddc98a:/# pwd
/
root@100b02ddc98a:/# cd /root/
root@100b02ddc98a:~# pwd
/root

请帮忙找出导致问题的原因。

最佳答案

使用WORKDIR

https://docs.docker.com/engine/reference/builder/#workdir

或者在一次运行中完成所有操作

当您进行另一次 RUN 时,您的 cd 被“遗忘”

顺便说一下,按照 Dockerfile 最佳实践中的指示对 RUN 进行分组

https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/

关于docker - 使用 Dockerfile 构建 docker 镜像时无法更改目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43518761/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com