Dockerfile:运行 带有特殊参数的命令
Dockerfile: RUN command with special parameters
我想 运行 bash 命令在 Dockerfile 中使用 $_ 等特殊参数。
我尝试了以下示例,但它似乎不起作用。如果我直接在 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
我想 运行 bash 命令在 Dockerfile 中使用 $_ 等特殊参数。 我尝试了以下示例,但它似乎不起作用。如果我直接在 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