3

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.

1
  • Please include the Dockerfile you're using. Commented Sep 20, 2016 at 13:50

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

thats a possibilty.
A slight improvement here is to run 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.
0

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

thats the trick, the certs are different for each run of a container.
Shouldn't be a problem. If you wrap that startup logic in 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.
so this is basically what @Lix suggested above?
Yep, if you need to create state for each container which shouldn't be stored in the image, it has to happen in the start command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.