Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker build for C# core need the internet connection

I am writing in C# .NET core (VS 2017), using the build for linux containers, using docker-compose.

When I build the image (or publish), there is always reference to the internet, because of nuget usage.

Last error encountered (which I persume of downtime on the nuget, but no matter - any downtime should not lead to exception in build/publish).

/usr/share/dotnet/sdk/2.1.503/NuGet.targets(114,5): error : Unable to load the service index for source https://api.nuget.org/v3/index.json. ...

I want to build the image, even the nuget is down, or even there is no interet connection

The yml file look like this:

services:

 myProj:
 image: my_proj
 build:
  context: ./all_projects/base_solution/
  dockerfile: myProj/Dockerfile 

It seems that "dotnet publish..." command call restore from the internet.

If I run publish with --no-restore, the code not compiled, but I want to restore the nuget packages from my own pre-build of my computer.

How can I do it? With no internet connection? Why should I depend on the internet to restore the nuget at each build?

Why cannot I restore nuget package from my own pre-build image, and not always?! get from nuget (actually nuget packages not change occasionally in my code).

May I just copy the folder from the nuget, and not using "COPY" command?

I did the following:

Build an image from a 'common' project, that uses all the nuget package.

Add a reference in the built-image for the new other image, like this:

Create a new common image: # there is no runtime.

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY TestDock/TestDock.csproj TestDock/

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

And at the original image: FROM microsoft/dotnet:2.1-runtime AS base WORKDIR /app

# FROM microsoft/dotnet:2.1-sdk AS build do:
FROM my_common_image AS build
WORKDIR /src
COPY TestDock/TestDock.csproj TestDock/

# Added the following. I tried to copy all, but this doesn't help.
# I persume I can copy part of the common build.
COPY --from=build /app /app
COPY --from=build /src /src
COPY --from=build /usr /usr

FROM build AS publish
RUN dotnet publish TestDock.csproj --no-restore --no-dependecies -c Release -o /app

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

I have tried to add an intermediate image, with Dockerfile as following:

# This image is base image for all dockers (no need runtime)
#    docker build . -f DockerFile_Common -t docker_common 


FROM microsoft/dotnet:2.1-sdk AS build

COPY . .

WORKDIR /src/myProj

FROM build AS publish

RUN dotnet publish myProj.csproj -c Release -o /app
RUN dotnet pack /src/myProj.csproj -c Release -o /app

and use it in my image (that I don't want to build using the internet), instead of:

FROM microsoft/dotnet:2.1-sdk AS build

I did:

FROM docker_common AS build

I also tried to add the line: "RUN dotnet publish ..." when setting the workdir as the solution folder, and mark the "Microsoft Visual Studio Offline Packages" (unmark default "nuget.org").

Still, the above doesn't build correctly.

like image 803
Eitan Avatar asked Feb 07 '19 12:02

Eitan


People also ask

What is a docker build?

The docker build command builds Docker images from a Dockerfile and a “context”. A build's context is the set of files located in the specified PATH or URL . The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.

Is docker written in C?

Docker is written in the Go programming language and takes advantage of several features of the Linux kernel to deliver its functionality. Docker uses a technology called namespaces to provide the isolated workspace called the container.

Is docker a compiler?

Docker is a compiler that deals with higher-level abstractions. That makes it an extremely powerful tool. First, a refresher on compilers.

What is GCC docker?

The GNU Compiler Collection (GCC) is a compiler system produced by the GNU Project that supports various programming languages. GCC is a key component of the GNU toolchain. The Free Software Foundation (FSF) distributes GCC under the GNU General Public License (GNU GPL).


1 Answers

For not using the internet, you can follow the steps.

What is needed is to publish in visual studio, as following:

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-2.2

(Alternative publish in command line).

dotnet publish myProj.csproj -c Release /p:PublishProfile=Properties\PublishProfiles\ 
   <myprofile>.pubxml /p:PublishDir=<proj_folder>\bin\Release\netcoreapp2.1\publish

or put in the Post-build event a line like:

dotnet publish $(ProjectDir)$(ProjectName).csproj -c $(Configuration) 
/p:PublishProfile=Properties\PublishProfiles\PublishProfile.pubxml 
/p:PublishDir=$(TargetDir)publish --no-build

... and in the Dockerfile I just did with need to do "COPY ..." from the relevant publish directory!

FROM microsoft/dotnet:2.1-runtime

COPY MyProjectFolder/bin/Release/netcoreapp2.1/publish /app
WORKDIR /app

ENTRYPOINT ["dotnet", "myproject.dll"]
like image 108
Eitan Avatar answered Sep 19 '22 06:09

Eitan