我正在学习
docker
并且我正在尝试将 blazor wasm
应用程序发布到 docker
容器中。
这是我的
docker-compose.yml
:
version: '3.7'
services:
reverseproxy:
build:
context: .
dockerfile: nginx/nginx.Dockerfile
ports:
- "44395:44395"
networks:
testnet
test.ui:
build:
context: .
dockerfile: Web/Dockerfile
depends_on:
- reverseproxy
environment:
- ASPNETCORE_URLS=http://*:5005
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "5005:5005"
networks:
- testnet
networks:
testnet: {}
nginx docker 文件:
FROM nginx
COPY nginx/nginx.local.conf /etc/nginx/nginx.conf
Blazor wasm Docker 文件:
FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS publish
RUN apk add nodejs
RUN apk add npm
WORKDIR /src
COPY . .
WORKDIR "/src/Web"
RUN npm install
RUN dotnet publish "Web.csproj" -c Release -o /output
FROM nginx:alpine
WORKDIR /usr/share/nginx/html
COPY --from=publish /output/wwwroot .
到目前为止文件没有正确复制。
更新一:
Blazor wasm Docker 文件:
FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build-env
RUN apk add nodejs
RUN apk add npm
WORKDIR /app
COPY . ./ <--- Change here
WORKDIR "/app/Web"
RUN npm install
RUN dotnet publish -c Release -o output
FROM nginx:alpine
WORKDIR /usr/share/nginx/html
COPY --from=build-env /app/Web/output/wwwroot/ . <--- Change here
现在当我单独运行这个docker文件时,文件夹
/app/Web/output/wwwroot/
中的内容被复制到/usr/share/nginx/html
一切都可以正常编译和构建。但是当我从 docker compose 运行时,出现以下错误并且
reverse-proxy
仍然继续运行。
所以我决定进入 test.ui 容器,我看到了
No such file or directory
C:\Users\xxxx>docker exec -it 18aa171dae9d sh
/app/Web # cd ..
/app # cd ..
/ # cd usr
/usr # cd share
/usr/share # cd nginx
sh: cd: can't cd to nginx: No such file or directory
/usr/share #
当我在
reverseproxy
评论docker-compose.yml
时,我可以看到文件被正确复制。但是在reverseproxy
中启用docker-compose.yml
,目录丢失了。我不确定是什么覆盖了我的 test.ui 容器的内容。
这里是构建日志:
8>#31 [testui:dev build-env 5/5] RUN dotnet publish -c Release -o output
8>#31 0.788 MSBuild version 17.5.0+6f08c67f3 for .NET
8>#31 2.253 Determining projects to restore...
8>#31 7.795 Restored /app/Shared/Shared.csproj (in 4.75 sec).
8>#31 14.82 Restored /app/Web/Web.csproj (in 11.8 sec).
8>#31 18.41 Shared -> /app/Shared/bin/Release/net7.0/Shared.dll
...
8>#31 35.82 Web -> /app/Web/bin/Release/net7.0/Web.dll
8>#31 35.82 Web (Blazor output) -> /app/Web/bin/Release/net7.0/wwwroot
8>#31 36.26 Optimizing assemblies for size may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink
8>#31 36.26 Optimizing assemblies for size. This process might take a while.
8>#31 97.61 Compressing Blazor WebAssembly publish artifacts. This may take a while...
8>#31 133.3 Web -> /app/Web/app/Web/output/
8>#31 DONE 133.5s
8>#18 [testui:dev] exporting to image
8>#18 exporting layers
从构建日志中我可以看到 COPY 操作没有发生。发布后开始导出图像。
请在我错的地方帮助我。