在多次运行Docker图像时传递不同的参数 [英] Passing Different Arguments When Running Docker Image Multiple Times
问题描述
Dockerfile具有以下功能:
WORKDIRmydir / build
CMD ./maker oneapp> /artifacts/oneapp_$1.log; ./maker twoapp> /artifacts/twoapp_$1.log; ./maker -j13 threeapp> /artifacts/threeapp_$1.log
我将多次运行相同的Docker Image,所以我需要日志根据需要保存在_0,_1,_2,_3附加的/工件中。
我尝试在Docker文件中保留此内容,但是不要在运行docker的时候将此全线作为参数传递。
ENTRYPOINT [/ bin / bash]
。 > /artifacts/oneapp_$1.log; ./maker twoapp>
/artifacts/twoapp_$1.log; ./maker -j13 threeapp>
/artifacts/threeapp_$1.log
有可能吗?在Dockerfile中需要修改什么来做我想要的?
只需将参数注入ENV。
ENV后缀0
./maker oneapp> /artifacts/oneapp_${suffix}.log
环境变量集使用
ENV
将在从生成的映像运行容器时持续。
您可以使用docker inspect
,并使用docker run --env< key> =< value>
更改它们。
这样,你可以声明上的nofollow noreferrer> ENV
,并从运行容器中的值获益。
操作员可以通过使用一个或多个-e标志来设置容器中的任何环境变量,甚至覆盖上面提到的那些,或者已经由开发人员使用Dockerfile定义了ENV:
在您的情况下,例如:
docker run -e后缀= 2< image_name>
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?
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 usingdocker inspect
, and change them usingdocker 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>
这篇关于在多次运行Docker图像时传递不同的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!