[英]Docker run to execute script in mount without exiting container automatically?
I have a simple bash script 'test.sh' in the root of mounted folder:我在已安装文件夹的根目录中有一个简单的 bash 脚本“test.sh”:
#!/bin/bash
Rscript -e "source('/home/rstudio/mount-folder/src/controller.R')";
However, when i try to mount folder and start the container with docker run as follows:但是,当我尝试挂载文件夹并使用 docker 启动容器时,运行如下:
docker run -d -p 8000:8787 -e ROOT=true -e DISABLE_AUTH=true --name container -v mount-folder/:/home/rstudio/ image_name /home/rstudio/test.sh
above run command starts the container but exits automatically.上面的运行命令启动容器但自动退出。
I am looking for a docker run command that starts the container, mounts the folder and then executes the bash script which is in the mount-folder without exiting the container.我正在寻找启动容器的 docker 运行命令,安装文件夹然后执行安装文件夹中的 bash 脚本而不退出容器。
(** dont want to go with docker exec command as it is not suitable for my use case for other reasons) (** 不想使用 go 和 docker exec 命令,因为其他原因它不适合我的用例)
Dockerfile: Dockerfile:
FROM rocker/rstudio:4.0.2
//some RUN commands to install necessary r packages
EXPOSE 8787
CMD tail -f /dev/null
Other details:其他详情:
Edit:编辑:
Docker containers shutdown automatically when run in detached mode. Docker 容器在分离模式下运行时自动关闭。 I think this article proposes a nice solution:我认为这篇文章提出了一个很好的解决方案:
http://bigdatums.net/2017/11/07/how-to-keep-docker-containers-running/ http://bigdatums.net/2017/11/07/how-to-keep-docker-containers-running/
You could add tail -f /dev/null
as the last command in your bash script instead so that the script will never halt unless it is told to do so.您可以将tail -f /dev/null
添加为 bash 脚本中的最后一个命令,这样脚本将永远不会停止,除非它被告知这样做。
When you do docker run [options] image_name [cmd]
the command you specify becomes the command for the container and replaces any the command specified in the dockerfile (that's why adding CMD tail -f /dev/null
doesn't do anything).当您执行docker run [options] image_name [cmd]
时,您指定的命令将成为容器的命令并替换 dockerfile 中指定的任何命令(这就是添加CMD tail -f /dev/null
不执行任何操作的原因)。 If you ran your container without the /home/rstudio/test.sh
at the end, it should stay running.如果你在最后没有/home/rstudio/test.sh
的情况下运行你的容器,它应该保持运行。
The solution would be to update your script to add the tail
command at the end.解决方案是更新您的脚本以在末尾添加tail
命令。
#!/bin/bash
Rscript -e "source('/home/rstudio/mount-folder/src/controller.R')";
exec tail -f /dev/null
If you can't update that script, you could instead add it to the command being passed to the container, with something like: docker run [options] image_name bash -c '/home/rstudio/test.sh && exec tail -f /dev/null'
如果您无法更新该脚本,则可以将其添加到传递给容器的命令中,例如: docker run [options] image_name bash -c '/home/rstudio/test.sh && exec tail -f /dev/null'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.