Sourabh
Sourabh

Reputation: 564

How can I pass array into a dockerfile and loop through it?

I am calling the docker build command from a shell script and I wanna pass an array in the build args .. First question can I really do that ? If yes then how do i iterate inside the docker file. A Small example will really help.

Upvotes: 14

Views: 10400

Answers (2)

Wasim Ansari
Wasim Ansari

Reputation: 290

You can use the keyword ARG RELEASE=master inside the Dockerfile and and then pass the argument at the time of docker build as --build-arg RELEASE=v0.1.0.

The below link give you completed example of the same.

https://blog.dockbit.com/making-use-of-docker-build-arguments-68792d751f3

Upvotes: -3

Adiii
Adiii

Reputation: 59976

You can pass a space-separated string to builds then convert string to an array or just loop over the string.

Dockerfile

FROM alpine
ARG items
RUN for item in $items; do \
    echo "$item"; \
    done;

pass value during build time

docker build --build-arg items="item1 item2 item3 item4" -t my_image .

output

Step 3/3 : RUN for item in $items; do     echo "$item";     done;
 ---> Running in bee1fd1dd3c6
item1
item2
item3

Upvotes: 13

Related Questions