nate nate - 4 months ago 74
Node.js Question

Pass argument to dockerfile @ runtime

Basically want to be able to run a docker container with specified arguments

dockerfile:

FROM nodesource/node:4.0

ADD package.json package.json
RUN npm install
ADD . .

ENTRYPOINT ["node","index.js"]


I am essentially running the tutorial code found for rabbitMQ:
https://www.rabbitmq.com/tutorials/tutorial-one-javascript.html

The only difference is, for me, instead of a hard coded Que name, i am using $args for it

var argv = require('minimist')(process.argv.slice(2));
var q = argv.que;


the cmd line way i run my nodejs is:
node index.js --que 1


and its working fine. however when executing from the
docker run
it doesn't seem to be working (args not read correctly)

i have tried:

docker run -it mydockerimage --que (my queue name)


but getting error:


Error response from daemon: oci runtime error: exec: "--que":
executable file not found in $PATH.


What is the correct way for me to write this docker image so that i can pass in arguments at runtime?

I know i can do it by overriding the --entrypoint, but ideally i would like to type less...is this even possible?

Answer

Combining ENTRYPOINT and CMD allows you to specify the default executable for your image while also providing default arguments to that executable which may be overridden by the user.

FROM nodesource/node:4.0

ADD package.json package.json
RUN npm install
ADD . .

ENTRYPOINT ["node","index.js"]
CMD ["--que","1"]

So, the default behaviour of docker run will be to send the argument --que 1

docker run -it mydockerimage

But you can override that value

docker run -it mydockerimage --que 2
Comments