Dotnet Cannot Restore Project Correctly While Using Docker - c#

I have a .Net webapi project, and I want to dockerize it. Without docker, dotnet sdk(6.0) can restore everything correctly and there is no obstacle for me to publish the app. But when I use docker to do that, it gives me tones of errors with the error codes CS0246, CS8714, CS0103, CS0535, CS0738. You can find my project folder structure and relevant information below:
src
|-->API
| |-->API.csproj
| |-->Some other stuff
tools
|-->Docker
| |-->Dockerfile
docker-compose.yml
#./src/Docker/Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /source
COPY *.sln ./
COPY ./src/API src/API
WORKDIR /src/API
RUN dotnet restore
RUN dotnet publish -c release -o /src/API
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /src/API
COPY --from=build /src/API ./
EXPOSE 80/tcp
ENV ASPNETCORE_URLS http://*:80
ENTRYPOINT ["dotnet", "API.dll"]
#./docker-compose.yml
version: '3'
services:
api:
build:
context: .
dockerfile: tools/Docker/Dockerfile
ports:
- "5009:5009"
environment:
ASPNETCORE_ENVIRONMENT: "prod"

the .NET 6.0 SDK image is not official yet
you can use some RC version instead
try
ARG REPO=mcr.microsoft.com/dotnet/aspnet
FROM $REPO:6.0.0-rc.1-alpine3.14-amd64
follow here for updates:
https://hub.docker.com/_/microsoft-dotnet-sdk?tab=description

Related

Unable to load the service index for source error for a CodeArtifact feed using a Dockerfile

It's my first question here so let me know if I have to change something or add more information.
We’re using AWS CodeArtifact for storing our packages and when we try to build a Docker image from our Dockerfile it fails because it's unable to load the source during the restore process.
We have a web API in .Net we want to deploy using AWS Fargate. This project runs smoothly from Visual Studio 2022 using Docker but we can’t build the image from PowerShell after adding our packages from CodeArtifact.
Our approach to include the credentials in the build is to pass the NuGet.Config stored on the host using Buildkit.
This’s our Dockerfile:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
ENV ASPNETCORE_URLS=http://+:49151
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY . .
WORKDIR /src
COPY ["Src/Presentation/Project.API/Project.API.csproj", "Src/Presentation/Project.API/"]
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
--mount=type=secret,id=nugetconfig \
dotnet restore "Src/Presentation/Project.API/Project.API.csproj" \
--configfile /run/secrets/nugetconfig
COPY . .
WORKDIR "/src/Src/Presentation/Project.API"
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
dotnet build "Project.API.csproj" -c Release -o /app/build \
--no-restore
FROM build AS publish
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
dotnet publish "Project.API.csproj" -c Release -o /app/publish \
--no-restore
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Project.API.dll"]
Our script in PowerShell:
docker buildx build --secret id=nugetconfig,src=$HOME\AppData\Roaming\NuGet\NuGet.Config -f "Src\Presentation\Project.API\Dockerfile" -t my-dotnet-image .
Output:
#14 [build 6/9] RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages --mount=type=secret,id=nugetconfig dotnet restore "Src/Presentation/Project.API/Project.API.csproj"
--configfile /run/secrets/nugetconfig
#14 1.175 Determining projects to restore...
#14 2.504 /src/Src/Presentation/Project.API/Project.API.csproj : error NU1301: Unable to load the service index for source
https://domain-123456789012.d.codeartifact.us-east-2.amazonaws.com/nuget/repository/v3/index.json.
What are we missing here? Once we have the Dockerfile working we want to use CDK for deploying the Docker image alongside our infrastructure.
We’re using aws codeartifact login command to authenticate with the service. We work on windows with a Linux container

Project not found when build docker container on Azure DevOps

I run commands to build and publish the docker image to registry but it fails with this error:
MSBUILD : error MSB1009: Project file does not exist.
Switch: ./Project.Api/Project.Api.csproj
Project structure
src
|--Project.Api
| |--Project.Api.csproj
|-- Dockerfile
tests
azure-pipelines.yml
Docker task in pipeline
stages:
...
- stage: build
pool:
vmImage: 'ubuntu-latest'
jobs:
- job: build_container
displayName: 'Build docker container image'
steps:
- bash: |
docker login -u $(REGISTRY_USER) -p $REGISTRY_PASSWORD $(REGISTRY_URL)
docker build -t $(REGISTRY_URL)/$(IMAGE_REPOSITORY):kkk . -f $(build.sourcesDirectory)/src/Dockerfile
docker push $(REGISTRY_URL)/$(IMAGE_REPOSITORY):kkk
displayName: 'Build and push docker image'
env:
REGISTRY_PASSWORD: $(REGISTRY_PASSWORD)
my Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY . /src
RUN dotnet restore ./Project.Api/Project.Api.csproj
RUN dotnet publish ./Project.Api/Project.Api.csproj --no-restore -c Release -o /app
FROM build AS publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Project.Api.dll"]
Does anyone know that is the cause of this error and how to fix it? Thank you.
Follow standard troubleshooting steps: Check what directory you're running that bash command out of. Put a RUN ls or similar command in your Dockerfile so you can check the directory structure within your container during build and confirm it's what you expect.
Your Docker context is likely set a directory level higher than you think it is.
You can set your working directory appropriately from there.
- bash: |
docker ..
workingDirectory: $(build.sourcesDirectory)/src
I'd guess that your code is ending up in the directory /src/src/ in your Dockerfile.

