Search code examples
dockerdocker-composedockerfile

Docker not resolving image config


I am trying to use docker syntax 1.7-labs which allows to use --exclude in COPY. In docker engine config I have

{
  "builder": {
    "gc": {
      "defaultKeepStorage": "20GB",
      "enabled": true
    }
  },
  "experimental": false,
  "features": {
    "buildkit": true
  }
}

On the same machine I have 2 projects where in the first one I have:

# 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"]

When I build this image with docker build --platform --no-cache linux/amd64 --secret id=nuget_login,src=nuget_login.secret -f Src/Api.Services/Dockerfile -api.services:latest . it works ok and the progress even reports using the config:

[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
...

But the second one is throwing:

=> [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

The docker file in the second one is:

# 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"]

The command to build the second image is: 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 .

Any idea why the second one is throwing the parse error even though the docker file syntax config is specified on the first line?


Solution

  • I have checked line endings and they are all LF but I spotted that the working docker file was as ASCII text and the non working ones were in Unicode UTF-8 when I changed them to ASCII encoding it started to work OK.