Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Multiple python processes in Docker

Tags:

I am new to docker, trying to run multiple python processes in docker. Though it's not recommended, however, it should work as suggested here "https://docs.docker.com/engine/admin/multi-service_container/"

My 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 :

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.
  • tried without nohup, didn't work.
  • tried python subprocess also to start other python processes, didn't work.

Is it possible to run multiple processes without supervisord or docker-compose?

update: not getting any error, only "lastLine" is getting printed and docker container exits.

like image 859
Shubham Jain Avatar asked Jan 30 '18 12:01

Shubham Jain


People also ask

Can you run multiple processes in a Docker container?

It's ok to have multiple processes, but to get the most benefit out of Docker, avoid one container being responsible for multiple aspects of your overall application. You can connect multiple containers using user-defined networks and shared volumes.

Can I have multiple CMD in Dockerfile?

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect. If CMD is used to provide default arguments for the ENTRYPOINT instruction, both the CMD and ENTRYPOINT instructions should be specified with the JSON array format.


2 Answers

Docker docs has examples of how to do this. If you're using Python, then using supervisord is a good option.

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.

like image 74
six8 Avatar answered Oct 11 '22 17:10

six8


Your problem is putting everything in background. Your container starts, executes all commands and then exits when CMD process finishes - despite background processes running. Docker does not know that.

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.

like image 28
Hannu Avatar answered Oct 11 '22 15:10

Hannu