3

I need to give an argument while running Docker Image which will be a number from 0-3.

Dockerfile has the following:

WORKDIR "mydir/build"
CMD ./maker oneapp > /artifacts/oneapp_$1.log ; ./maker twoapp > /artifacts/twoapp_$1.log ; ./maker -j13 threeapp > /artifacts/threeapp_$1.log

I will be running the same Docker Image multiple times so I need logs to be saved in /artifacts appended with _0, _1, _2, _3, as appropriate.

I tried keeping this in Docker file but don't want to pass this full line as argument while running docker.

ENTRYPOINT ["/bin/bash"]

./maker oneapp > /artifacts/oneapp_$1.log ; ./maker twoapp > /artifacts/twoapp_$1.log ; ./maker -j13 threeapp > /artifacts/threeapp_$1.log

Is it possible to do this? What do I need to modify in Dockerfile to do what I want?

Community
  • 1
  • 1

1 Answers1

3

Simply inject your parameter as an ENV.

Declare an ENV in your Dockerfile.

ENV suffix 0
./maker oneapp > /artifacts/oneapp_${suffix}.log

The environment variables set using ENV will persist when a container is run from the resulting image.
You can view the values using docker inspect, and change them using docker run --env <key>=<value>.

That way, you can declare that ENV on docker run, and benefit from its value in the running container.

the operator can set any environment variable in the container by using one or more -e flags, even overriding those mentioned above, or already defined by the developer with a Dockerfile ENV:

In your case, for instance:

docker run -e suffix=2 <image_name>
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This is nice &elegant approach. – Rao Nov 04 '16 at 01:38
  • I have added ENV count 0 and CMD ./maker oneapp > /artifacts/oneapp_${suffix}.log in Dockerfile but seeing this error: root@onl-dev:/data/sept15/target-docker# docker run -it target -e count=0 docker: Error response from daemon: oci runtime error: exec: "-e": executable file not found in $PATH. –  Nov 04 '16 at 18:13
  • @Sweety yes: `docker run` needs an image name to run (create a container from it). Your command has no image name. – VonC Nov 04 '16 at 18:15
  • Changed the order and it worked, thanks a lot! docker run -it -e count=0 target –  Nov 04 '16 at 18:32