.NET Core Web应用程序无法在Docker容器中运行

时间:2018-09-30 16:12:33

标签: docker asp.net-core .net-core-2.0

我有一个在JetBrains Rider中设置的香草.NET Core 2 Web应用程序,我立即开始在Docker环境中工作。我按照本指南开始使用:

https://docs.docker.com/engine/examples/dotnetcore/

我对此做了些微改动以解决这个问题:

FROM microsoft/dotnet:latest AS packager

RUN mkdir -p /opt/build
WORKDIR /opt/build

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

# Copy everything else and build
COPY . .
RUN dotnet publish -c Release -o bin

# --

# Build runtime image
FROM microsoft/dotnet:runtime AS runtime

RUN mkdir -p /opt/app
WORKDIR /opt/app

COPY --from=packager /opt/build/bin/. .

ENTRYPOINT ["dotnet", "/opt/app/aspnetapp.dll"]

图像生成,当我运行容器时,得到以下输出:

dan@mycomputer ~/Desktop/coreapi (master)
$ docker build -t myapp .
Sending build context to Docker daemon  25.09kB
Step 1/12 : FROM microsoft/dotnet:latest AS packager
 ---> e1a56dca783e
Step 2/12 : RUN mkdir -p /opt/build
 ---> Using cache
 ---> 95f9c936d0d1
Step 3/12 : WORKDIR /opt/build
 ---> Using cache
 ---> 64f26c356fd7
Step 4/12 : COPY *.csproj .
 ---> Using cache
 ---> 38a2fb7ca6bb
Step 5/12 : RUN dotnet restore
 ---> Using cache
 ---> 70dbc44d98ae
Step 6/12 : COPY . .
 ---> Using cache
 ---> b1019d53a861
Step 7/12 : RUN dotnet publish -c Release -o bin
 ---> Using cache
 ---> 8e112606633a
Step 8/12 : FROM microsoft/dotnet:runtime AS runtime
 ---> cc240a7fd027
Step 9/12 : RUN mkdir -p /opt/app
 ---> Using cache
 ---> 954f494febc4
Step 10/12 : WORKDIR /opt/app
 ---> Using cache
 ---> b74be941e7dc
Step 11/12 : COPY --from=packager /opt/build/bin/. .
 ---> Using cache
 ---> 4c229192d99b
Step 12/12 : ENTRYPOINT ["dotnet", "/opt/app/aspnetapp.dll"]
 ---> Using cache
 ---> fb6ef4015fba
Successfully built fb6ef4015fba
Successfully tagged myapp:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

dan@mycomputer ~/Desktop/coreapi (master)
$ docker run -p 5001:5001 myapp:latest
Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
  http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409

该应用程序无法运行,并且收到一条消息,提示我需要安装SDK。那是怎么回事?运行时Docker映像不应该具备运行该应用程序所需的一切吗?

1 个答案:

答案 0 :(得分:0)

我能够通过修改ENTRYPOINT来运行tail -f /dev/null来找到解决方案。从那里,我进入了容器,看到二进制文件的名称根据您的项目名称进行了调整,而Docker文档对此并不明确。

我还更新了基本图像,这解决了我的问题。这是我下面最新的Dockerfile:

FROM microsoft/dotnet:2.1-sdk AS packager

RUN mkdir -p /opt/build
WORKDIR /opt/build

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

# Copy everything else and build
COPY . .
RUN dotnet publish -c Release -o bin

# --

# Build runtime image
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime

RUN mkdir -p /opt/app
WORKDIR /opt/app

COPY --from=packager /opt/build/bin/. .

EXPOSE 80

ENTRYPOINT ["dotnet", "/opt/app/coreapi.dll"]