Dockerfile:具有特殊参数的RUN命令

我想运行特殊的参数,如$ _在Dockerfile中的bash命令。 我尝试了下面的例子,但它似乎不工作。 如果我直接在bash中执行相同的命令,它就像一个魅力。 有没有人有一个想法,我在做什么错了?

$ RUN mkdir /foo && cd $_ && pwd 

它取决于基础容器中的shell。 例如,Ubuntu使用短划线,将默认shell更改为bash有助于获得您期望的结果:

 FROM ubuntu RUN rm /bin/sh && ln -s /bin/bash /bin/sh RUN mkdir /foo && cd $_ && pwd 

结果:

 docker build --rm . Sending build context to Docker daemon 2.048 kB Sending build context to Docker daemon Step 0 : FROM ubuntu ---> d0955f21bf24 Step 1 : RUN rm /bin/sh && ln -s /bin/bash /bin/sh ---> Using cache ---> c90ef9fd9710 Step 2 : RUN mkdir /foo && cd $_ && pwd ---> Running in 8e1d943a6b60 /foo ---> 1871918ee02c Removing intermediate container 8e1d943a6b60 Successfully built 1871918ee02c