The answer is just to set the container to restart on crash. In development, I'm using nodemon which prevents the process from exiting on a crash - so I assumed that the process crashing didn't cause the container to exit. My mistake. The process crashing does in fact cause the container to exit when using node and not nodemon.
{
"scripts": {
"development:service": "nodemon ./service/server.js",
"production:service": "node ./service/server.js"
}
}
How do I restart a docker container when a process inside the container crashes?
Would the best way be to set --autorestart ON_FAILURE for the container and then make the container crash when the inner process crashes? If so, how do I force the container to crash when one of it's process crashes?
I'm using the image node:10.15.3.
Useful Link: how to make a container restart if the container, itself, crashes
FROM node:10.15.3
ENV NODE_ENV development
# Create application directory.
RUN mkdir -p /src
WORKDIR /src
# Install app dependencies and build.
ADD . /src
RUN yarn install --force
RUN ["chmod", "+x", "./run.sh"]
CMD [ "./run.sh" ]
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user 13 0.0 0.1 17952 2840 ? S 15:09 0:00 bash ./install.sh
user 72 0.5 3.2 889668 66728 ? Sl 15:09 0:00 /usr/local/bin/node /opt/yarn-v1.13.0/bin/yarn.js start
user 97 2.4 3.2 919992 65748 ? Sl 15:09 0:01 /usr/local/bin/node ./service/server.js
You can use the --restart flag for docker run command https://docs.docker.com/engine/reference/commandline/run/#restart-policies---restart
Example:
docker run --restart=on-failure my-image-name
Or if you are using docker-compose then you restart: on-failure for the service
https://docs.docker.com/compose/compose-file/#restart
When you run multiple services in a container, and you are very, very sure you want this (it's best practice to run a single process in a single container), you may want a process supervisor (for example systemd) which restart any crashed process inside, or crash if any of it's supervised processes crash.
There are also some other, more lightweight, ways to accomplish this, like a wrapper bash script.
It's all described here: https://docs.docker.com/config/containers/multi-service_container/
Edit after question was updated with Dockerfile:
The easiest way is probably to make sure run.sh crashes if any of it's subprocesses crash. Then the container will crash, and optionally restart if you use --restart=on-failure in docker run.