Philipp Waller
Philipp Waller

Reputation: 604

Dockerfile: RUN command with special parameters

I want to run bash commands with special parameters like $_ inside the Dockerfile. I tried the following example but it seems to be not working. If I execute the same command in the bash directly, it works like a charm. Does anyone have an idea what I'm doing wrong?

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

Upvotes: 1

Views: 1055

Answers (1)

ISanych
ISanych

Reputation: 22680

It depends on shell in base container. ubuntu is using dash for example, changing default shell to bash is helping to get result which you expect:

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

result:

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

Upvotes: 5

Related Questions