Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a list of arguments to docker at build / run time

I am writing a dockerfile where i want to download a changing set of git repos: repo1, repo2, repo3. How should I supply these urls/strings to the dockerfile at image build or container run times?

I know one can pass an argument using the ARG instruction and using

docker build --build-arg <var-name>=<value>

But what happens when I have a list of args of arbitrary length?

Thanks a lot.

like image 361
Nimrodshn Avatar asked Jan 05 '17 16:01

Nimrodshn


People also ask

Can I pass arguments in docker run?

You can pass environment variables with ENV declarations in a Dockerfile or with -e arguments to the docker run command. The following examples show ways to use environment variables.

How do I pass multiple ARGs to Dockerfile?

If you want to pass multiple build arguments with docker build command you have to pass each argument with separate — build-arg. docker build -t <image-name>:<tag> --build-arg <key1>=<value1> --build-arg <key2>=<value2> .

How do I pass a build argument in Docker compose?

If you want to pass variables through the docker-compose process into any of the Dockerfiles present within docker-compose. yml , use the --build-arg parameter for each argument to flow into all of the Dockerfiles.


1 Answers

Build time

The best I can think of is to pass in a comma-separated list:

 docker build --build-arg repos=repo1,repo2

Docker won't parse this into a list for you, but the scripts run during the build could split the string into a list.

Run time

If you define your command using ENTRYPOINT then any trailing parameters to docker run get appended to the entry point command.

So if your Dockerfile contains:

   ENTRYPOINT echo hello

Then:

   docker run myimage glorious world 

... will run the command

   echo hello glorious world
like image 124
slim Avatar answered Sep 28 '22 17:09

slim