Docker multistage build --target argument


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
CMD node --inspect= --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
ENV NODE_ENV development
CMD node --inspect-brk=${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?


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,, with content similar to this:

# contents of

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

and then in your CMD set to CMD ["bash", "](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?


Thanks @imrehg, sounds like a good workaround!