简体   繁体   中英

ASP.NET Core docker build error

I'm new to ASP.NET Core and docker. I've created a simple ASP.NET Core 2.0 app and try to use docker with it on Windows. However, I get this error:

Your Docker server host is configured for 'Linux', however the docker-compose project targets 'Windows'.

Although it seems to be pretty informative error, I can't find where to 'configure host for Windows'

It is docker-compose.dcproj file where you can set up the OS you want to target:

<DockerTargetOS>Linux</DockerTargetOS>

To switch docker daemon to the same OS you can use Docker tray icon or Docker Settings window (accessible from the same menu):
在此输入图像描述

Well basically the answer of Celestin Bochis and Pavel Agarkov are great. However since .net core 2.2 at least, the os of docker is stored in the .csproj file.

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

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    ...
   </PropertyGroup>

   ...
</Project>

And also don't forget to modify your docker file. The images should be the correct one. For .net core 2.2 That is :

Linux:
Microsoft/dotnet:2.2-aspnetcore-runtime AS base
microsoft/dotnet:2.2-sdk AS build

Windows:
microsoft/dotnet:2.2-aspnetcore-runtime-nanoserver-1803
microsoft/dotnet:2.2-sdk-nanoserver-1803

Make sure to choose the correct OS when you Enable docker support:

操作系统下拉菜单

Your docker daemon has to target Linux containers as well.

I got this error when I created the project to target Windows and later wanted to switch it to target to Linux. The steps are a little bit more involved if you want to use Linux containers instead:

  1. Unload docker-compose, edit the DockerTargetOS to Linux, then reload the project
  2. Go to docker-compose.yml. Make sure that the backslash is a forward slash. Should look like "WebApplication/Dockerfile"
  3. On the Dockerfile, for the base use "microsoft/aspnetcore:2.0" and for build, use "microsoft/aspnetcore-build:2.0" so it should look like this:

     FROM microsoft/aspnetcore:2.0 AS base WORKDIR /app EXPOSE 80 FROM microsoft/aspnetcore-build:2.0 AS build WORKDIR /src COPY WebApplication7/WebApplication.csproj WebApplication/ RUN dotnet restore WebApplication/WebApplication.csproj COPY . . WORKDIR /src/WebApplication RUN dotnet build WebApplication.csproj -c Release -o /app 
  4. Right click on the Docker tray icon > settings > Shared Drives > pick the drive your project resides in.

If the docker is running on the windows machine then you need to change the value of "DockerTargetOS" should be "Windows" in .dcproj file.

Unload the docker project from visual studio and edit the project and set the value "Windows" to "DockerTargetOS".

<DockerTargetOS>Windows</DockerTargetOS>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM