简体   繁体   English

如果镜像有入口脚本,Docker 无法启动交互式 shell

[英]Docker unable to start an interactive shell if the image has an entry script

My custom-made image ends with我的定制图像以

ENTRYPOINT [ "/bin/bash", "-c", "/home/tool/entry_script.sh" ]

This is absolutely needed because at runtime, the first thing the user must do is to update an already cloned github project, and users will often forget to do it.这是绝对需要的,因为在运行时,用户必须做的第一件事是更新已经克隆的 github 项目,而用户经常会忘记这样做。

But then, when i try to launch using但是,当我尝试使用

docker run -it --rm my_image /bin/bash

i can see that the ENTRYPOINT script is being executed, but then the container exit.我可以看到正在执行ENTRYPOINT脚本,但随后容器退出。

I expect to have /bin/bash being executed and the shell to remain in interactive mode, due to -it flags.由于-it标志,我希望执行/bin/bash并且 shell 保持交互模式。

What am I doing wrong?我究竟做错了什么?

UPDATE: I add my entry script更新:我添加了我的入口脚本

#!/bin/bash

echo "UPDATING GIT REPO";

cd /home/tool/cloned_github_tools_root
git pull
git submodule init
git submodule update

echo "Entrypoint ended";

Actually I've not kind of errors at runtime实际上我在运行时没有任何错误

When you set and entry point in a docker container.当您在 docker 容器中设置和入口点时。 It is the only thing it will run.这是它唯一会运行的东西。 It's the one and only process that matters (PID 1).这是唯一重要的过程(PID 1)。 Once your entry_point.sh script finishes running and returns and exit code, docker thinks the container has done what it needed to do and exits, since the only process inside it exits.一旦您的entry_point.sh脚本完成运行并返回并退出代码,docker 认为容器已经完成了它需要做的事情并退出,因为它里面的唯一进程退出了。

If you want to launch a shell inside the container, you can modify your entry point script like so:如果你想在容器内启动一个 shell,你可以像这样修改你的入口点脚本:

#!/bin/bash

echo "UPDATING GIT REPO";

cd /home/tool/cloned_github_tools_root
git pull
git submodule init
git submodule update

echo "Entrypoint ended";

/bin/bash "$@"

This starts a shell after the repo update has been done.这将在 repo 更新完成后启动一个 shell。 The container will now exit when the user quits the shell.当用户退出 shell 时,容器现在将退出。

The -i and -t flags will make sure the session gives you an stdin/stdout and will allocate a psuedo-tty for you, but they will not automatically run bash for you. -i-t标志将确保会话为您提供标准输入/标准输出并为您分配一个伪 tty,但它们不会自动为您运行 bash。 Some containers don't even have bash in them.有些容器中甚至没有 bash。

I think the original question and answer are pretty good (thank you!).我认为原始问题和答案非常好(谢谢!)。 However I had the same exact problem but the provided solution did not work for me.但是,我遇到了完全相同的问题,但提供的解决方案对我不起作用。 I ended up wasting a lot of time figuring out what I was doing wrong.我最终浪费了很多时间来弄清楚我做错了什么。 Hence I came up with a solution that should work all the time, if this could save time for others.因此,我想出了一个应该一直有效的解决方案,如果这可以为​​其他人节省时间。 In my docker entry point I'm sourcing a shell script file from Intel compiler and the received parameters $@ are somewhat changed by the 'source' command.在我的 docker 入口点中,我从 Intel 编译器获取了一个 shell 脚本文件,并且接收到的参数 $@ 被“source”命令稍微改变了。 Then when ending the script with /bin/bash "$@" the original parameters are gone.然后当以 /bin/bash "$@" 结束脚本时,原始参数消失了。 Here is my updated version that would be safer for all use cases:这是我的更新版本,它对所有用例都更安全:

#!/bin/bash

# Save original parameters
allparams=("$@")

echo "UPDATING GIT REPO";

cd /home/tool/cloned_github_tools_root
git pull
git submodule init
git submodule update

echo "Entrypoint ended";

# Forward initial parameters
/bin/bash "${allparams[@]}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM