使用自定义 shell 脚本作为 docker 入口点
Using custom shell script as docker entrypoint
我正在尝试使用 docker-compose 来启动一个容器。作为 ENTRYPOINT,该容器有一个我编写的简单 bash 脚本。现在,当我尝试通过以下任何一种方式调出容器时:
docker-compose up
docker-compose up foo
它没有完成。即,尝试将 (docker exec -i -t /bin/bash
) 附加到 运行ning 容器,失败并显示:
Error response from daemon: Container xyz is restarting, wait until the container is running.
我尝试在后台放置命令。那没有用。
我的-script.sh
cmd1 &
cmd2 &&
...
cmdn &
我还尝试了 i) 有无 entrypoint: /bin/sh /usr/local/bin/my-script.sh
和 ii) 有无 tty: true
选项。没有骰子。
docker-compose.yml
version: '2'
services:
foo:
build:
context: .
dockerfile: Dockerfile.foo
...
tty: true
entrypoint: /bin/sh /usr/local/bin/my-script.sh
还尝试了手动 docker 构建/运行 循环。并且(没有在 ENTRYPOINT 中启动 /bin/sh
) 运行 就退出了。
$ docker build ... .
$ docker run -it ...
... shell echos commands here, then exits
$
我敢肯定这很简单。这里的解决方案是什么?
首先你需要放一些无限的东西来保持 运行 你的容器在后台,就像你可以 tail -f application.log
或类似的东西这样即使你退出你的容器 bash 它使 运行 在后台
您不需要执行 cmd1 & cmd2 &&...cmdn &
只需放置一个这样的命令 touch 1.txt && tail -f 1.txt
作为 my-script.sh
中的最后一步。它会保留 运行 你的容器。
您还需要更改的一件事是 docker run -it -d
-d
将启动带有背景的容器 mode.If 您想要进入容器内部而不是 docker exec -it container_name/id bash
调试问题和 exit.It仍会保留您的容器 运行 直到您停止使用 docker stop container_id/name
希望这有帮助。
谢谢!
你的entrypoint
在你的docker-compose.yml
只需要
entrypoint: /usr/local/bin/my-script.sh
只需在脚本顶部添加 #! /bin/sh
即可指定要使用的 shell。
您还需要将 exec "$@"
添加到入口点脚本的底部,否则它将立即退出,从而终止容器。
我正在尝试使用 docker-compose 来启动一个容器。作为 ENTRYPOINT,该容器有一个我编写的简单 bash 脚本。现在,当我尝试通过以下任何一种方式调出容器时:
docker-compose up
docker-compose up foo
它没有完成。即,尝试将 (docker exec -i -t /bin/bash
) 附加到 运行ning 容器,失败并显示:
Error response from daemon: Container xyz is restarting, wait until the container is running.
我尝试在后台放置命令。那没有用。
我的-script.sh
cmd1 &
cmd2 &&
...
cmdn &
我还尝试了 i) 有无 entrypoint: /bin/sh /usr/local/bin/my-script.sh
和 ii) 有无 tty: true
选项。没有骰子。
docker-compose.yml
version: '2'
services:
foo:
build:
context: .
dockerfile: Dockerfile.foo
...
tty: true
entrypoint: /bin/sh /usr/local/bin/my-script.sh
还尝试了手动 docker 构建/运行 循环。并且(没有在 ENTRYPOINT 中启动 /bin/sh
) 运行 就退出了。
$ docker build ... .
$ docker run -it ...
... shell echos commands here, then exits
$
我敢肯定这很简单。这里的解决方案是什么?
首先你需要放一些无限的东西来保持 运行 你的容器在后台,就像你可以 tail -f application.log
或类似的东西这样即使你退出你的容器 bash 它使 运行 在后台
您不需要执行 cmd1 & cmd2 &&...cmdn &
只需放置一个这样的命令 touch 1.txt && tail -f 1.txt
作为 my-script.sh
中的最后一步。它会保留 运行 你的容器。
您还需要更改的一件事是 docker run -it -d
-d
将启动带有背景的容器 mode.If 您想要进入容器内部而不是 docker exec -it container_name/id bash
调试问题和 exit.It仍会保留您的容器 运行 直到您停止使用 docker stop container_id/name
希望这有帮助。
谢谢!
你的entrypoint
在你的docker-compose.yml
只需要
entrypoint: /usr/local/bin/my-script.sh
只需在脚本顶部添加 #! /bin/sh
即可指定要使用的 shell。
您还需要将 exec "$@"
添加到入口点脚本的底部,否则它将立即退出,从而终止容器。