Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This is not really a bug, need guidance on a proper dockerfile for .net 8 preview 6 aot build for linux #8663

Closed
jdurnil opened this issue Aug 4, 2023 · 26 comments

Comments

@jdurnil
Copy link

jdurnil commented Aug 4, 2023

Hello,

I have created a native aot build on the windows side and have it working using the the new api project template in .net 8 preview 6. I also managed to get a native build using visual studio code in the terminal after installing dotnet 8 preview 6 and the suggested clang install in my wsl2 distribution. My next goal was to get a linux docker container built for the application but am having trouble getting my docker file right as i am using the .net8 preview 6 linux build but it of course doesnt have the clang installed to compile the native binary. Here is my docker file its a feeble attempt to get clang installed the aspnetcore image and put it in the PATH

FROM debian:latest AS temp
# Install clang and zlib1g-dev
RUN apt-get update && \
    apt-get install -y clang zlib1g-dev


FROM mcr.microsoft.com/dotnet/aspnet:8.0-preview AS base

WORKDIR /app
EXPOSE 5500

ENV ASPNETCORE_URLS=http://+:5500
USER app
FROM mcr.microsoft.com/dotnet/sdk:8.0-preview AS build
COPY --from=temp /usr/lib/x86_64-linux-gnu/libclang* /usr/lib/x86_64-linux-gnu/
COPY --from=temp /usr/include/clang* /usr/include/
COPY --from=temp /usr/lib/x86_64-linux-gnu/libz* /usr/lib/x86_64-linux-gnu/
ENV PATH="/usr/lib/llvm-7/bin:${PATH}"
ARG configuration=Release
WORKDIR /src
COPY ["MyFirstAotWebApi.csproj", "./"]
RUN dotnet restore "MyFirstAotWebApi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "MyFirstAotWebApi.csproj" -c $configuration -o /app/build

FROM build AS publish
ARG configuration=Release
RUN dotnet publish "MyFirstAotWebApi.csproj" -c $configuration -o /app/publish /p:UseAppHost=true

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyFirstAotWebApi.dll"]

I believe the last part is also wrong as we dont need "dotnet" to run it and I dont think a dll is being produced, what should the ENTRYPOINT be and how do i get apt-get install -y clang zlib1g-dev installed on the image for the native compile?

@vitek-karas
Copy link
Member

@richlander I think you've been looking into this scenario, right?

@richlander
Copy link
Member

richlander commented Aug 4, 2023

Lots of options to consider at dotnet/dotnet-docker#4742

Looks like you are doing it "the hard way". That Debian stage isn't needed. You can do all of that in the SDK stage, skipping all of that error prone copying.

debian:latest and mcr.microsoft.com/dotnet/aspnet:8.0-preview are the exact same build of Debian.

ARG configuration=Release -- That's not needed unless you also want debug images. dotnet publish produces release assets by default with .NET 8.

@jdurnil
Copy link
Author

jdurnil commented Aug 5, 2023 via email

@jdurnil
Copy link
Author

jdurnil commented Aug 7, 2023 via email

@richlander
Copy link
Member

Do you have buildkit enabled? These samples require it.

https://docs.docker.com/build/buildkit/

@jdurnil
Copy link
Author

jdurnil commented Aug 7, 2023

hmm no i dont, ill install it

@jdurnil
Copy link
Author

jdurnil commented Aug 7, 2023

I actually i have docker desktop installed with wsl2 support, it says there is nothing more i need to do to enable buildkit

@jdurnil
Copy link
Author

jdurnil commented Aug 7, 2023

I tried
docker buildx build .

I am still getting

>  [stage-1 1/3] FROM mcr.microsoft.com/dotnet/runtime-deps:8.0-preview-jammy-chiseled@sha256:833c72f664dfa4e98ee66eab93e8a67ab  0.0s
>  => CACHED [stage-1 2/3] WORKDIR /app                                                                                             0.0s
>  => ERROR [build 2/8] RUN <<EOF (apt-get update...)                                                                               0.4s
> ------
>  > [build 2/8] RUN <<EOF (apt-get update...):
> 0.364 E: Invalid operation update
> 0.369 Reading package lists...
> 0.376 Building dependency tree...
> 0.376 Reading state information...
> 0.377 E: Unable to locate package build-essential

@jdurnil
Copy link
Author

jdurnil commented Aug 7, 2023

Figured out the issue and got my native container built!!
for some reason my docker build did not like the RUN <<EOF
as soon as i got rid of that and changed it to the following my container built.
RUN apt-get update
RUN apt-get install -y build-essential clang libz-dev

@richlander
Copy link
Member

richlander commented Aug 7, 2023

Very odd. I haven't tried that Dockerfile in WSL. Which distro (and version) were you using and which Docker version? I successfully built that Dockerfile in Ubuntu 22.04.

@jdurnil
Copy link
Author

jdurnil commented Aug 7, 2023 via email

@jdurnil
Copy link
Author

