Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 330
Sourabh Avatar asked Feb 13 '26 14:02

Sourabh


1 Answers

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
like image 114
Adiii Avatar answered Feb 15 '26 12:02

Adiii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!