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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:02:23  来源:igfitidea点击:

Commands to execute background process in Docker CMD

bashshelldockerdockerfile

提问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.shscript and keep that start.shscript itself in foregroundusing the nohupcommand and the ampersand (&). I did not try it with mongodbut something like the following in your start.shscript could work:

除了对您的问题的评论已经指出了有关 Docker 最佳实践的一些事情之外,您无论如何都可以从脚本中启动后台进程,start.sh并使用命令和与符号 ( )将该start.sh脚本本身保持在前台。我没有尝试过,但您脚本中的以下内容可以工作:nohup&mongodstart.sh

#!/bin/sh
...
nohup sh -c mongod --dbpath /test &
...