gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:02:19 25 4
gpt4 key购买 nike

我有一个在JetBrains Rider中设置的 Vanilla .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镜像不应该具备运行该应用程序所需的一切吗?

最佳答案

我可以通过修改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"]

关于docker - .NET Core Web应用程序无法在Docker容器中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52579719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com