Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dockerize an ASP.NET Core 2.0 application?

I have a simple ASP.NET Core 2.0 application with 2 projects in a solution. One project is a class library (WorldLibrary.dll), which is referenced by the web application (WeatherWeb.dll).

I have tried to follow the steps here:

https://docs.docker.com/engine/examples/dotnetcore/

This is my Dockerfile:

FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "WeatherWeb.dll"]

But, there is a problem with the referenced .dll. I get the following output from the "docker build" command:

C:\Users\olavt\source\repos\Weather\WeatherWeb>docker build -t weather .
Sending build context to Docker daemon  6.398MB
Step 1/10 : FROM microsoft/aspnetcore-build:2.0 AS build-env
 ---> df4c9af52c86
Step 2/10 : WORKDIR /app
 ---> Using cache
 ---> ae9d1b099da7
Step 3/10 : COPY *.csproj ./
 ---> Using cache
 ---> 2d9f3fba6470
Step 4/10 : RUN dotnet restore
 ---> Using cache
 ---> 19af5fb355a3
Step 5/10 : COPY . ./
 ---> 1704520a3ced
Step 6/10 : RUN dotnet publish -c Release -o out
 ---> Running in 7bcdf847e4dc
/usr/share/dotnet/sdk/2.0.2/NuGet.targets(792,5): warning MSB3202: The project file "/WorldLibrary/WorldLibrary.csproj" was not found. [/app/WeatherWeb.csproj]
Microsoft (R) Build Engine version 15.4.8.50001 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

/usr/share/dotnet/sdk/2.0.2/Microsoft.Common.CurrentVersion.targets(1768,5): warning : The referenced project '../WorldLibrary/WorldLibrary.csproj' does not exist. [/app/WeatherWeb.csproj]
Controllers/Api/WeatherController.cs(6,7): error CS0246: The type or namespace name 'WorldLibrary' could not be found (are you missing a using directive or an assembly reference?) [/app/WeatherWeb.csproj]
Controllers/WeatherController.cs(6,7): error CS0246: The type or namespace name 'WorldLibrary' could not be found (are you missing a using directive or an assembly reference?) [/app/WeatherWeb.csproj]
Controllers/Api/WeatherController.cs(14,39): error CS0246: The type or namespace name 'Weather' could not be found (are you missing a using directive or an assembly reference?) [/app/WeatherWeb.csproj]
The command '/bin/sh -c dotnet publish -c Release -o out' returned a non-zero code: 1

C:\Users\olavt\source\repos\Weather\WeatherWeb>

How should I properly get the referenced .dll copied over correctly?

like image 469
OlavT Avatar asked Oct 16 '17 16:10

OlavT


People also ask

Can you Dockerize an existing application?

Dockerizing an existing project seems like a big undertaking, but it can be accomplished with a few specific steps. Once done, the Compose file can be added to the repository, reducing setup time and standardizing the local development environment.


1 Answers

The ASP.NET Core project templates contain an option to add Docker support with the option to target Linux or Windows Containers.

Even if you create an ASP.NET Core web application without Docker support, you can add it to an existing project from the Project > Docker Support menu

The generated Dockerfile executes defines the base image, executes the dotnet restore, build, publish steps and creates a final image that runs the web application :

FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY WebApplication1/WebApplication1.csproj WebApplication1/
RUN dotnet restore
COPY . .
WORKDIR /src/WebApplication1
RUN dotnet build -c Release -o /app

FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]

It also generates a docker-compose.yml file that describes the imagesyou wish to deploy. In this case I've added two web applications. The first was created with Docker support. In the second, Docker support was added afterwards. The docker-compose.yml file includes both :

version: '3'

services:
  webapplication1:
    image: webapplication1
    build:
      context: .
      dockerfile: WebApplication1\Dockerfile

  webapplication2:
    image: webapplication2
    build:
      context: .
      dockerfile: WebApplication2\Dockerfile
like image 64
Panagiotis Kanavos Avatar answered Oct 12 '22 04:10

Panagiotis Kanavos