Docker dotnet restore: Unable to load the service index for source https://api.nuget.org/v3/index.json

Objective -> I am currently trying to integrate Dapr with a small sample project provided by the .Net project templates. At the end of my test cases, I would like to use Docker Compose to launch multiple containers for a web API and a web app to communicate with each other using the Dapr sidecars.
Problem -> Whenever I run docker compose up on the sample application, the container fails to be created and is stopped by this error:
> [build 4/7] RUN dotnet restore "MyBackEnd/MyBackEnd.csproj":
#0 0.945 Determining projects to restore...
#0 7.179 /src/MyBackEnd/MyBackEnd.csproj : error NU1301: Unable to load the service index for source https://api.nuget.org/v3/index.json.
#0 7.202 Failed to restore /src/MyBackEnd/MyBackEnd.csproj (in 6.09 sec).
Steps to Reproduce ->
The following are the steps I took with this project:
Ran mkdir sample_project && cd sample_project
Ran dotnet new webapi -o MyBackEnd followed by cd MyBackEnd
Ran touch Dockerfile && cd .. && touch docker-compose.yml
I then put this in my Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base
WORKDIR /app
RUN apt-get update && apt-get install -y libcurl4
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
COPY ["MyBackEnd/MyBackEnd.csproj", "MyBackEnd/"]
RUN dotnet restore "MyBackEnd/MyBackEnd.csproj"
COPY . .
WORKDIR "/src/MyBackEnd"
RUN dotnet build "MyBackEnd.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyBackEnd.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyBackEnd.dll"]
And this was what I did with my Docker Compose YAML file:
version: '3.4'
services:
mybackend:
image: ${DOCKER_REGISTRY-}mybackend
build:
context: .
dockerfile: MyBackEnd/Dockerfile
ports:
- "3000:80"
Finally, I ran docker compose up to arrive at the issue
Additional Information -> I am not running this on a Windows system. I am using Ubuntu 20, and thus, I do not have access to Visual Studio to handle the launching and configuration for me. I am aware that this is a heavily replicated question, but I have yet to see anything that specifically applies to users that do not have access to Visual Studio or on Ubuntu. Thanks!

host.docker.internal will not resolve in docker-compose

