简体   繁体   English

在Docker中运行多个python进程

[英]Running Multiple python processes in Docker

I am new to docker, trying to run multiple python processes in docker. 我是docker的新手,试图在docker中运行多个python进程。 Though it's not recommended, however, it should work as suggested here " https://docs.docker.com/engine/admin/multi-service_container/ " 虽然不推荐,但它应该按照“ https://docs.docker.com/engine/admin/multi-service_container/ ”的建议工作。

My Dockerfile : 我的Dockerfile:

FROM custom_image
MAINTAINER Shubham 
RUN apt-get update -y
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["/bin/bash"]
CMD ["start.sh"]

start.sh : start.sh:

nohup python flask-app.py &
nohup python sink.py &
nohup python faceConsumer.py &
nohup python classifierConsumer.py &
nohup python demo.py &
echo lastLine

run command : 运行命令:

docker run --runtime=nvidia -p 5000:5000 out_image
  • the same shell script worked when I go to terminal and run. 当我去终端并运行时,相同的shell脚本工作。
  • tried without nohup, didn't work. 尝试没有nohup,没有工作。
  • tried python subprocess also to start other python processes, didn't work. 尝试过python子进程也启动其他python进程,没有用。

Is it possible to run multiple processes without supervisord or docker-compose? 是否可以在没有supervisord或docker-compose的情况下运行多个进程?

update: not getting any error, only "lastLine" is getting printed and docker container exits. 更新:没有收到任何错误,只有“lastLine”被打印并且docker容器退出。

Docker docs has examples of how to do this. Docker文档中有如何执行此操作的示例。 If you're using Python, then using supervisord is a good option. 如果您使用的是Python,那么使用supervisord是一个不错的选择。

FROM ubuntu:latest
RUN apt-get update && apt-get install -y supervisor
RUN mkdir -p /var/log/supervisor
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY my_first_process my_first_process
COPY my_second_process my_second_process
CMD ["/usr/bin/supervisord"]

The advantage of this over running a bunch of background processes is you get better job control and processes that exit prematurely will be restarted. 这比运行大量后台进程的优点是可以获得更好的作业控制,并且可以重新启动过早退出的进程。

Your problem is putting everything in background. 你的问题是把一切都放在后台。 Your container starts, executes all commands and then exits when CMD process finishes - despite background processes running. 您的容器启动,执行所有命令,然后在CMD进程完成时退出 - 尽管后台进程正在运行。 Docker does not know that. Docker不知道这一点。

You could try running everything else in background but then 您可以尝试在后台运行其他所有内容

python demo.py

as it is. 原样。 This would cause the process to stay alive assuming demo.py does not exit. 假设demo.py没有退出,这将导致进程保持活动状态。

您也可以在分离模式下运行或将nohup重定向到log / nohup.out,因为默认的docker通过套接字运行命令,重定向不会发生。

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

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