bash 如何在每个“exec”命令中执行 Docker 镜像的入口点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43723777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute the Entrypoint of a Docker images at each "exec" command?
提问by Doubidou
After trying to test Dockerfiles with Dockerspec, I finally had an issueI can't resolve properly.
在尝试使用 Dockerspec 测试 Dockerfiles 后,我终于遇到了一个无法正确解决的问题。
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.
我认为问题出在 Docker 本身;如果我了解它的过程,一个 Entrypoint 只会在 run 时执行,但是如果容器保持启动状态并且我在其中启动了一个“exec”命令,它就不会被重新调用。
I think it's the wanted behavior.
我认为这是想要的行为。
But if the Entrypoint is a "gosu" script which precede all my commands, it's a problem...
但是,如果入口点是“古薮”脚本,我之前所有的命令,这是个问题......
Example
例子
"myImage" has this Entrypoint :
gosu 1000:1000 "$@"
“myImage”有这个入口点:
gosu 1000:1000 "$@"
If I launch : docker run -it myImage id -u
如果我启动: docker run -it myImage id -u
The output is "1000".
输出为“1000”。
If I start a container : docker run -it myImage bash
如果我启动一个容器: docker run -it myImage bash
In this container, id -u
outputs "1000".
在这个容器中,id -u
输出“1000”。
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
但是如果我在这个容器中启动一个新命令,它会启动一个新的 shell,并且不执行 Entrypoint,所以: docker exec CONTAINER_ID id -u
Output "0", because the new shell is started as "root".
输出“0”,因为新 shell 以“root”身份启动。
It there a way to execute each time the entrypoint ? Or re-use the shell open ?
有没有办法在每次进入入口点时执行?还是重新用shell打开?
Or a better way to do that ?
或者有更好的方法来做到这一点?
Or, maybe I haven't understand anything ? ;)
或者,也许我什么都不明白?;)
Thanks !
谢谢 !
EDIT
编辑
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
.
在阅读了此处提出的解决方案后,我明白问题不在于 Docker 的工作方式,而在于 Serverspec 的工作方式;我的目标是直接将命令作为docker run
参数进行测试,但 Serverspec 启动一个容器并使用docker exec
.
So, the best solution is to found how get the stdout of the docker run
executed by Serverspec.
因此,最好的解决方案是找到如何获取docker run
Serverspec 执行的标准输出。
But, in my personal use-case, the best solution is maybe to not use Gosu but --user flag :)
但是,在我个人的用例中,最好的解决方案可能是不使用 Gosu 而是 --user 标志:)
采纳答案by Derick Bailey
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
选项。
docker exec --user myuser container-name [... your command here]
docker exec --user myuser container-name [... your command here]
If you want to run gosu
every time, you can specify that as the command with docker exec
如果你想gosu
每次都运行,你可以将它指定为命令docker exec
docke exec container-name gosu 1000:1000 [your actual command here]
docke exec container-name gosu 1000:1000 [your actual command here]
in my experience, the best way to encapsulate this into something easily re-usable is with a .sh script (or .cmd file in windows).
根据我的经验,将其封装为易于重用的最佳方法是使用 .sh 脚本(或 Windows 中的 .cmd 文件)。
drop this into a file in your local folder... maybe gs
for example.
将其放入本地文件夹中的文件中......也许gs
例如。
#! /bin/sh
docker exec container-name gosu 1000:1000 "$@"
give it execute permissions with chmod +x gs
and then run it with ./gs
from the local folder
给它执行权限,chmod +x gs
然后./gs
从本地文件夹运行它