我有一个来自镜像的Dockerfile。 我想修改它并添加一些文件操作,这些操作将相对于与FROM图像所给定的WORKDIR不同。因此,我想在修改WORKDIR之前将WORKDIR保存到一个变量,并在命令结束后从该变量恢复WORKDIR。该怎么做?
想要的结果:
Dockerfile伪代码:
FROM centos:7
some original commands
some original commands
...
var=WORKDIR #save WORKDIR <== how to do that?
WORDIR /my/path/
my commands
my commands
...
WORKDIR = var # restore path that was before <== how to do that?
答案 0 :(得分:0)
如果您尝试从基本映像中保留WORKDIR,则不可能
WORKDIR仅在您在同一dockerfile中进行设置时才具有值。
如果您尝试为不同的命令使用不同的工作目录:
Dockerfile伪代码:
FROM centos:7
some original commands
some original commands
...
ARG PATH1=/path1
ARG PATH2=/path1
my commands
my commands
WORKDIR PATH1
my commands
my commands
WORKDIR PATH2
my commands
my commands
WORKDIR PATH1
my commands
my commands
...
WORKDIR PATH1 # restore path that was before <== how to do that?
答案 1 :(得分:0)
添加到WSMathias9的答案中:
除非WORKDIR
被您使用的基本图像更改,否则默认情况下始终为/
,您可以在完成以下操作后将其设置为/
这是centos
图片的默认图片
但是,如果您使用WORKDIR
代替cd
,只需在cd
中使用RUN
而不是例如:
FROM centos:7
WORKDIR /folder
RUN mycommand
要做:
FROM centos:7
RUN cd /folder && mycommand
答案 2 :(得分:0)
您可以创建环境变量。
ENV WORKDIR=/my/path/
WORDIR $WORKDIR