This is an example on how to leverage multi-stage Dockerfile with docker-compose.dev.yml to improve DX with balena.
-
The dockerfile is split in 3 main stages :
build(all the preparation steps, including the compilations steps, etc.)prod(imports the important files from build and leave out the scaffolding. Then adds the configurations and commands for production)dev(imports all things needed for test and dev from the build stage. Then adds the configurations and commands for development)
-
It's important for
devto be the latest step as we'll want to be able to use balena'slive-reloadwhen pushing inlocal modeand it only works if we run the full Dockerfile (all steps). -
If we were to add a
teststage it should go betweenprodanddev. -
To build a certain stage we use
balena build --target _step_ -t _imagename:tag_ .( i.e.balena build --target prod -t hello:world .) -
We set the target in the
docker-compose.ymlwithtarget: prod -
Then we set the
docker-compose.dev.ymlwithtarget: devso it's overwritten when usinglocal modepush.
i.e. :
services:
hello:
build:
context: .
target: dev
- For live update to work in
local mode, thedevstep must be the latest step docker-compose.dev.ymlwon't be merged when doing abuild(currently only works when pushing in local mode). See this PR for information about how it actually works.