bash 在 Docker CMD 中执行后台进程的命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31559132/
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
Commands to execute background process in Docker CMD
提问by Jeevitha G
I am creating a docker image using a Dockerfile. I would like to execute some scripts while starting the docker container. Currently I have a shell script to execute all the necessary processes
我正在使用 Dockerfile 创建一个 docker 镜像。我想在启动 docker 容器时执行一些脚本。目前我有一个 shell 脚本来执行所有必要的过程
CMD ["sh","start.sh"]
CMD ["sh","start.sh"]
I would like to execute a shell command with a process running in background example
我想使用在后台示例中运行的进程执行 shell 命令
CMD ["sh", "-c", "mongod --dbpath /test &"]
CMD ["sh", "-c", "mongod --dbpath /test &"]
采纳答案by 0x7d7b
Besides the comments on your question that already pointed out a few things about Docker best practices you could anyway start a backgroundprocess from within your start.sh
script and keep that start.sh
script itself in foregroundusing the nohup
command and the ampersand (&
). I did not try it with mongod
but something like the following in your start.sh
script could work:
除了对您的问题的评论已经指出了有关 Docker 最佳实践的一些事情之外,您无论如何都可以从脚本中启动后台进程,start.sh
并使用命令和与符号 ( )将该start.sh
脚本本身保持在前台。我没有尝试过,但您脚本中的以下内容可以工作:nohup
&
mongod
start.sh
#!/bin/sh
...
nohup sh -c mongod --dbpath /test &
...