Docker build fails when project reference and nuget feed added - c#

I'm new to docker and I'm trying to create docker image/container in my local machine. It is working fine if I've a simple project without any project reference and private nuget feed. I'm getting the below errors when I do docker build (from src\Account\Account.host folder) after adding a project reference and private nuget feed.
Skipping project "/Shared/SharedComponent/SharedComponent.csproj" because it was not found.
error NU1101: Unable to find package . No packages exist with this id in source(s): nuget.org
I tried to modify it but getting copying files out of context errors. My questions are:
- is it possible to run docker build with the below folder structure?
- do i need to use docker compose if i want to stick with the same folder structure?
- I've got other project where i need to add another docker file but I'm just testing with one project which doesnt seems to be working.
Project folder structure
Shared
++ SharedComponent
+++ ShareComponent.csproj
+++ Other files
Account
++Account.Host
+++ Account.Host.csproj (SharedComponent.csproj reference added)
+++ Dockerfile
+++ Other files
Dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["Account.Host.csproj", "./"]
RUN dotnet restore "./Account.Host.csproj"
COPY . .
RUN dotnet build "Account.Host.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Account.Host.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Account.Host.dll"]

Docker build needs to be able to see the whole project when it builds, so it can determine if any of the files have changed (among other things).
You probably need to run docker build from the root directory of the project, and tell it where the Dockerfile is, like this:
docker build -t the_name_of_the_image -f Account/Account.Host/Dockerfile
The above might not be quite right, but the directory structure you posted didn't render right :(

Related

Docker build not finding nuget package from local directory

