If I have a docker file that has multiple stages (such as base and build), is there a way to change the docker command that visual studio uses when debugging the container - it seems to use the first build in the docker file, without invoking the subsequent stages.
Here is my docker file:
FROM microsoft/aspnetcore:2.0.3 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0.3 AS build
WORKDIR /src
COPY *.sln ./
COPY Web/Web.csproj Web/
RUN dotnet restore
COPY . .
WORKDIR /src/Web
RUN dotnet build -c Release -o /app
FROM build AS publish
#RUN npm install
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
# Set the entry point of the application.
ENTRYPOINT ["dotnet", "Web.dll"]
When I hit f5, it will create the container with the base image, and subsequently result in error because the project needs the node install of the aspnetcore-build image; I can resolve this by changing the base image to be the aspnetcore-build.
However, is there a way to tell the project in visual studio that it is a multi-stage build and needs to use build?
I am very new to docker so perhaps I am missing something obvious.
With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don't want in the final image.
docker build --target builder -f Dockerfile -t react-b:latest . The --target the option allows you to specify a specific stage at which you'd like to stop. It can be useful if your last step is only used in production or for testing.
A multistage build allows you to use multiple images to build a final product. In a multistage build, you have a single Dockerfile, but can define multiple images inside it to help build the final image.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With