Passing Different Arguments When Running Docker Image Multiple Times

余生长醉 提交于 2019-12-10 09:57:14

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/40413639/passing-different-arguments-when-running-docker-image-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!