简体   繁体   English

Docker 在后台启动一个进程并且仍然交互使用容器

[英]Docker start a process on startup in the background and still use the container interactively

I use a Docker container for doing scientific calculations.我使用 Docker 容器进行科学计算。 All my libraries are installed inside the docker container.我所有的库都安装在 docker 容器内。 In addition, I got ParaView installed in order to do live visualizations.此外,我安装了 ParaView 以进行实时可视化。
For my simulation pipeline, I need to keep a ParaView server running inside the container while working with my bash shell inside it.对于我的模拟管道,我需要让 ParaView 服务器在容器内运行,同时在其中使用我的 bash shell。 A working solution for me is to use tmux as a terminal multiplexer, start the pvserver in one terminal session and use the other to do my simulation stuff.对我来说,一个可行的解决方案是使用 tmux 作为终端多路复用器,在一个终端 session 中启动 pvserver,然后使用另一个来做我的模拟工作。
However, this is very inconvenient and I would like to start the ParaView server on each start of the container automatically in the background.但是,这非常不方便,我想在每次容器启动时在后台自动启动 ParaView 服务器。 For this purpose, I tried to modify the ENTRYPOINT as:为此,我尝试将 ENTRYPOINT 修改为:

ENTRYPOINT pvserver && /bin/bash

This works but does not allow me to use the shell, nor can I move the pvserver process in the background.这可行,但不允许我使用 shell,也不能在后台移动 pvserver 进程。 Therefore it does not solve the issue of not using tmux for me.因此它不能解决我不使用 tmux 的问题。 Then I tried to move the pvserver command into a CMD block, but that only starts and directly ends the process.然后我尝试将pvserver命令移动到 CMD 块中,但这只会启动并直接结束进程。 So how can I start the server in the background while still being able to use the shell as usual when starting the container with the -it flag.那么如何在后台启动服务器,同时在使用-it标志启动容器时仍然能够像往常一样使用 shell。

Generally, I read a couple of questions regarding this topic already, but most of the questions (or the docker documentation itself) focus either running just one process in the background and using bash as main process to not terminate the container, or they focus on running multiple predefined processes (not the shell) simultaneously like in the documentation.一般来说,我已经阅读了几个关于这个主题的问题,但是大多数问题(或 docker 文档本身)都集中在后台运行一个进程并使用 bash 作为不终止容器的主进程,或者他们专注于像文档中一样同时运行多个预定义进程(不是 shell)。

The reason why the documentation you found says to do it that way, is because that's how containers are intended to be used.您找到的文档说要这样做的原因是因为容器的使用方式就是这样。 One process per container.每个容器一个进程。

You can try publishing a port on your service then using your local shell to interact with the container over the published port.您可以尝试在您的服务上发布一个端口,然后使用您的本地 shell 通过发布的端口与容器进行交互。

Or you can try splitting the shell and the server into two containers, and exec-ing into the shell container to then access the server container, but this is a bit convoluted and unneccessary imo.或者您可以尝试将 shell 和服务器拆分为两个容器,然后执行到 shell 容器中,然后访问服务器容器,但这有点复杂且不必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM