10

I'm trying to run my ASP.NET Core 2.1 application with docker image. For that I have docker file with the following content:

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /app

# copy csproj and restore as distinct layers
COPY . .
RUN dotnet restore

# copy everything else and build app
COPY Presentation/MyProject.Web/. ./Presentation/MyProject.Web/
WORKDIR /app/Presentation/MyProject.Web
RUN dotnet publish -c Release -o out

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

But when execute the command docker build -t MyProject-web . it gives me an error:

The command '/bin/sh -c dotnet publish -c Release -o out' returned a non-zero code: 1


 MyProject.Web -> /app/Presentation/MyProject.Web/bin/Release/netcoreapp2.1/MyProject.Web.dll
  No executable found matching command "dotnet-/app/Build\ClearPluginAssemblies.dll"
/app/Build/ClearPluginAssemblies.proj(21,5): error MSB3073: The command "dotnet "/app/Build\ClearPluginAssemblies.dll" "OutputPath=/app/Build/../Presentation/MyProject.Web/bin/Release/netcoreapp2.1/|PluginPath=/app/Presentation/MyProject.Web/Plugins/DiscountRules.CustomerRoles/;/app/Presentation/MyProject.Web/Plugins/ExchangeRate.EcbExchange/;/app/Presentation/MyProject.Web/Plugins/ExternalAuth.Facebook/;/app/Presentation/MyProject.Web/Plugins/Payments.CheckMoneyOrder/;/app/Presentation/MyProject.Web/Plugins/Payments.Manual/;/app/Presentation/MyProject.Web/Plugins/Payments.PayPalStandard/;/app/Presentation/MyProject.Web/Plugins/Payments.Square/;/app/Presentation/MyProject.Web/Plugins/Payments.Worldpay/;/app/Presentation/MyProject.Web/Plugins/Pickup.PickupInStore/;/app/Presentation/MyProject.Web/Plugins/Shipping.FixedByWeightByTotal/;/app/Presentation/MyProject.Web/Plugins/Shipping.UPS/;/app/Presentation/MyProject.Web/Plugins/Tax.FixedOrByCountryStateZip/;/app/Presentation/MyProject.Web/Plugins/Widgets.GoogleAnalytics/;/app/Presentation/MyProject.Web/Plugins/Widgets.NivoSlider/|SaveLocalesFolders="" exited with code 1.
The command '/bin/sh -c dotnet publish -c Release -o out' returned a non-zero code: 1

Edit 1: Here is my project structure:

Build
    ClearPluginAssemblies
Libraries
    MyProject.Core
    MyProject.Data
    MyProject.Services
Plugins
    MyProject.Plugin.Discount
    MyProject.Plugin.Payment
    ..
Presentation
    MyProject.Web   
    MyProject.Web.Framework
Tests

Edit:2 Web project file:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>        
    <Description>MyProject.Web is also an MVC web application project, a presentation layer for public store and admin area.</Description>                
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\Libraries\MyProject.Core\MyProject.Core.csproj" />
    <ProjectReference Include="..\..\Libraries\MyProject.Data\MyProject.Data.csproj" />
    <ProjectReference Include="..\..\Libraries\MyProject.Services\MyProject.Services.csproj" />
    <ProjectReference Include="..\MyProject.Web.Framework\MyProject.Web.Framework.csproj" />
  </ItemGroup>

  <ItemGroup>
    <!-- We copy the entire \App_Data directory. But we ignore JSON files and data protection keys  -->
    <Content Include="App_Data\**" CopyToPublishDirectory="PreserveNewest" Exclude="App_Data\*.json" />
    <Content Update="App_Data\*.json" CopyToPublishDirectory="Never" />
    <Content Update="App_Data\DataProtectionKeys\*.xml" CopyToPublishDirectory="Never" />

    <Compile Remove="Plugins\**" />
    <EmbeddedResource Remove="Plugins\**" />
    <None Remove="Plugins\**" />

    <Content Include="Plugins\**" CopyToPublishDirectory="PreserveNewest" Exclude="Plugins\**\*.config;Plugins\**\*.cshtml;Plugins\**\*.json" />
    <Content Include="Themes\**" CopyToPublishDirectory="PreserveNewest" Exclude="Themes\**\*.config;Themes\**\*.cshtml;Themes\**\*.json" />

    <!-- We copy the \Logs directory -->
    <Content Include="Logs\**" CopyToPublishDirectory="PreserveNewest" />

    <None Update="Areas\Admin\sitemap.config">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

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

  <!-- This target execute after "Build" target.
    We use it to clean up folder with plugins from unnecessary and obsolete libraries. -->
  <Target Name="NopTarget" AfterTargets="Build">
    <ItemGroup>
      <!-- Get plugin description files to get plugin paths -->
      <PluginsDescription Include="$(MSBuildProjectDirectory)\Plugins\**\plugin.json;" />      
      <!-- Get paths for all plugins -->
      <PluginsFolders Include="@(PluginsDescription->'%(relativedir)')" />

      <!-- Get all the libraries from the shadow copy folder to remove them,
        because depending on the settings, this may not happen when the application is starting,
        but this can lead to unpredictable results during debugging of the project. -->
      <ShadowCopiesLibraries Include="$(MSBuildProjectDirectory)\Plugins\bin\*.*" Exclude="$(MSBuildProjectDirectory)\Plugins\bin\placeholder.txt" />
    </ItemGroup>
    <PropertyGroup>
      <PluginsFolders>@(PluginsFolders)</PluginsFolders>
    </PropertyGroup>
    <!-- Delete libraries from the shadow copy folder -->
    <Delete Files="@(ShadowCopiesLibraries)" />
    <!-- When .NET Core builds a project, it copies all referenced libraries to the output folder.
      For plugins it creates too many unnecessary files that just take up space.
      At the moment you can't disable this behavior. That's why we have to manually delete all unnecessary libraries from plugin output directories. -->
    <MSBuild Projects="$(MSBuildProjectDirectory)\..\..\Build\ClearPluginAssemblies.proj" Properties="PluginPath=$(PluginsFolders)" Targets="NopClear" />
  </Target>

  <PropertyGroup>  
    <ConcurrentGarbageCollection>false</ConcurrentGarbageCollection>
  </PropertyGroup>

