如何在每个“ exec”执行一个Docker镜像的入口点。命令? [英] How to execute the Entrypoint of a Docker images at each "exec" command?
问题描述
尝试使用Dockerspec测试Dockerfile之后,我终于遇到了一个问题我可以
After trying to test Dockerfiles with Dockerspec, I finally had an issue I can't resolve properly.
我认为问题出在Docker本身;如果我了解其过程,入口点仅在运行时执行,但是如果容器保持启动状态并且我在其中启动了 exec命令,则不会重新调用它。
The problem is, I think, from Docker itself ; If I understand its process, an Entrypoint is only executed at run, but if the container stay started and I launch an "exec" command in, it's not re-called.
我认为这是通缉犯。
但是如果Entrypoint是 gosu 脚本,这是一个问题...
But if the Entrypoint is a "gosu" script which precede all my commands, it's a problem...
示例
myImage具有此入口点:$ b $ b gosu 1000:1000 $ @
"myImage" has this Entrypoint :
gosu 1000:1000 "$@"
如果我启动: docker run -it myImage id -u
输出为 1000。
如果我启动一个容器: docker run -it myImage bash
If I start a container : docker run -it myImage bash
在此容器中, id -u
输出 1000。
In this container, id -u
outputs "1000".
但是如果我在此容器中启动新命令,它将启动一个新的外壳程序,并且不执行入口点,因此: docker exec CONTAIN ER_ID id -u
But if I start a new command in this container, it starts a new shell, and does not execute the Entrypoint, so : docker exec CONTAINER_ID id -u
输出 0,因为新的shell以 root启动。
是否有一种方法可以在每次进入入口时执行?
或重新使用外壳打开吗?
It there a way to execute each time the entrypoint ? Or re-use the shell open ?
还是一种更好的方法?
或者,也许我什么都不懂? ;)
Or, maybe I haven't understand anything ? ;)
谢谢!
编辑
阅读了此处提出的解决方案后,我了解到问题不是Docker的工作方式,而是Serverspec的工作方式;我的目标是直接将命令作为 docker run
参数进行测试,但是Serverspec启动一个容器并使用 docker exec
。
After reading solutions proposed here, I understand that the problem is not how Docker works but how Serverspec works with ; my goal is to directly test a command as a docker run
argument, but Serverspec start a container and test commands with docker exec
.
因此,最好的解决方案是找到如何获取由Serverspec执行的 docker run
的标准输出。
So, the best solution is to found how get the stdout of the docker run
executed by Serverspec.
但是,在我的个人用例中,最好的解决方案可能是不使用Gosu而是--user标志:)
But, in my personal use-case, the best solution is maybe to not use Gosu but --user flag :)
推荐答案
如果您的目标是在容器内使用特定用户运行 docker exec
,则可以使用-user
选项。
if your goal is to run the docker exec
with a specific user inside of the container, you can use the --user
option.
docker exec --user myuser container-name [。 ..您的命令在这里]
如果您想每次运行 gosu
,您可以使用 docker exec
If you want to run gosu
every time, you can specify that as the command with docker exec
docke exec容器名称gosu指定为命令1000:1000 [您的实际命令在这里]
以我的经验,这是将其轻松封装成某种东西的最佳方法可重复使用的是.sh脚本(或Windows中的.cmd文件)。
in my experience, the best way to encapsulate this into something easily re-usable is with a .sh script (or .cmd file in windows).
将此文件拖放到本地文件夹中的文件中……也许<$ c $例如c> gs 。
drop this into a file in your local folder... maybe gs
for example.
#! /bin/sh
docker exec container-name gosu 1000:1000 "$@"
使用 chmod + x gs
授予执行权限,然后使用 ./ gs $运行它c $ c>从本地文件夹
give it execute permissions with chmod +x gs
and then run it with ./gs
from the local folder
这篇关于如何在每个“ exec”执行一个Docker镜像的入口点。命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!