i have to run some shell script which creates some certificates before the node app.js command is called in Dockerfile. how can i do it so it won't exit after running a shell script and will continue to run the node app.js web app.
-
Please include the Dockerfile you're using.BMitch– BMitch2016-09-20 13:50:23 +00:00Commented Sep 20, 2016 at 13:50
2 Answers
What I have done in the past is have my entrypoint defined in the Dockerfile be a shell script and then run the node command from within that script.
So in my Dockerfile I have this -
ENTRYPOINT ["./docker-start.sh"]
And my docker-start.sh script contains this:
#! /bin/bash
# Initialization logic can go here
echo "Starting node..."
node start.js $* # The $* allows me to pass command line arguments that were passed to the docker run command.
# Cleanup logic can go here
2 Comments
exec node start.js $* so that SIGTERM and other process signals get forwarded to the NodeJS process where you can handle graceful shutdown. See unix.stackexchange.com/a/196053 for more.Do you want your certificates to be stored in the image, so the certs are the same for every container you run from the image? If so then you can just do that in the Dockerfile:
COPY create-certs.sh /create-certs.sh
RUN /create-certs.sh
CMD ["node", "etc."]
When you docker build, the output of the RUN instruction will be saved in the image, so your certs are permanently stored and you don't need to create them when the container starts.
If you need to create certs on demand for each container, then you can use a bootstrap shell script which does multiple steps - check if the certs exist, create them if needed, then start the Node app. The bootstrap shell script is what you use for your CMD.
4 Comments
bootstrap.sh and the final command in the script starts your Node app, then the script process will stay alive and that's what Docker monitors.