</Project> 
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
  • What is your project folder structure? Which path did you run `docker build`? Are you miss `.` in your `docker build` command? Share us a demo project to reproduce your issue. – Edward Dec 03 '18 at 02:44
  • @TaoZhou: Sorry I'd missed `.` that has been updated in the question. Also, added project structure. Further note, I have given this solution to another one, and he is able to build the image and run the container, however with the same I'm facing an issue, that's strange – Divyang Desai Dec 03 '18 at 17:05
  • Can you post the project file of MyProject.Web? And,.. you copy the solution including all artifacts into your docker image. (and MyProject.Web is copied twice). You could try to clean the solution first to make sure, that it's not a problem with existing artifacts. If this doesn't help, Id' remove everything starting from your publish line and build and run your image. Then go into your running container, check what was copied and run the publish command manually - or test some other commands. – Christoph Lütjen Dec 04 '18 at 19:53
  • @ChristophLütjen: Added MyProject.Web in the question – Divyang Desai Dec 05 '18 at 04:03
  • Are you trying to run this on "Docker for Windows"? Have you switched to Windows containers? The fact that your error shows '/bin/sh' even though your URN command does not specify the shell, seems a little suspicious. – sxm1972 Dec 11 '18 at 13:16
  • Do you run the command from **root folder**? This error is related with `ClearPluginAssemblies.dll` and I found you have task in ``. First, try to comment out this task to check whether this issue exists. You are running publish command from `MyProject.Web`, try to run this command directly from Folder system to check whether you will get any similar error. – Edward Dec 12 '18 at 03:29
  • Issue seems with folder structure, I was running it with linux container and that was creating an issue, I've catch it before @sxm1972 commented here. – Divyang Desai Dec 12 '18 at 10:45
  • @Div did you find a solution? I have the same with `docker run` in container e.g. `docker run .... dotnet /path/assembly.dll` and it fails with error because it tries to run `dotnet-/path/assembly.dll` – dr11 May 04 '20 at 17:54
  • 1
    @deeptowncitizen, Yes, I was using Linux container on windows system, I switched to windows container and it started working – Divyang Desai May 05 '20 at 05:42

1 Answers1

1

This looks more like a dotnet publish issue then an actually issue with docker.

If you search on the actual error that dotnet cli shows MSB3073, you can find that it's often related to post build events. In many cases, the path to each folder/assembly referenced in the post build event is not correct.

If we look closer at where we are when the publish command is executed

WORKDIR /app/Presentation/MyProject.Web

And then for example look at the PluginPath in the error message

..PluginPath=/app/Presentation/MyProject.Web/Plugins/DiscountRules.CustomerRoles/;..

You can then see that the path is not inline from where the command is executed.

One solutions would be to add absolute path or make them relative from where the publish command is executed. You could also specify a specific post/pre-events for each configuration, here's an answer regarding publish-events

Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69