I have a nuget package stored on my local filesystem at C:\Temp. When I add the nuget package to my Visual Studio Solution everything builds as expected. However when I run my docker build command, I receive this error:
MyProject depends on NugetExample.DLL (>= 0.0.4) but NugetExample.DLL 0.0.4 was not found.
Heres a copy of my Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:6.0 as build
WORKDIR /build
EXPOSE 80
EXPOSE 443
EXPOSE 3000
COPY . .
# Restore/build...
# Use debug publish when you need to debug the service running in docker
RUN dotnet publish src/app/MyProject.csproj -c Debug -o /app
# Stage 2
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build /app .
RUN sed -i -e "s|^MinProtocol = .*|MinProtocol = TLSv1.0|g" "/etc/ssl/openssl.cnf"
ENTRYPOINT ["dotnet", "MyProject.dll"]
Is there a way to get docker to find my nuget package stored on my local filesystem?
To pull a NuGet from your local filesystem it needs to be in your Docker build context: https://docs.docker.com/build/building/context/
Alternatively, you can use the new dotnet publish support for making a container image (https://learn.microsoft.com/en-us/dotnet/core/docker/publish-as-container) which does a local build and then copies it into a container image for you. One caveat for your use, to get maintain the effect of:
RUN sed -i -e "s|^MinProtocol = .*|MinProtocol = TLSv1.0|g" "/etc/ssl/openssl.cnf"
You would need to create a base image based on your current stage 2 and then configure publish to use it with ContainerBaseImage

Getting the error "program does not contain a static 'main' method suitable for an entry point" while trying to create docker on my ASP API [duplicate]

I'm running into an issue using Docker and couldn't find a proper solution.
I'm trying to build a Docker image using .NET SDK 2.1.
The thing is that when Docker tries to run the build statement, it fails and the error output is
CSC : error CS5001: Program does not contain a static 'Main' method
suitable for an entry point
The funny thing is that if I perform the build statement on command line locally, it works fine.
I have already checked my LanguageVersion tag on the project and it is 7.3.
Here is my Docker file
FROM microsoft/dotnet:2.1-sdk AS builder
WORKDIR /src
COPY ./nuget ./nuget
COPY ./NuGet.Config ./
COPY Services/AadTracking ./
# Copy all the referenced projects
COPY ./Services/AadTracking/Company/Company.Service.AadTracking/Company.Service.AadTracking.csproj ./AadTracking/Company/Company.Service.AadTracking/Company.Service.AadTracking.csproj
COPY ./Services/AadTracking/Office.Re.Service.AadTracking/Office.Re.Service.AadTracking.csproj ./AadTracking/Office.Re.Service.AadTracking/Office.Re.Service.AadTracking.csproj
COPY ./Services/AadTracking/Company/Office.Re.Service.AadTracking.Company/Office.Re.Service.AadTracking.Company.csproj ./AadTracking/Company/Office.Re.Service.AadTracking.Company/Office.Re.Service.AadTracking.Company.csproj
COPY ./Services/AadTracking/Office.Re.Service.AadTracking.EventStore/Office.Re.Service.AadTracking.EventStore.csproj ./AadTracking/Office.Re.Service.AadTracking.EventStore/Office.Re.Service.AadTracking.EventStore.csproj
# Restore packages
RUN dotnet restore "./AadTracking/Company/Company.Service.AadTracking/Company.Service.AadTracking.csproj"
RUN dotnet build -c Debug --no-restore "./AadTracking/Company/Company.Service.AadTracking/Company.Service.AadTracking.csproj"
# COPY source code
#aad tracking
COPY ./Services/AadTracking/Company/Company.Service.AadTracking ./AadTracking/Company/Company.Service.AadTracking/
COPY ./Services/AadTracking/Office.Re.Service.AadTracking ./AadTracking/Office.Re.Service.AadTracking/
COPY ./Services/AadTracking/Company/Office.Re.Service.AadTracking.Company ./AadTracking/Company/Office.Re.Service.AadTracking.Company/
COPY ./Services/AadTracking/Office.Re.Service.AadTracking.EventStore ./AadTracking/Office.Re.Service.AadTracking.EventStore/
# Publish
RUN dotnet publish "./AadTracking/Company/Company.Service.AadTracking/Company.Service.AadTracking.csproj" -c Debug -o "../../dist"
# #Build the app image
FROM microsoft/dotnet:2.1-aspnetcore-runtime
WORKDIR /app
ENV ASPNETCORE_ENVIRONMENT Switch
ENV REINSURANCE_INSTANCE Docker-dev
COPY --from=builder /dist .
ENTRYPOINT ["dotnet", "Company.Service.AadTracking.dll"]
Thanks for your help!
I know this is little bit late to answer. Still VS 2019 has the same issue with .NET Core 3.1. I took a peek at the examples provided by Microsoft. Turns out the Docker file resided in a different place in the solution and Docker copy command wasn't working properly.
You have to move your docker file one directory up, so that they are at the same level as the sln file. It will fix the issue.
OR else you can change the paths like below sample docker file WITHOUT changing the docker file location, IMHO it is better to keep the docker file with other files.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim-arm64v8 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["WhatzThat.Web.csproj", "WhatzThat.Web/"]
RUN dotnet restore "WhatzThat.Web/WhatzThat.Web.csproj" -r linux-arm64
WORKDIR "/src/WhatzThat.Web"
COPY . .
RUN dotnet build "WhatzThat.Web.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WhatzThat.Web.csproj" -c Release -o /app/publish -r linux-arm64 --self-contained false --no-restore
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WhatzThat.Web.dll"]
I had the same issue.
I've realized that I had my Docker file at the same level of my .csproj file. I've moved my Docker file one level up in my folder structure and it's building fine.
Compiling and publishing an application inside docker will need entire application to be copied inside docker.
which means you need to copy all class files(.cs) along with required supporting files(maybe resx or config files) inside docker.
please find below reference for same
https://github.com/aspnet/aspnet-docker/issues/401
Using a multi-project solution structure, I fixed by adding "src" again into the directory to build.
Something like this:
COPY ["src/Todo.Core/Todo.Core.csproj", "Todo.Core/"]
RUN dotnet restore "Todo.Api/Todo.Api.csproj"
COPY . .
WORKDIR "/src/Todo.Api/"
RUN dotnet build "Todo.Api.csproj" -c Release -o /app/build
Turned into this:
COPY ["src/Todo.Core/Todo.Core.csproj", "Todo.Core/"]
RUN dotnet restore "Todo.Api/Todo.Api.csproj"
COPY . .
WORKDIR "/src/src/Todo.Api/"
RUN dotnet build "Todo.Api.csproj" -c Release -o /app/build
2023 Update
Unfortunately, there is an inconsistency in what Microsoft includes in Dockerfile and in which directory you run it.
There are three solutions to get this work.
First Solution
Put your auto-generated Dockerfile one level up, alongside the .sln file.
From the root directory of the solution, run the below command
docker build -t imagename .
Second Solution
Leave the Dockerfile as it is, inside your project folder.
From the root directory of the solution, run the below command
docker build -t imagename -f .\SampleProject\Dockerfile .
Third Solution
Make small changes to the Dockerfile. Now you have to run docker commands not from the root directory of the solution, but from your project folder. (one level down from .sln file).
Change from below
to
Supplementary
My DockerFile is at root level not upper and I changed the DockerFile to:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["projectName.csproj", "projectName/"]
RUN dotnet restore "projectName/projectName.csproj"
WORKDIR "/src/projectName"
COPY . .
RUN dotnet build "projectName.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "projectName.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "projectName.dll"]
For me the COPY.. was after the workdir before the dotnet build command. I just put it after the workdir command. Then it was working.
WORKDIR "/src/WeatherAPI"
COPY . .
RUN dotnet build "WeatherAPI.csproj" -c Release -o /app/build
I have a faux pas reason.
My docker file.......I was correctly copying the directory structure and the .sln and .csproj files...........
But I had a syntax error copying the SOURCE files. (.cs files, etc, etc)
#doh!
If you look at the question above, basically I had a syntax-error/bug in the steps right below where the OP has this:
# COPY source code
More importantly, how did I figure this out ??? Here ya go:
docker images
and you do not want to drill into the last one (which is your failing image), but the NEXT TO LAST image. (remembering that docker keeps making new images for the steps of the docker file)
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mything1/mything2 latest aaaaaaaaaaaa 27 minutes ago 271MB
<none> <none> bbbbbbbbbbbb 27 minutes ago 1.18GB
and then drill into (the next to last one)
docker run --rm -it bbbbbbbbbbbb sh
when you get in there, start using "ls" and "cd" commands.
I found out I didn't have my .cs source files in the right place. A few fixes addressing relative-path issues (in my specific case) later......I had my source files in the right place(s). #yay
This is a great in-general tip for when trying to debug a failing non running image.
Answer by marvelTracker worked for me but busted using the built in Docker tools in Visual Studio 2022.
Using the command docker build -f Dockerfile .. while in the project folder builds the dockerfile from the perspective of the parent folder.
Microsoft Doc that explains building Docker in VS2022: https://learn.microsoft.com/en-us/visualstudio/containers/container-build?view=vs-2022
NOTE: I found this troubleshooting the same error for a .NET 6 Docker project.
Hopefully this helps someone else.
This error means that the file containing the Main method is NOT included so you either forgot to copy it over to the proper directory or docker is point to the incorrect directory.
From the folder having Dockerfile at present,
execute
mv Dockerfile ../
then invoke
docker build
I got same issue and manage to identify the root cause.
The issue occurred because I run the application on my windows machine before I build the docker Linux image.
Because I run it on my local windows it generate obj folder on the source code, the obj folder got copied to Docker container which contain windows specific assembly version IMHO.
To fix the issue I simply delete the obj folder from my project and rebuild the Docker.
Lesson learned, do build your Docker images on a freshly cloned repository.

