我正在尝试将 .NET Aspire 项目部署到 Docker 容器。我使用
docker init
创建了 dockerfile 和 docker compose,但是当我尝试运行 docker compose up --build 时,它说没有找到安装的 Aspire 工作负载。我尝试添加命令来安装工作负载,但现在当我运行发布命令时,它不会创建输出文件夹。
Dockerfile
# syntax=docker/dockerfile:1
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/
# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7
################################################################################
# Learn about building .NET container images:
# https://github.com/dotnet/dotnet-docker/blob/main/samples/README.md
# Create a stage for building the application.
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
COPY . /source
WORKDIR /source/Catalog.AppHost
# This is the architecture you’re building for, which is passed in by the builder.
# Placing it here allows the previous steps to be cached across architectures.
ARG TARGETARCH
# Build the application.
# Leverage a cache mount to /root/.nuget/packages so that subsequent builds don't have to re-download packages.
# If TARGETARCH is "amd64", replace it with "x64" - "x64" is .NET's canonical name for this and "amd64" doesn't
# work in .NET 6.0.
RUN dotnet workload install aspire && \
dotnet restore
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
dotnet publish -a ${TARGETARCH/amd64/x64} -c Debug --use-current-runtime --self-contained false -o /app && \
ls -al && ls ../ -al && ls ../../ -al
# If you need to enable globalization and time zones:
# https://github.com/dotnet/dotnet-docker/blob/main/samples/enable-globalization.md
################################################################################
# Create a new stage for running the application that contains the minimal
# runtime dependencies for the application. This often uses a different base
# image from the build stage where the necessary files are copied from the build
# stage.
#
# The example below uses an aspnet alpine image as the foundation for running the app.
# It will also use whatever happens to be the most recent version of that tag when you
# build your Dockerfile. If reproducability is important, consider using a more specific
# version (e.g., aspnet:7.0.10-alpine-3.18),
# or SHA (e.g., mcr.microsoft.com/dotnet/aspnet@sha256:f3d99f54d504a21d38e4cc2f13ff47d67235efeeb85c109d3d1ff1808b38d034).
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS final
WORKDIR /app
# Copy everything needed to run the app from the "build" stage.
COPY --from=build /app .
# Switch to a non-privileged user (defined in the base image) that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
# and https://github.com/dotnet/dotnet-docker/discussions/4764
USER $APP_UID
ENTRYPOINT ["dotnet", "Catalog.AppHost.dll"]
docker-compose
services:
server:
build:
context: .
target: final
ports:
- 8080:8080
错误:
docker compose up --build
[+] Building 4.1s (16/16) FINISHED docker:desktop-linux
=> [server internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 2.80kB 0.0s
=> [server] resolve image config for docker-image://docker.io/docker/dockerfile:1 1.0s
=> [server auth] docker/dockerfile:pull token for registry-1.docker.io 0.0s
=> CACHED [server] docker-image://docker.io/docker/dockerfile:1@sha256:fe40cf4e92cd0c467be2cfc30657a680ae2398318afd50b0c80585784c604f28 0.0s
=> [server internal] load metadata for mcr.microsoft.com/dotnet/sdk:8.0-alpine 0.4s
=> [server internal] load metadata for mcr.microsoft.com/dotnet/aspnet:8.0-alpine 0.4s
=> [server internal] load .dockerignore 0.0s
=> => transferring context: 647B 0.0s
=> [server internal] load build context 0.0s
=> => transferring context: 2.73kB 0.0s
=> [server build 1/5] FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine@sha256:6e8997576d16a6d7b4e6ba7ac0956d3ae46cb7a376581c40eabd20fbc5c28b8d 0.0s
=> [server final 1/3] FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine@sha256:e22d879fb9a0fba3b2a6b15c88f005192c239e189061534c86643347b4630140 0.0s
=> CACHED [server build 2/5] COPY . /source 0.0s
=> CACHED [server build 3/5] WORKDIR /source/Catalog.AppHost 0.0s
=> CACHED [server build 4/5] RUN dotnet workload install aspire && dotnet restore 0.0s
=> [server build 5/5] RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages dotnet publish -a x64 -c Testing --use-current-runtime --self-contai 2.1s
=> CACHED [server final 2/3] WORKDIR /app 0.0s
=> ERROR [server final 3/3] COPY --from=build /app . 0.0s
------
> [server final 3/3] COPY --from=build /app .:
------
failed to solve: failed to compute cache key: failed to calculate checksum of ref b9d56e54-9eee-48c5-b9a6-71b47c1d6335::lymdtg8e61phx9dtukafwjrl7: "/app": not found
我尝试检查是否确实创建了一个文件夹,但如果我在可能包含该文件夹的所有目录上运行 ls,它不会出现在任何地方。
我使用 aspirate 创建我的 aspire 解决方案的 docker compose 文件。
安装吸液器
dotnet tool install -g aspirate
然后要生成 compose 文件,请 cd 进入 AppHost 项目文件夹并执行以下命令。
aspirate generate --output-format compose
这将在 AppHost 项目文件夹的子文件夹 aspirate-output 中创建一个 docker compose 文件。然后您可以根据需要进行调整。