jdurnil commented Aug 7, 2023 via email

@jdurnil
Copy link
Author

jdurnil commented Aug 7, 2023 via email

@jdurnil
Copy link
Author

jdurnil commented Aug 7, 2023

One last thing i wanted to mention that might be of interest when creating docker files for this is in the aot project
<InvariantGlobalization>false</InvariantGlobalization> gets set to true automatically. Microsoft.data.sqlclient will not work with this set to true. However my container wouldnt build when i set it to false so i had to add
RUN apt-get install libicu70
to get it to build. Hopefully it will now all work with my sql connections. If so you guys have something amazing here,

@richlander
Copy link
Member

We set this value to true by default in AOT project files.

@jdurnil
Copy link
Author

jdurnil commented Aug 7, 2023 via email

@richlander
Copy link
Member

Yup. You can change the default.

@richlander
Copy link
Member

This appears to be why the Dockerfile failed on your machine.

moby/buildkit#3329 (comment)

@jdurnil
Copy link
Author

jdurnil commented Aug 8, 2023

Hello Richard I am really close to a solution but i need apt-get install libicu70 to be installed on mcr.microsoft.com/dotnet/runtime-deps:8.0-preview-jammy-chiseled. Ive noticed it is a slim build and exactly what i want but of course i cant run apt-get update or apt get install on their. In my build image I am using
FROM mcr.microsoft.com/dotnet/sdk:8.0-preview-jammy AS build ARG TARGETARCH RUN apt-get update RUN apt-get install libicu70 RUN apt-get install -y build-essential clang libz-dev

I just want to get the libicu70 install and whatever path env variables it might need on the chiseled image. Is there anything you could recommend.
when i use the build image as the main final image it ends up being like 843 mb instead of 23 mb and that is not desirable.
The solution works when using the main build image it is just huge.

@jdurnil
Copy link
Author

jdurnil commented Aug 8, 2023

I tried this because the other one gave me errors from the beginning

`FROM golang:1.18 as chisel

RUN git clone --depth 1 -b main https://github.com/canonical/chisel /opt/chisel
WORKDIR /opt/chisel
RUN go build ./cmd/chisel


FROM mcr.microsoft.com/dotnet/sdk:8.0-preview-jammy AS build

RUN apt-get update
RUN apt-get install clang zlib1g-dev -y
COPY --from=chisel /opt/chisel/chisel /usr/bin/
RUN mkdir /rootfs \
    && chisel cut --release "ubuntu-22.04" --root /rootfs \
        libicu70_libs


WORKDIR /source

# copy csproj and restore as distinct layers
COPY *.csproj .
RUN dotnet restore -r linux-arm64


# copy and publish app and libraries
COPY . .
RUN dotnet publish -r linux-arm64 --no-restore -o /app MySecondAotWebApi.csproj   
RUN rm /app/*.dbg /app/*.Development.json

# final stage/image
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0-preview-jammy-chiseled

COPY --from=build /rootfs /
WORKDIR /app
COPY --from=build /app . 
ENTRYPOINT ["./MySecondAotWebApi"]`


I get this globalization error when trying to build the native code part
`91.40 clang : error : linker command failed with exit code 1 (use -v to see invocation) [/source/MySecondAotWebApi.csproj]
91.42 /root/.nuget/packages/microsoft.dotnet.ilcompiler/8.0.0-preview.7.23375.6/build/Microsoft.NETCore.Native.targets(357,5): error MSB3073: The command ""clang" "obj/Release/net8.0/linux-arm64/native/MySecondAotWebApi.o

36.18 /root/.nuget/packages/azure.core/1.25.0/lib/net5.0/Azure.Core.dll : warning IL3053: Assembly 'Azure.Core' produced AOT analysis warnings. [/source/MySecondAotWebApi.csproj]
91.39 /usr/bin/ld.bfd: unrecognised emulation mode: aarch64linux
91.39 Supported emulations: elf_x86_64 elf32_x86_64 elf_i386 elf_iamcu elf_l1om elf_k1om i386pep i386pe
91.40 clang : error : linker command failed with exit code 1 (use -v to see invocation) [/source/MySecondAotWebApi.csproj

SBuild version 17.8.0-preview-23367-03+0ff2a83e9 for .NET
3.356 Determining projects to restore...

@jdurnil
Copy link
Author

jdurnil commented Aug 8, 2023

figured out my issue, for some reason i had labeled the linux builds asr arm64 instead of amd064 since with this script the #TARGETARCH variable was not coming through it builds now

@richlander
Copy link
Member

richlander commented Aug 8, 2023

Cool. The chisel pieces should all be in the chisel stage.

The way Dockerfile ARGs work is unintuitive. They have to be copied through the stages.

@tarekgh
Copy link
Member

tarekgh commented Aug 17, 2023

Is there anything pending here or can we close this issue?

@richlander
Copy link
Member

If this continues to be a problem, please raise the issues in dotnet/docker (assuming they are container related).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants