Search code examples
dockerdocker-composedockerfiledocker-machine

Passing Different Arguments When Running Docker Image Multiple Times


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?


Solution

  • 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>