我正在尝试使用 docker 语法 1.7-labs,它允许在
--exclude
中使用 COPY
。在 docker 引擎配置中我有
{
"builder": {
"gc": {
"defaultKeepStorage": "20GB",
"enabled": true
}
},
"experimental": false,
"features": {
"buildkit": true
}
}
在同一台机器上,我有 2 个项目,其中第一个项目是:
# syntax=docker.io/docker/dockerfile:1.7-labs
# The line above is to enable the --exclude in COPY command and has to be TOTALLY first. See https://stackoverflow.com/a/78110854 and comments under that answer.
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
# Copy nuget.config file before restoring
COPY nuget.config .
# Update & replace placeholders in nuget.config
RUN --mount=type=secret,id=uget_login . /run/secrets/nuget_login && \
sed -i "s|{NUGET_USERNAME}|$NUGET_USERNAME|g" nuget.config && \
sed -i "s|{NUGET_PASSWORD}|$NUGET_PASSWORD|g" nuget.config
# this step is for caching purposes
COPY ["Src/Api.Services/Api.Services.csproj", "Src/Api.Services/"]
RUN dotnet restore "./Src/Api.Services/Api.Services.csproj" --configfile /src/nuget.config
COPY --exclude=nuget.config . .
WORKDIR "/src/Src/Api.Services"
RUN dotnet build "./Api.Services.csproj" --configfile /src/nuget.config -c $BUILD_CONFIGURATION -o /app/build
RUN dotnet dev-certs https --trust
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./Api.Services.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.Services.dll"]
当我使用
docker build --platform --no-cache linux/amd64 --secret id=nuget_login,src=nuget_login.secret -f Src/Api.Services/Dockerfile -api.services:latest .
构建此图像时,它工作正常,甚至使用配置报告进度:
[internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 1.68kB 0.0s
=> resolve image config for docker-image://docker.io/docker/dockerfile:1.7-labs 7.5s
=> CACHED docker-image://docker.io/docker/dockerfile:1.7-labs@sha256:b99fecfe00268a8b556fad7d9c37ee25d716ae08a5d7320e6d51c4dd83246894 0.0s
=> [internal] load metadata for mcr.microsoft.com/dotnet/sdk:8.0
...
但是第二个
正在投掷:
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 1.66kB 0.0s
Dockerfile:24
--------------------
22 | COPY ["Src/Manufacture.Api.Services/Manufacture.Api.Services.csproj", "Src/Manufacture.Api.Services/"]
23 | RUN dotnet restore "Src/Manufacture.Api.Services/Manufacture.Api.Services.csproj" --configfile /src/nuget.docker.config
24 | >>> COPY --exclude=nuget.docker.config . .
25 | WORKDIR "/src/Src/Manufacture.Api.Services"
26 | RUN dotnet build "Manufacture.Api.Services.csproj" -c $BUILD_CONFIGURATION -o /app/build
--------------------
ERROR: failed to solve: dockerfile parse error on line 24: unknown flag: --exclude
第二个中的docker文件是:
# syntax=docker.io/docker/dockerfile:1.7-labs
# The line above is to enable the --exclude in COPY command and has to be TOTALLY first. See https://stackoverflow.com/a/78110854 and comments under that answer.
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
# Copy nuget.docker.config file before restoring
COPY nuget.docker.config .
# Update & replace placeholders in nuget.docker.config
RUN --mount=type=secret,id=nuget_login . /run/secrets/nuget_login && \
sed -i "s|{NUGET_USERNAME}|$NUGET_USERNAME|g" nuget.docker.config && \
sed -i "s|{NUGET_PASSWORD}|$NUGET_PASSWORD|g" nuget.docker.config
COPY ["Src/Manufacture.Api.Services/Manufacture.Api.Services.csproj", "Src/Manufacture.Api.Services/"]
RUN dotnet restore "Src/Manufacture.Api.Services/Manufacture.Api.Services.csproj" --configfile /src/nuget.docker.config
COPY --exclude=nuget.docker.config . .
WORKDIR "/src/Src/Manufacture.Api.Services"
RUN dotnet build "Manufacture.Api.Services.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "Manufacture.Api.Services.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Manufacture.Api.Services.dll"]
构建第二个镜像的命令是:
docker build --no-cache --platform linux/amd64 --secret id=nuget_login,src=nuget_login.secret -f Src/Manufacture.Api.Services/Dockerfile -t manufacture.api.services:latest .
知道为什么第二个抛出解析错误,即使在第一行指定了 docker 文件语法配置吗?
ASCII
文本,非工作文件是
Unicode UTF-8
当我将它们更改为 ASCII
编码时,它开始正常工作。