docker - passing file name as arguments to nodejs application docker - passing file name as arguments to nodejs application kubernetes kubernetes

docker - passing file name as arguments to nodejs application


You mention in your first comment the requirement that filenames be specified at runtime. Something like this will work:

ENV work /appWORKDIR $workCOPY ./arg1.json ./arg2.json $work/CMD["node", "index.js", "./arg1.json", "arg2.json"]

Runtime command:

docker run -v $(pwd)/myarg1.json:/app/arg1.json -v $(pwd)/myarg2.json:/app/arg2.json <image> 


You can do this with the help of ENV, build the Dockerfile with some default ENV then if you need to overide with some other file, overide them at run time.

FROM node:alpineENV HOME="/app"ENV file1="file1"ENV file2="file2"COPY index.js $HOME/index.jsCMD ["/bin/sh", "-c", "node $HOME/index.js $HOME/$file1 $HOME/$file2"]

index.js

const args = process.argv;console.log(args);

output of nodejs script

[ '/usr/local/bin/node', '/app/index.js', '/app/file1', '/app/file2' ]

To overide filename for different config

docker run --rm -it -e file1="stage.json" abc

Now the output will be

[  '/usr/local/bin/node',  '/app/index.js',  '/app/stage.json',  '/app/file2']

If you are expecting big number of argument then better follow this method.

FROM node:alpineENV HOME="/app"COPY index.js $HOME/index.jsRUN echo $'#!/bin/sh \n\     node $HOME/index.js $@' >> /entrypoint.shRUN chmod +x /entrypoint.shENTRYPOINT ["/entrypoint.sh"]

So now pass as many argument as you want, docker run

docker run --rm -it -e file1="stage.json" abc "$HOME/arg1.json $HOME/arg2.json $HOME/arg3.json"

node args result will be

[  '/usr/local/bin/node',  '/app/index.js',  '/home/adiii/arg1.json',  '/home/adiii/arg2.json',  '/home/adiii/arg3.json']