我有以下代码
RUN apt-get update
RUN apt-get install -y wget #install wget lib
RUN mkdir -p example && cd example #create folder and cd to folder
RUN WGET -r https://host/file.tar && tar -xvf *.tar # download tar file to example folder and untar it in same folder
RUN rm -r example/*.tar # remove the tar file
RUN MV example/foo example/bar # rename untar directory from foo to bar
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
/bin/sh: 1: WGET: not found
tar: example/*.tar: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
Run Code Online (Sandbox Code Playgroud)
我是码头工人的新手。
Dockerfile 中的每个后续RUN
命令都将位于该目录的上下文中/
。因此,您的.tar
文件不在该example/
目录中,它实际上位于该/
目录中,因为您的“cd 到文件夹”对于后续RUN
命令没有任何意义。不要执行,而是在运行之前cd example
执行,例如:WORKDIR example
wget
RUN apt-get update
RUN apt-get install -y wget #install wget lib
RUN mkdir -p example # create folder and cd to folder
WORKDIR example/ # change the working directory for subsequent commands
RUN wget -r https://host/file.tar && tar -xvf *.tar # download tar file to example folder and untar it in same folder
RUN rm -r example/*.tar # remove the tar file
RUN mv example/foo example/bar # rename untar directory from foo to bar
Run Code Online (Sandbox Code Playgroud)
或者,cd example && ... some command
在您想要在example
目录中执行的任何命令之前添加。
正如 Ntokozo 所说,每个 RUN 命令都是构建过程中的一个单独的“会话”。因此,Docker 的真正设计目的是将尽可能多的命令打包到单个 RUN 中,从而允许更小的整体映像大小和更少的层。所以命令可以这样写:
RUN apt-get update && \
apt-get install -y wget && \
mkdir -p example && \
cd example/ && \
wget -r https://host/file.tar && \
tar -xvf *.tar && \
rm -r example/*.tar && \
mv example/foo example/bar
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16894 次 |
最近记录: |