【问题标题】:How to host Blazor app with IIS using Docker?如何使用 Docker 使用 IIS 托管 Blazor 应用程序?
【发布时间】:2019-12-27 14:33:10
【问题描述】:

我正在尝试创建一个包含 IIS 的容器,该 IIS 用于托管我的 blazor 应用程序。

我使用静态 HTML 文件运行它,但是当我尝试托管 Blazor 应用程序时,我在浏览器中收到错误:

You do not have permission to view this directory or page.

或

The page cannot be displayed because an internal server error has occurred.

当我重新启动容器并查看日志时,我得到以下信息:

Service 'w3svc' has been stopped

APPCMD failed with error code 4312

Failed to update IIS configuration


当我使用一种解决方法来创建自己的入口点并启动w3svc 时,我陷入了无限循环。 我当前的 dockerfile

FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-20191210-windowsservercore-ltsc2019

COPY ./dotnet/dotnet-hosting-3.1.0-win.exe .
RUN powershell ./dotnet-hosting-3.1.0-win.exe /quiet /install

RUN powershell -NoProfile -Command Remove-Item -Recurse C:\inetpub\wwwroot\*
WORKDIR /inetpub/wwwroot
COPY ./output . # Contains compiled blazor hello world project

COPY ./config.ps1 .
RUN powershell ./config.ps1 # Creates new app and pool

COPY ./ContainerRunTime.ps1 .
ENTRYPOINT ./ContainerRunTime.ps1 # C:\\ServiceMonitor.exe w3svc

我的操作系统是 WinServer 2019

【问题讨论】:

标签: powershell docker iis


【解决方案1】:

我终于使它与以下 Dockerfile 一起工作。

将此 Dockerfile 复制到项目的根目录中,重要的是项目有自己的 projectforlder。

# Use iis as base image and install dotnet 3.0.1 hosting pack
FROM microsoft/iis as base
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'Continue'; $verbosePreference='Continue';"]

ADD https://download.visualstudio.microsoft.com/download/pr/32b71802-0b4d-4064-a7e6-083b5155d3b1/080cf60a5c06be4ed27e2eac6c693f2f/dotnet-hosting-3.0.1-win.exe "C:/setup/dotnet-hosting-3.0.1-win.exe"
RUN start-process -Filepath "C:/setup/dotnet-hosting-3.0.1-win.exe" -ArgumentList @('/install', '/quiet', '/norestart') -Wait
RUN Remove-Item -Force "C:/setup/dotnet-hosting-3.0.1-win.exe"

##############################################################################################################

# Use dotnet core sdk as build image 
FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.sln ./
COPY ./{PROJECT_NAME}/*.csproj ./{PROJECT_NAME}/
RUN dotnet restore

# Copy everything else and build app
COPY ./{PROJECT_NAME}/. ./{PROJECT_NAME}/
RUN dotnet publish -c Release -o out

##############################################################################################################

# Create final image
FROM base AS final
WORKDIR /inetpub/wwwroot/{PROJECT_NAME}

RUN Import-Module WebAdministration
RUN Remove-Website -Name 'Default Web Site'

# Create pool
RUN New-WebAppPool -Name '{PROJECT_NAME}-pool'; \
    Set-ItemProperty IIS:\AppPools\{PROJECT_NAME}-pool -Name managedRuntimeVersion -Value ''; \
    Set-ItemProperty IIS:\AppPools\{PROJECT_NAME}-pool -Name enable32BitAppOnWin64 -Value 0; \
    Set-ItemProperty IIS:\AppPools\{PROJECT_NAME}-pool -Name processModel.identityType -Value Service
# Create new Website
RUN New-Website -Name '{PROJECT_NAME}' \
    -Port 80 -PhysicalPath 'C:\inetpub\wwwroot\{PROJECT_NAME}' \
    -ApplicationPool '{PROJECT_NAME}-pool' -force

# Copy output from build img
COPY --from=build /app/out .

EXPOSE 80

现在将所有{PROJECT_NAME} 替换为您的项目名称并创建并启动容器:

docker build -t blazor-iis .
docker run -d -p 8080:80 --name blazor-iis-container blazor-iis

现在您可以在 chrome 中打开您的应用了

start chrome $(docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" "blazor-iis-container")

【讨论】:

    猜你喜欢
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多