dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.5k stars 10.04k forks source link

Dockerized ASP.NET Core Blazor .NET 9 RC 1 static assets issue #57898

Closed ysmoradi closed 1 month ago

ysmoradi commented 2 months ago

Is there an existing issue for this?

Describe the bug

Newly created blazor web project: 1- Runs correctly using Visual Studio F5 | Ctrl + F5 2- Runs correctly using dotnet watch / run commands 3- Runs correctly using Visual Studio + WSL 4- Runs correctly using vscode Open in container 5- DOES NOT run correctly using Visual Studio + Linux based Docker OR Linux Docker in production

Based on items 3 and 4, there seems to be no issue with Linux itself, and this only happens when you run the dockerized app.

It seems that there's something wrong with app.MapStaticAssets() method call.

image

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

9.0.100-rc.1.24452.12

Anything else?

No response

javiercn commented 2 months ago

@ysmoradi thanks for contacting us.

This is a known issue that was addressed in RC2. If you want to validate or get unblocked, you can adapt the following dockerfile

ARG IMAGE_VERSION=9.0-preview
FROM mcr.microsoft.com/dotnet/sdk:${IMAGE_VERSION} AS install-nightly
ARG DOTNET_CHANNEL=9.0.1.xx
ARG DOTNET_NIGHTLY_VERSION=9.0.100-rc.2.24463.6
ARG DOTNET_INSTALL_DIR=/.dotnet
RUN mkdir -p $DOTNET_INSTALL_DIR
RUN command curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --channel $DOTNET_CHANNEL --install-dir $DOTNET_INSTALL_DIR ${DOTNET_NIGHTLY_VERSION:+--version $DOTNET_NIGHTLY_VERSION}
RUN mkdir -p /src
WORKDIR /src

FROM install-nightly AS workspace
ENV DOTNET_ROOT=$DOTNET_INSTALL_DIR
ENV PATH=${DOTNET_INSTALL_DIR}:$PATH

FROM workspace AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
RUN dotnet new nugetconfig
RUN dotnet nuget add source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet9/nuget/v3/index.json -n dotnet9 --configfile /src/nuget.config
RUN cat /src/nuget.config
COPY ["BlazorTest/BlazorTest.csproj", "BlazorTest/"]
RUN dotnet restore "./BlazorTest/BlazorTest.csproj"
COPY . .
WORKDIR "/src/BlazorTest"
RUN dotnet build "./BlazorTest.csproj" -c $BUILD_CONFIGURATION -o /app/build

# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./BlazorTest.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM workspace AS final
WORKDIR /src
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BlazorTest.dll"]
neozhu commented 1 month ago

thank you @javiercn share my Dockerfile for Blazor WebAssembly project in .net 9 rc2 image

# Stage 1: Build the Blazor WebAssembly project
ARG IMAGE_VERSION=9.0-preview
FROM mcr.microsoft.com/dotnet/sdk:${IMAGE_VERSION} AS build
ARG DOTNET_CHANNEL=9.0.1.xx
ARG DOTNET_NIGHTLY_VERSION=9.0.100-rc.2.24463.6
ARG DOTNET_INSTALL_DIR=/.dotnet
ARG BUILD_CONFIGURATION=Release
WORKDIR /src

# Copy project files
COPY ["src/BlazorPocket.WebAssembly/BlazorPocket.WebAssembly.csproj", "src/BlazorPocket.WebAssembly/"]
COPY ["src/BlazorPocket.Shared/BlazorPocket.Shared.csproj", "src/BlazorPocket.Shared/"]
COPY ["src/PocketBaseClient.BlazorPocket/PocketBaseClient.BlazorPocket.csproj", "src/PocketBaseClient.BlazorPocket/"]
COPY ["pbcodegen/src/PocketBaseClient/PocketBaseClient.csproj", "pbcodegen/src/PocketBaseClient/"]
COPY ["pbcodegen/src/PocketBaseClient.CodeGenerator/PocketBaseClient.CodeGenerator.csproj", "pbcodegen/src/PocketBaseClient.CodeGenerator/"]
COPY ["pbcodegen/sdk/pocketbase-csharp-sdk/pocketbase-csharp-sdk.csproj", "pbcodegen/sdk/pocketbase-csharp-sdk/"]

# Restore dependencies
RUN dotnet restore "src/BlazorPocket.WebAssembly/BlazorPocket.WebAssembly.csproj"

# Copy the rest of the files
COPY . .

# Build the project
WORKDIR "/src/src/BlazorPocket.WebAssembly"
RUN dotnet build "BlazorPocket.WebAssembly.csproj" -c $BUILD_CONFIGURATION -o /app/build

# Publish the project
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "BlazorPocket.WebAssembly.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# Stage 2: Setup Apache HTTP Server with HTTPS
FROM httpd:2.4-alpine AS final

# Install openssl and enable mod_ssl
RUN apk add --no-cache openssl && \
    sed -i '/^#LoadModule ssl_module/s/^#//' /usr/local/apache2/conf/httpd.conf && \
    sed -i '/^#LoadModule socache_shmcb_module/s/^#//' /usr/local/apache2/conf/httpd.conf

# Generate a self-signed certificate
RUN mkdir -p /usr/local/apache2/conf/ssl && \
    openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes \
    -keyout /usr/local/apache2/conf/ssl/private.key -out /usr/local/apache2/conf/ssl/certificate.crt \
    -subj "/C=US/ST=State/L=City/O=Organization/CN=localhost"

# Copy the published files from the build stage
COPY --from=build /app/publish/wwwroot /usr/local/apache2/htdocs/

# Copy custom httpd configuration to enable SSL
COPY httpd-ssl.conf /usr/local/apache2/conf/extra/httpd-ssl.conf
RUN echo "Include conf/extra/httpd-ssl.conf" >> /usr/local/apache2/conf/httpd.conf

# Configure Apache to redirect 404 errors to the root
RUN echo "ErrorDocument 404 /index.html" >> /usr/local/apache2/conf/httpd.conf

EXPOSE 80
EXPOSE 443

CMD ["httpd-foreground"]
neozhu commented 1 month ago

image

dotnet-policy-service[bot] commented 1 month ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.