如何在每个“exec"处执行 Docker 映像的入口点命令? [英] How to execute the Entrypoint of a Docker images at each "exec" command?
问题描述
在尝试使用 Dockerspec 测试 Dockerfiles 后,我终于遇到了一个问题我可以'无法正确解决.
After trying to test Dockerfiles with Dockerspec, I finally had an issue I can't resolve properly.
我认为问题出在 Docker 本身;如果我了解它的过程,Entrypoint 仅在运行时执行,但如果容器保持启动状态并且我在其中启动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.
我认为这是想要的行为.
I think it's the wanted behavior.
但如果入口点是我所有命令之前的gosu"脚本,这是个问题……
But if the Entrypoint is a "gosu" script which precede all my commands, it's a problem...
示例
"myImage" 有这个入口点:gosu 1000:1000 "$@"
"myImage" has this Entrypoint :
gosu 1000:1000 "$@"
如果我启动:docker run -it myImage id -u
输出为1000".
如果我启动一个容器:docker run -it myImage bash
在这个容器中,id -u
输出1000".
In this container, id -u
outputs "1000".
但是如果我在这个容器中启动一个新命令,它会启动一个新的shell,并且不执行入口点,所以:docker exec CONTAINER_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"启动的.
每次入口点都有执行的方法吗?还是重新用shell打开?
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
docker exec container-name 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).
将其放入本地文件夹中的文件中...例如 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
运行它
give it execute permissions with chmod +x gs
and then run it with ./gs
from the local folder
这篇关于如何在每个“exec"处执行 Docker 映像的入口点命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!