How to create docker image for dotnet app?

I have a dotnet project that work when i do dotnet run, i am trying to containerize that dotnet project.
For that i have create the Dockerfile as below:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
COPY bin/Release/netcoreapp3.1/publish/ App/
WORKDIR /App
EXPOSE 5000
CMD ["dotnet", "MediatorAgent.dll"]
Before creating the docker image i did run dotnet publish -c Release. Now when i try to run this docker image, i am getting the below error
Unhandled exception. System.DllNotFoundException: Unable to load shared library 'indy' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libindy: cannot open shared object file: No such file or directory
I am following the instruction for Containerize a .NET Core app for creating docker image.
How to create docker image for dotnet app?
Well, you did it.
What is likely to have gone wrong is that the DLL refered to as indy is not copied to the App folder.
Since you are copying the data, please verify it's included in the original build at bin/Release/netcoreapp3.1/publish
make sure bin/Release/ directory available in the same directory where your Dockerfile exist.
You can specify the project .csproj or .sln to build.
You can have a look on below dockerfile, hope that will help you.
FROM microsoft/aspnetcore-build AS builder
WORKDIR /source
COPY projectname.csproj .
RUN dotnet restore
RUN dotnet build projectname.csproj -c Release -o /app/build
COPY . .
RUN dotnet publish -c Release -o /app/publish
FROM microsoft/aspnetcore
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "projectname.dll"]

docker-compose build copy failed

