Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate in RUN command in Dockerfile

I have a line that looks something like:

RUN for i in `x y z`; do echo "$i"; done

...with the intention of printing each of the three items

But it raises /bin/sh: 1: x: not found

Any idea what I'm doing wrong?

like image 844
Maximilian Avatar asked Sep 14 '16 17:09

Maximilian


People also ask

What does run command do in Dockerfile?

The docker run command first creates a writeable container layer over the specified image, and then starts it using the specified command. That is, docker run is equivalent to the API /containers/create then /containers/(id)/start .

Can I run 2 commands in Dockerfile?

Apart from the Dockerfile CMD directive and the docker run command, we can execute multiple commands with the docker-compose tool.

What is run and CMD in Dockerfile?

RUN is an image build step, the state of the container after a RUN command will be committed to the container image. A Dockerfile can have many RUN steps that layer on top of one another to build the image. CMD is the command the container executes by default when you launch the built image.


1 Answers

It looks like you're using backticks. What's in backticks gets executed and the text in the backticks gets replaced by what's returned by the results.

Try using single quotes or double quotes instead of backticks.

Try getting rid of the backticks like so:

RUN for i in x y z; do echo "$i"; done
like image 140
arjabbar Avatar answered Oct 06 '22 01:10

arjabbar