2

I recently started learning docker, But I am facing issues while hosting a simple asp.net core web api boiler plate app created using Visual Studio Code inside a docker windows container.

I have set Docker Fie in project and run docker build --tag helloapi .

But when I run docker run -p 9000:80 helloapi I get bellow error:

It was not possible to find any compatible framework version The specified framework 'Microsoft.AspNetCore.App', version '2.1.1' was not found. - Check application dependencies and target a framework version installed at: \ - Alternatively, install the framework version '2.1.1'.

I have tried many tutorials this this and this.

I have a tried different docker images for asp.net core this this and this.

dotnet core version:

 dotnet --info

.NET Core SDK (reflecting any global.json):
  Version:   2.1.402

.NET Core SDKs installed:
  2.1.402 

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.4 
  Microsoft.AspNetCore.App 2.1.4 
  Microsoft.NETCore.App 2.1.4 

helloapi.csproj file

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>

</Project>

Docker File:

FROM microsoft/aspnetcore
ARG source
WORKDIR /app
EXPOSE 80
COPY ${source:-/publish} .
ENTRYPOINT ["dotnet", "HelloApi.dll"]

enter image description here

Note: I am not able to Switch to Linux Container because docker is showing some run time error, So I am using Windows Container and also IIS Server is not enabled in Turn windows features.

I am not able to figure out what wrong am I doing ?

Can anyone guide me the correct way to host api using docker in windows container.

Any help would be great.

4
  • Can you show the contents of your Dockerfile as well? Commented Oct 24, 2018 at 4:48
  • Have you run docker build first? Commented Oct 24, 2018 at 6:06
  • @SimplyGed , I have updated, Thanks. Commented Oct 24, 2018 at 11:20
  • @MarcusHöglund , Its is obvious and I ran it, I have updated the the info in the question. Thanks. Commented Oct 24, 2018 at 11:21

4 Answers 4

2

Looks like the generated dockerfile form vs don't reference the dotnet:2.1-aspnetcore-runtime and dotnet:2.1-sdk.

Try to these steps

Add below Dockerfile to your solution folder

FROM microsoft/dotnet:2.1-sdk AS build-env
WORKDIR /build
COPY . /build

#Restore
RUN dotnet restore ./HelloApi.sln

RUN dotnet publish ./HelloApi/ -o /publish --configuration Release

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

Build it

docker build -t app1 .

Run it

docker run -p 8080:80 --name appcontainer1 app1

Test it

Browse to http://localhost:8080

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like the tutorials you have been following are all for .NET Core 2.0 but your project is using .NET Core 2.1.

As you (probably) know, the first line in the Dockerfile defines what image to run your application on. In your case you microsoft/aspnetcore. If you take a look at the Docker Hub page for the microsoft/aspnetcore image you will see it says:

Latest images for 2.1 and newer are now available on microsoft/dotnet. See this link for more details about migrating to 2.1.

Clicking on that link takes you to the Docker Hub page for the microsoft/dotnet image, which supports .NET Core 2.1.

So, as stated in another answer, you need to change your Dockerfile to reference a different image that has the .NET Core 2.1 runtime installed. You could just change the first line to:

FROM microsoft/dotnet

which could use an image that has the .NET Core 2.1 SDK installed, but that image would be overkill if you are planning to deploy the image to production.

Current practice is to use multi-stage Dockerfiles to perform the build, test and publish steps inside the container. You can read about this here. This allows you to use a different image base to build and test your application but only use the smaller runtime image for the final production image.

Take a look at some of the .NET Core Docker Samples to see how to do this.

Just be mindful when you are looking for tutorials that they use the newer .NET Core 2.1 as the base image, or ensure you create any applications using the same .NET Core version as the tutorial.

Hope that helps.

1 Comment

Thanks, Appreciate for the detailed info. I will try this and let you know later.
1

Dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
COPY ./SampleWebApi.csproj /SampleWebApi/
RUN dotnet restore ./SampleWebApi/SampleWebApi.csproj
COPY . ./SampleWebApi/
WORKDIR /SampleWebApi/
RUN dotnet build "SampleWebApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "SampleWebApi.csproj" -c Release -o /app/publish

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

docker-compose.yml

version: '3.5'

services:
    sample-web-api:
        build: SampleWebApi/
        restart: always
        ports:
            - "7000:80"

Run the below command

docker-compose build
docker-compose up

Sample web API code along with Dockerfile on Github

1 Comment

Thank you, I will try this next time :)
0

I've successfully hosted dotnet core 2.1 WebAPI applications in docker using Kestrel web-server with the microsoft/dotnet image and tags of 2.1-sdk and dotnet:2.1-aspnetcore-runtime for build and runtime respectively in multi-stage build e.g.

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
FROM microsoft/dotnet:2.1-sdk AS build

Hope this helps.

1 Comment

I am using windows container , Could you share your working DockerFile ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.