Eitan
Eitan

Reputation: 1386

.NET Core for Linux with dockers - create docker image

I want to create a docker image for code I have written in .NET Core (C# Visual Studio 2017), that can be run on Linux.

The steps - I create a new file, such as hello world:

using System;

namespace myApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

The docker file (for Windows), which had been tested and works:

FROM microsoft/dotnet:2.1-sdk
WORKDIR /app

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

# copy and build everything else
COPY . ./
RUN dotnet publish -c Release -o out
ENTRYPOINT ["dotnet", "out/myApp.dll"]

The docker file for linux:

FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY ./publish .
ENTRYPOINT ["dotnet", "Receive.dll"]

The above does not work well.

When I run (in command prompt. Linux container):

docker build . -t myapp_linux

I got the message:

COPY failed: stat /var/lib/docker/tmp/docker-builder786597608/publish: no such file or directory

Also, tried changing to configuration to something like:

FROM microsoft/aspnetcore:2.0
WORKDIR /app

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

# copy and build everything else
COPY . ./
RUN dotnet publish -c Release -o out
ENTRYPOINT ["dotnet", "Receive.dll"]

The above doesn't work either, and I got the message:

Did you mean to run dotnet SDK commands? Please install dotnet SDK from: http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409

Upvotes: 4

Views: 3018

Answers (2)

omajid
omajid

Reputation: 15203

Why are you using different Dockerfiles? The whole point of containers is to get a consistent environment. Use the one you are using on Windows (which you know works), everywhere:

FROM microsoft/dotnet:2.1-sdk
WORKDIR /app

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

# copy and build everything else
COPY . ./
RUN dotnet publish -c Release -o out
ENTRYPOINT ["dotnet", "out/myApp.dll"]

There's a number of advantages to doing this:

  1. You are building the code same way on Windows and Linux. No more operating system differences, like the ones you are running into.
  2. If you care about reproducible deployment via containers, don't you want to build in a reproducible environment too? Otherwise, as you are experiencing, what is installed on your OS affects how your code is compiled and executed.
  3. No more worrying about keeping Dockerfile and other versions in sync. You are using 2.1 on Windows but 2.0 on Linux. That's probably going to cause some build or runtime issues. Not to mention 2.0 is actually out of support and wont get security fixes.

Upvotes: 2

Aman B
Aman B

Reputation: 2388

You should run the restore and publish command outside of docker file e.g. PowerShell first and then just copy the output to docker, in docker file.

1- First run in cmd or powershell:

dotnet restore
dotnet publish -o ./publish

2- In your docker file:

FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY ./publish .
ENTRYPOINT ["dotnet", "Receive.dll"]

3- Build docker image

4- Run docker container

Upvotes: 3

Related Questions