Start Script for Multiple Processes in the background

support
raspberrypi3
docker

#1

I am having difficulties surrounding the startup script i’m trying to use to setup my system. Specifically this happens when i’m trying to call my start script from the end of my dockerfile, and run things in the background.

Below are the end of my dockerfile and start script.

Dockerfile

COPY ./src/start.sh ./src/start.sh

# start will run when container starts up on the device
EXPOSE 80

ENV INITSYSTEM on

WORKDIR /usr/src
CMD sh start.sh

Start Script
#!/bin/bash -x
echo "Starting Machine Comms"
python3 /usr/src/machineComm/main.py &
echo "Starting the frontend"
npm start --prefix frontEnd/ &
echo “Complete”

I am certain that I am calling the start script, the “echo” calls always work.
I have tried the following test cases:

  1. Removing the & from line 3 of the start script. This results in the python getting called successfully but execution of the script stops.
  2. Changing “sh start.sh” to “bash start.sh” or just “start.sh” results in no significant change in behavior.
  3. SSHing in and running “bash start.sh” from the command line. The script will run, echo calls will work, python and node will start running (I can see the logs and process)

Based of of test (3) I believe that my docker file is setup correctly and that there is nothing wrong with the setup of my container. I just can not wrap my head around why there is a significant difference between the CMD at the end of my dockerfile and SSHing in and running it directly.

Any guidance hints or tips would be appreciated.

Thanks,

Sam


#4

I don’t think your start.sh has any foreground task running, I think it dumps everything to background. Docker assumes that when the CMD exits, the container is done with.


#6

Nailed it. I added a “wait” to the end of my dockerfile and it is working fine. I don’t see this documented in the Dockerfile Reference but I suppose it makes enough sense.

If Docker assumes that when CMD exits that the container is done with, what does it do with the container? How can I still SSH in?

I’ll report back if I find the answer, haven’t seen it in the documentation yet.


#7

I’m working from experience rather than citable knowledge here, because I battled the same demon live at a hackathon.