Dotnetcore container errors on startup asking if I

2019-07-10 00:04发布

问题:

I'm creating a simple dotnetcore 2.0 application and I want to containerize it. The idea being this container will run once on deployment to do some application configuration tasks.

At present, the code just does this...

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

The docker file looks like this...

FROM microsoft/dotnet:2.0-runtime
ARG source
WORKDIR /app
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "consoleapp.dll"]

I hit F5 in Visual Studio 2017 and I can see by doing a docker image ls command that the container exists.

I then attempt to run the container as follows:

docker run consoleapp: dev

I now get the error:

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

My thinking on this is that because I am using a runtime image to base my image on, I should be able to somehow start my application. I'm guessing the error here is that dotnet which forms part of my entry point is part of the dotnet SDK and hence is not in my base image.

Should I base this container on a non-runtime version of the dotnet core?

回答1:

One reason why you can see this error message is when the argument passed to dotnet is unknown.

The runtime doesn't recognize the argument you are passing to it and wonder if you perhaps should be using the sdk instead.

Make sure the name of the dll and the path to it are correct in:

ENTRYPOINT ["dotnet", "consoleapp.dll"]

Also, make sure the COPY instruction works as expected.

In your example it's picking files from the obj/Docker/publish folder. The output ready to be executed should come out of the bin folder instead. If you are using the published version, make sure to run dotnet publish before creating the container. Or restore/build/publish when creating the image.

Remember the dll would have to end up exactly at the WORKDIR. In your case: /app Unless you provide the path to dotnet

I've created a basic working sample on Github:



回答2:

Create an app:

dotnet new console -o myApp
cd myApp/
dotnet publish -o publish -r linux-x64
# add docker file (see below)
docker build . -t myapp:latest
docker run --rm myapp:latest

Dockerfile:

FROM microsoft/dotnet:2-runtime
WORKDIR /opt/myapp
ADD publish .
ENV PATH="/opt/myapp:${PATH}"
CMD ["myApp"]