Visual Studio 2017 Docker - change the target for multi stage builds
According to this blog post, Visual Studio builds only the first image when you hit F5. However, I observed that this happens when using the Debug
configuration only (as correctly stated by Matheus' Answer).
I changed all solution configurations to use the Release
configuration for the docker project (.dcproj) only. This will build all images and correctly executes the multi-stage build without setting --target
to whatever.
EDIT
However, this configuration cannot be debugged from within Visual Studio. To allow for local debugging inside containers, you should have a separate Dockerfile that does not use a multistage build.
To use build target configuration from docker file, you need to add a docker-compose.vs.debug.yml file. In that file, just specify build target for the service and that's it.
Example:
version: '3.4'services: my.api: build: target: build
When you are using a multistage Dockerfile on VS 2017, you can run it in debug and release mode.
In DEBUG mode, VS will set the docker build with the "base" target and tag "DEV", so docker will build an empty image, as you already pointed.
Then VS will start a container of your image, so as soon it is running will publish the code to the current WORKDIR inside the container, start the application and attach to debug.
UPDATE
Lather that week, I found out that VS generate a docker-compose file at the obj\docker folder, that is responsible for the build in DEV mode.