So I have a .NET solution with two projects. These work fine when running with dotnet run, but I'm having issues with my docker compose. When adding env variables with url paths to talk between containers, i generally use something like host.docker.internal to resolve the path to the other container, but for some reason that doesn't resolve and just gets used as, for instance, https://host.docker.internal:49833/connect/authorize instead of https://localhost:49833/connect/authorize.
This doesn't make sense to me as i literally have another project on my machine that runs with this setup just fine. what am i missing?
project 1 has a dockerfile like this:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY ["RecipeManagement/src/RecipeManagement/RecipeManagement.csproj", "./RecipeManagement/src/RecipeManagement/"]
#COPY ["SharedKernel/SharedKernel.csproj", "./SharedKernel/"]
RUN dotnet restore "./RecipeManagement/src/RecipeManagement/RecipeManagement.csproj"
# Copy everything else and build
COPY . ./
RUN dotnet build "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj" -c Release -o /app/build
FROM build-env AS publish
RUN dotnet publish "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj" -c Release -o /app/out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=publish /app/out .
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
ENTRYPOINT ["dotnet", "/app/RecipeManagement.dll"]
project 2 has a dockerfile like this:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
RUN curl -sL https://deb.nodesource.com/setup_16.x | bash -
RUN apt install -y nodejs
# Copy csproj and restore as distinct layers
COPY ["AuthServerWithDomain/AuthServerWithDomain.csproj", "./AuthServerWithDomain/"]
#COPY ["SharedKernel/SharedKernel.csproj", "./SharedKernel/"]
RUN dotnet restore "./AuthServerWithDomain/AuthServerWithDomain.csproj"
# Copy everything else and build
COPY . ./
RUN dotnet build "AuthServerWithDomain/AuthServerWithDomain.csproj" -c Release -o /app/build
FROM build-env AS publish
RUN dotnet publish "AuthServerWithDomain/AuthServerWithDomain.csproj" -c Release -o /app/out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=publish /app/out .
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
ENTRYPOINT ["dotnet", "/app/AuthServerWithDomain.dll"]
and the compose looks like this:
version: '3.7'
services:
recipemanagement-db:
image: postgres
restart: always
ports:
- '63230:5432'
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: dev_recipemanagement
volumes:
- recipemanagement-data:/var/lib/postgresql/data
recipemanagement-api:
build:
context: .
dockerfile: RecipeManagement/src/RecipeManagement/Dockerfile
ports:
- "63231:8080"
environment:
ASPNETCORE_ENVIRONMENT: "Development"
ASPNETCORE_URLS: https://+:8080;
ASPNETCORE_Kestrel__Certificates__Default__Path: "/https/aspnetappcert.pfx"
ASPNETCORE_Kestrel__Certificates__Default__Password: "password"
DB_CONNECTION_STRING: "Host=recipemanagement-db;Port=5432;Database=dev_recipemanagement;Username=postgres;Password=postgres"
"AUTH_AUDIENCE": "recipe_management"
"AUTH_AUTHORITY": "https://host.docker.internal:49833"
# ^^^^^^^^^^^ not sure why this and the below don't resolve *************************************************************************
"AUTH_AUTHORIZATION_URL": "https://host.docker.internal:49833/connect/authorize"
"AUTH_TOKEN_URL": "https://host.docker.internal:49833/connect/token"
"AUTH_CLIENT_ID": "recipe_management.swagger"
"AUTH_CLIENT_SECRET": "974d6f71-d41b-4601-9a7a-a33081f80687"
"RMQ_HOST": "localhost:TBDRMQPORT"
"RMQ_VIRTUAL_HOST": "/"
"RMQ_USERNAME": "guest"
"RMQ_PASSWORD": "guest"
volumes:
- ~/.aspnet/https:/https:ro
recipemanagement-authserver:
build:
context: .
dockerfile: AuthServerWithDomain/Dockerfile
ports:
- "49833:8080"
environment:
"ASPNETCORE_ENVIRONMENT": "Development"
ASPNETCORE_URLS: "https://+:8080;"
ASPNETCORE_Kestrel__Certificates__Default__Path: "/https/aspnetappcert.pfx"
ASPNETCORE_Kestrel__Certificates__Default__Password: "password"
volumes:
- ~/.aspnet/https:/https:ro
volumes:
recipemanagement-data:
See #DavidMaze comment. Best to search on Networking in Compose as the starting point.
(The Dockerfile's should be irrelevant to your question.)
There are a couple of options, with trial and error to get the exact behavior you want. I'd encourage you to find better explanations on the following suggestions:
In your compose file, at the 'service' level, you can add extra_hosts
my-service:
extra_hosts:
host.docker.internal:host-gateway
#host.docker.internal:127.0.0.1 for linux
But when using compose, a better option is to have docker create a network specific to your containers with
docker network create --driver bridge my_recipe_ntwk
and then at the top level of your compose:
services:
volumes:
networks:
my-private-ntwk:
external:
name: my_recipe_ntwk
and then at the 'service' level
my-service-1:
networks:
- my-private-ntwk
my-service-2:
networks:
- my-private-ntwk
For postgres running on the host, see this answer and there should be a few other similar answers.
I believe if all services are configured on the same bridge network, you actually use the service name, e.g. my-service-1:8080 and don't inclue the https. I might be wrong. (In my case, my containers connect to Postgres on the host itself, and that requries 'extra_hosts' and it's own special workaround.) You are hosting a Postgres container, so I believe your connection host and port, when using the bridge network, would simply be my-private-ntwk:5432

How to pass command line argument to docker run and print it?

I created a .Net core 2.1 console application with docker (linux) support. Here is the system generated Dockerfile.
FROM microsoft/dotnet:2.1-runtime AS base
WORKDIR /app
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY MyApp/MyApp.fsproj MyApp/
RUN dotnet restore MyApp/MyApp.fsproj
COPY . .
WORKDIR /src/MyApp
RUN dotnet build MyApp.fsproj -c Release -o /app
FROM build AS publish
RUN dotnet publish MyApp.fsproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyApp.dll"]
The console program just prints out the argument. (Console.WriteLine("The args are {0}", args);)
However, docker run MyApp:dev doesn't print anything. And docker run MyApp:dev ABC got the following error.
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"ABC\": executable file not found in $PATH": unknown.
I tried docker run -it MyApp:dev dotnet /app/MyApp.dll ABC and it got the error of
Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
Running docker run -it MyApp:dev dotnet shows the Usage of dotnet.
PS C:\Temp> docker run -it MyApp:dev dotnet
Usage: dotnet [options]
Usage: dotnet [path-to-application]
Options:
-h|--help Display help.
--info Display .NET Core information.
--list-sdks Display the installed SDKs.
--list-runtimes Display the installed runtimes.
I tried docker run -it MyApp:dev bash and found the dirctory /app is empty. find . -name MyApp.dll cannot find anything?
It looks like what you're doing should work. I just made a repo here:
https://github.com/rquackenbush/DotNetCoreArgs
After building:
docker build .
I was able to run:
docker run <imageid> ABC
And I got:
Hello World!
There are 1 args.
ABC

Categories

Resources