I'm trying to upload my project to a Digital Ocean droplet using docker i'm stuck in this stage, where COPY fails whenever i run docker-compose build.
Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["iCoose.API/iChoose.API.csproj", "iCoose.API/"]
COPY ["iChoose.Business.Entities/iChoose.Business.Entities.csproj", "iChoose.Business.Entities/"]
COPY ["iChoose.Business.Services/iChoose.Business.Services.csproj", "iChoose.Business.Services/"]
COPY ["iChoose.Common.Core/iChoose.Common.Core.csproj", "iChoose.Common.Core/"]
COPY ["iChoose.DataAccess.Data/iChoose.DataAccess.Data.csproj", "iChoose.DataAccess.Data/"]
RUN dotnet restore "iCoose.API/iChoose.API.csproj"
COPY . .
WORKDIR "/src/iCoose.API"
RUN dotnet build "iChoose.API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "iChoose.API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "iChoose.API.dll"]
docker-compose.yml:
#docker-compose
version: '2'
services:
app:
build:
context: ./
dockerfile: Dockerfile
https-portal:
image: steveltn/https-portal:1
ports:
- '80:80'
- '443:443'
links:
- app
restart: always
environment:
DOMAINS: 'domain.com -> http://app:5000'
STAGE: 'production'
When I run the 'docker-compose build' command in the servers terminal it starts building until it gets to the first COPY command, where I get:
ERROR: Service 'app' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder987069796/iCoose.API/iChoose.API.csproj: no such file or directory.
I checked the files and there is no directory in that path after the 'tmp' folder, which is empty.
The Dockerfile and docker-compose.yaml files should be right next to sln and csproj file.
When you create a new project in Visual Studio and leave put project file inside the project folder this issue might occure.
Make sure you have sln and csproj in the same folder.
Also, the Dockerfile and 'docker-compose.yaml` in the same folder too.
Deeper answer:
Dockerfile contains instructions for the docker engine on how to create a docker image for your application.
Each line is a layer of docker image.
FROM
COPY
RUN
CMD
All these commands will create a new docker image, which are cache by the engine for next run.
Because these images are Linux based and linux uses cgroup and namespaces to create isolated environment which is the feature on which docker is built. This isolated environment has a special folder which is appearing in the error message.

How to build a Docker image with multiple dependencies for .NET Solution?

It could be kind of long, but I think it's easy to read... So let's begin ...
I have an IoT-Edge Solution created with IoT Edge Tools.
It generates a Dockerfile which works nice for a project without external DLLs as dependencies.
The problem comes when I add multiple DLLs(MySqlAdapter, BaseAdapter, Helper) in this module (MyEdgeModule), without changing the Dockerfile when trying to build an image,
It throws an error:
Skipping project "/MySqlAdapter/MySqlAdapter.csproj" because it was not found.
Skipping project "/MySqlAdapter/MySqlAdapter.csproj" because it was not found.
...
... : warning : The referenced project '../MySqlAdapter/MySqlAdapter.csproj' does not exist. [/app/MyEdgeModule.csproj]
and here is the generated Dockerfile for the MyEdgeModule
FROM microsoft/dotnet:2.1-sdk AS build-env
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
FROM microsoft/dotnet:2.1-runtime-stretch-slim
WORKDIR /app
COPY --from=build-env /app/out ./
RUN useradd -ms /bin/bash moduleuser
USER moduleuser
ENTRYPOINT ["dotnet", "MyEdgeModule.dll"]
These class library projects are located at "../{projname}/*.csproj" from the Dockerfile.
So I tried to relatively add these projects individually to the Dockerfile just to see if's gonna work but with no success.
I even got on this from the documentation that it's said:
The path must be inside the context of the build; you cannot COPY ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.
As far as I understand my context is the folder from which I try to go back, to find my Dependency Projects which seems impossible.
How can I create an image for this project, assuming that it has dozens of external DLLs as dependencies ?
A Thing that works for now is creating a fresh .NET Core project and using the Add Docker Support, each time when you generate it, it will refresh it Dockerfile with all of the dependecies from the References.
In order to work Dockerfile needs to be moved to the solution directory, instead in the project folder where it's generated.
Auto generated Dockerfile doesn't work at my situation.
COPY failed: stat /var/lib/docker/tmp/docker-builder471019165/src/server/MyWebService/MyWebService.csproj: no such file or directory

Categories