#Multi-stage docker build with Directory.docker_build()?

1 messages ยท Page 1 of 1 (latest)

river breach
#

Hello,
while daggerizing an existing application, we are tempted to re-use the existing multi-stage Dockerfile by executing Directory.docker_build(): https://dagger-io.readthedocs.io/en/latest/client.html#dagger.Directory.docker_build

The general structure of the Dockerfile is like this:

FROM (...)/dotnet/sdk:8.0 AS restore
WORKDIR /src
COPY ["src/MyProject/MyProject.csproj", "src/MyProject/"]
COPY ["nuget.config", "."]
RUN dotnet restore "src/MyProject/MyProject.csproj"

FROM restore AS build
ARG BUILD_VERSION
ENV BUILD_VERSION=${BUILD_VERSION:-0.0.0}
COPY . .
WORKDIR "/src/src/MyProject"
RUN dotnet publish "MyProject.csproj" --no-restore -c Release -o /app/publish -p:Version=$BUILD_VERSION

# Final Stage
FROM ${REPO_LOCATION}/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /app/publish .

ENV HTTP_PORTS=80
EXPOSE 80

ENTRYPOINT ["dotnet", "MyProject.dll"]

For now we stumble upon a dotnet build failure ("missing dependency") as if the restore stage result is not correctly passed to the build stage.

Is it a good way to proceed or are we totally wrong on the approach?

cedar estuary
#

@river breach it should work fine, generally it's more pleasant to just replace the Dockerfile with native Dagger code, but sometimes that's not practical or convenient, or you want to do it later. In those cases it's fine to use Directory.docker_build. There are a few limitations, but the core implementation itself is solid - it's literally the same buildkit implementation used by official docker build ๐Ÿ™‚

Could your error be caused by a missing build arg perhaps?

#

Basic multi-stage should absolutely not be a problem

river breach
#

Hi @cedar estuary thanks for your reply!
FYI the docker build works perfectly when ran in a terminal.
When ran from a dagger function, the dotnet publish --no-restore complains about a missing lib.
We started to doubt that docker multi-stage builds were completely supported, but if you say it should work we'll keep investigating! ๐Ÿ™‚

karmic summit
river breach
#

Thank you @karmic summit , next week we might need some help if we're still stuck ๐Ÿ˜‡

cedar estuary
#

@river breach if you have a trace URL showing the issue, that would probably help. It's very easy (and free) to setup in Dagger Cloud.

river breach
#

Not sure how to do it, I use an individual plan for now, which allows me to explore my local traces in a friendly environment.
Would you be able to explore a trace of mine if I just shared the trace's URL?

cedar estuary
fierce hatch
#

(working on this with Fabrice) - if that helps, it seems that the reported missing lib in the error message is a transitive dependency - however in the build stage, as far as we can tell, it's available on the /root/.nugget/packages on the container, which is where I understand dotnet is supposed to look during the build process
We also compared the env variables on a local docker run and in the dagger run, and I couldn't see any significant differences

fierce hatch
fierce hatch
#

hey @karmic summit if you've got the chance to have a look at the trace above ๐Ÿ˜‡

karmic summit
fierce hatch
#

yes sure !

karmic summit
#

as always.. we jumped into a call and everything was working all of the sudden. ๐Ÿ™Œ

#

let us know if you have any other issues @fierce hatch ๐Ÿ™

fierce hatch
#

thanks for the help ! unfortunately, it works for me but the same code doesn't on Fabrice's machine ๐Ÿ˜ฌ

karmic summit
fierce hatch
#

yes he did, it didn't help

#

also, we're not seeing in the cloud traces all the dockerfile steps - the log starts at "build 2/3"

river breach
#

As Mikael said, it is not very easy to see clearly all the steps of the multi-stage build in Dagger cloud

#

It could almost be seen as if it started directly at the second stage

karmic summit
#

@river breach have a few minutes to jump into a quick call with you to check it out together?

#

I can't see anything evident from the trace

river breach
#

Hey @karmic summit , sure, I'm available if you're still available too!

karmic summit
#

we found the issue why it was working in @fierce hatch's setup and not in others. Since we're not parsing .dockerignore, some things were being copied into the build context which made the build to fail.

#

cc @wanton flower WDYT about maybe asking Rajat to check this one out?

river breach
#

@karmic summit I can confirm that our 3rd colleague also managed to succeed with their multi-stage build once the filtering was done!
Thanks again for your time! ๐Ÿ™

karmic summit