4

I have the following nodejs dockerfile:

# pull image
FROM node:13.12.0-alpine

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install app dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm install

# add app
COPY . ./

# start app
CMD node server dev

I need to dynamically run a custom JS script inside the container after start up. How can I achieve this?

UPDATE: I tried adding the following entry point after CMD, but neither CMD not ENTRYPOINT was executed:

ENTRYPOINT node customScript.js

Added a wrapper shell script (startup.sh) to include both commands:

#!/bin/sh

nohup node server dev > startup.log && node data/scripts/custom.js > custom.log

Replaced CMD with:

CMD ["./startup.sh"]

This only executes the first command in the shell and not the second. I also don't see the output-redirect log files being created in the container.

9
  • Can you give an example? For example, you can write whatever code you want in your application's main function; is that a good place to do the setup you need? Or can you launch a separate container, or make an HTTP management call from the host, or use an entrypoint wrapper script? There's lots of options. Commented Oct 28, 2021 at 18:38
  • Just add entry point and script name at the end of your script like ENTRYPOINT[“sh”, Abc.sh] Commented Oct 28, 2021 at 19:30
  • @DavidMaze, see my update. I cannot run the script in a separate container as it would need a lot of the code from the node container. Commented Oct 29, 2021 at 4:50
  • All the code should be in the image, no? You should be able to docker run the-same-image some other command (if you do not use ENTRYPOINT). Commented Oct 29, 2021 at 11:20
  • 1
    @sotn It looks like as if you just want to make docker to start two endless processes instead of one. Is that correct? If so, see this: docs.docker.com/config/containers/multi-service_container Commented Nov 2, 2021 at 11:50

4 Answers 4

3

ideally, you should be running the process of nohup in background

there is no issue due to CMD or ENTRYPOINT

#!/bin/sh

nohup node server dev > startup.log && node data/scripts/custom.js > custom.log

use & last at your nohup command to start the process into backgroud mode

nohup ./yourscript &

or

nohup command &

after this you can run the node command

sh example

nohup nice "$0" "calling_myself" "$@" > $nohup_out &
node index.js
sleep 1
tail -f $nohup_out
Sign up to request clarification or add additional context in comments.

Comments

2

You should use a supervisor to run multiple processes in a single container. They will/may also auto-restart died process again. Without a supervisor, your processes may be messed up.

Ex:

In my case, I use s6 in the project alpine-s6-nginx-php to run processes in a single container. The project has an OOP-like inheritance model from:

  • base the image alpine-s6-base
  • to alpine-s6-nginx
  • to alpine-s6-nginx-php.

Base image prepares the s6 setup, nginx image adds nginx process and final alpine-s6-nginx-php image adds php-fpm7 process. You may check these repos and s6 supervisor. It is not needed to separate layers for each process, all processes can be added in a single Dockerfile. And also you may use s6 alternatives like systems or etc.

TLDR; you can use s6-overlay to build s6 supervised container images easily.

Comments

1

You can add entry point keyword and script name at the end of your script like ENTRYPOINT[“sh”, “Abc.sh”]

1 Comment

see my update to OP.
1
+25

If you need to run two commands in the docker startup, one should be non-blocking and other can be blocking call.

If both call are blocking then you need to start one command in separate thread/process and then run the other command.

This can be achieved in a parent script(sh) which can start one process for one command and call the last command in current process.

https://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/internal.html https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_01_04.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.