Reputation: 1386
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
Reputation: 15203
Why are you using different Dockerfile
s? 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:
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
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