我想在每次使用docker exec
或kubectl exec
启动shell时运行一个shell脚本。我正在寻找类似于.bashrc
/.bash_profile
/.profile
的脚本,但由于这个特定的容器是基于alpine linux的,因此bash不可用。
如何在容器中执行shell脚本?
我想实现这样的目标:
> docker exec -it my_alpine_based_container sh
Hi! Welcome to the container. This message comes from a shell script.
/usr/src/app $
类似地,对于kubectl
:
> kubectl exec -it my_pod_running_the_container -- sh
Hi! Welcome to the container. This message comes from a shell script.
/usr/src/app $
1条答案
按热度按时间emeijp431#
Alpine linux使用ash来支持
/bin/sh
,但只有在作为登录shell运行时,Ash才支持.profile
。下面是一个打印欢迎消息的Dockerfile示例:
以下是它的实际应用:
现在,这不符合您的要求之一,因为如果您运行:
该消息将不会打印,因为它缺少
-l
选项。但由于此特定容器基于AlpineLinux,因此Bash不可用。
Bash是一个相当轻量级的图像添加。如果你像这样将Bash添加到你的图像中,那么它只会给图像增加4. 6 MB。
我会说,如果它使您的服务更容易调试,这是值得的。