Search code examples
visual-studiodockerasp.net-coredocker-for-windowsdocker-desktop

Docker Desktop for Windows + ASP.NET Core Error


I have recently decided to give docker a try and I'm running into this error for 2 days now. No matter what I do, I can't seem to get rid of it.

For a .NET Core 2.2 application:

2>C:\Users\Russell\.nuget\packages\microsoft.visualstudio.azure.containers.tools.targets\1.4.4\build\Container.targets(256,5): error : An item with the same key has already been added.

This is freshly added Docker support for the project, no config changes. I've tried a clean, rebuild, updated all nugets, etc. If I run the project in IIS express it runs fine.

Anyone have any thoughts?

Edit 1: Docker file:

WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["SearchAPICore_Search/SearchAPICore_Search.csproj", "SearchAPICore_Search/"]
COPY ["SearchAPICore_Models/SearchAPICore_Models.csproj", "SearchAPICore_Models/"]
RUN dotnet restore "SearchAPICore_Search/SearchAPICore_Search.csproj"
COPY . .
WORKDIR "/src/SearchAPICore_Search"
RUN dotnet build "SearchAPICore_Search.csproj" -c Release -o /app

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

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


Solution

  • You probably have a left over container that's causing the issue. Try cleaning your environment with the following commands:

    Linux:

    # Clean exited containers
    docker ps --filter status=exited -q | xargs docker rm
    # Clean dangling images
    docker images --filter dangling=true -q | xargs docker rmi
    # Clean old tagged images 
    docker images --filter reference="$ContainerTag" -q | xargs docker rmi
    

    Windows PowerShell:

    # Clean exited containers
    docker ps --filter status=exited -q | %{ docker rm }
    # Clean dangling images
    docker ps --filter dangling=true -q | %{ docker rmi }
    # Clean old tagged images 
    docker images --filter reference="$ContainerTag" -q | %{ docker rmi }
    

    The last one might not be necessary but it can't hurt. Just make sure to either replace $ContainerTag with the name of the image, or create a variable with that value before hand.