Search code examples
pythonbashdockercontainers

Docker: Container permanently restarting


I'm new to the whole Docker Container topic and currently trying to run multiple python scripts in shell via bash script (cause it seemed to be the easiest thing to do in terms of running multiple python scripts at the same time). Before that I build my Image via the following Dockerfile

FROM debian:buster-slim

ENV PACKAGES1="build-essential git python3"

RUN apt-get update && \
    apt-get install -y $PACKAGES1 
    
COPY /mnt /mnt

CMD [ "/bin/bash", "/mnt/setup_bash.sh" ]

to execute the setup_bash.sh

#! /bin/bash

python3 script1.py & 
python3 script2.py &

after running the resulting container he keeps restarting and doesn't stay active. Meanwhile the docker logs command doens't display any errors so I'm kinda clueless what's the problem.


Solution

  • The main process of the system exits, so docker is killed. You are running two processes in the background and the main bash scripts quits. You could:

    • run one script on foreground, or
    • run sleep infinity to keep the main script running
    • refactor it all and for complex setups consider using service management, like supervisord

    Like with option 2:

    #! /bin/bash
    python3 script1.py & 
    python3 script2.py &
    sleep infinity  # don't quit