Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2017 Docker - change the target for multi stage builds

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.

like image 669
Pectus Excavatum Avatar asked Dec 27 '17 22:12

Pectus Excavatum


People also ask

How do multistage Docker builds work?

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.

What is Docker build target?

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.

Can a Dockerfile build multiple images?

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.


2 Answers

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
like image 51
Gregor Avatar answered Oct 05 '22 23:10

Gregor


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.

like image 45
nNickSize Avatar answered Oct 06 '22 00:10

nNickSize