在 Docker CMD 中执行后台进程的命令 [英] Commands to execute background process in Docker CMD

查看:36
本文介绍了在 Docker CMD 中执行后台进程的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Dockerfile 创建一个 docker 映像.我想在启动 docker 容器时执行一些脚本.目前我有一个 shell 脚本来执行所有必要的进程

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

CMD ["sh","start.sh"]

CMD ["sh","start.sh"]

我想用后台运行的进程执行一个shell命令示例

I would like to execute a shell command with a process running in background example

CMD ["sh", "-c", "mongod --dbpath/test &"]

CMD ["sh", "-c", "mongod --dbpath /test &"]

推荐答案

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

Besides the comments on your question that already pointed out a few things about Docker best practices you could anyway start a background process from within your start.sh script and keep that start.sh script itself in foreground using 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:

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

这篇关于在 Docker CMD 中执行后台进程的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