Docker multistage build --target argument

#1

We have a Node.js project and are attaching a remote debugger to an instance of Node running on a device in local mode.
Depending on whether the container should be used for production or debugging, the CMD command in the Dockerfile should be sth like
CMD node src/main.js
or
CMD node --inspect=0.0.0.0:9229 --nolazy src/main.js

We wanted to use a multi-stage Docker build to achieve this duality of options, like this:

# previous steps...

FROM buildresult as development
ARG DEBUG_PORT=9229
ENV NODE_ENV development
CMD node --inspect-brk=0.0.0.0:${DEBUG_PORT} --nolazy src/main.js

FROM buildresult as production
ENV NODE_ENV production
CMD node src/main.js

Because the production target is the last, this should work fine with the Balena builder. But it seems that we cannot specify a --target option, like it would be possible with docker build, when using the Balena CLI (and specifically balena local push). Is there any way of achieving the desired result?

#4

Hey @maciej.ldc, the right way to do this split in the execution is likely not the multistage build, but by a split in the application start. For example you can add a start script, start.sh, with content similar to this:

# contents of start.sh
#!/usr/bin/bash 

if [ -n "$DEVELOPMENT" ]; then
  # This will run if the DEVELOPMENT environment variable is set to any value
  node --inspect-brk=0.0.0.0:${DEBUG_PORT} --nolazy src/main.js
else
  node src/main.js
fi

and then in your CMD set to CMD ["bash", "start.sh](not to forget copying the file in). After this, for devices where you set the DEVELOPMENT env var to a non-empty value it will run the that command line, or if it’s not set, it will run the production command line.

What do you think?

#6

Thanks @imrehg, sounds like a good workaround!