2
\$\begingroup\$

I have the following dockerfile, which builds a Backend and Frontend for a large project. The project uses the following technologies:

  • C++ (using vcpkg as a package manager <-- very slow)
  • Swift (using SPM <-- reasonably fast)
  • Rust (using Cargo <-- reasonably fast)
  • NPM (reasonably fast)

Can you review this docker build with a focus on speed and performance? I am looking forward to replace this build script with nix; will this negatively impact the performance of this project?

FROM swift:6.0.3-noble AS build1-environment
RUN apt update && apt upgrade -y && apt install -y curl && curl -sfS https://dotenvx.sh | sh
FROM build1-environment AS build2-environment
RUN apt install -y curl git cmake \
 ninja-build gdb clangd clang-format clang-tidy zip unzip swi-prolog \
 cargo rustc rust-src rustfmt npm
# we do not need to install clang since it is included in the swift:noble image
FROM build2-environment AS vcpkg-builder
# install vcpkg
RUN curl -L -o vcpkg.tar.gz https://github.com/microsoft/vcpkg/archive/master.tar.gz
RUN mkdir /opt/vcpkg
RUN tar xf vcpkg.tar.gz --strip-components=1 -C /opt/vcpkg
RUN /opt/vcpkg/bootstrap-vcpkg.sh
RUN ln -s /opt/vcpkg/vcpkg /usr/local/bin/vcpkg
# build dependencies
WORKDIR /tmp/vcpkg-cache
COPY vcpkg.json .
ENV VCPKG_BUILD_PARALLEL_LEVEL=8
RUN vcpkg install --triplet x64-linux
FROM build2-environment AS development
ARG BUILD_MODE
WORKDIR /
RUN git config --global --add safe.directory /workspace
FROM development AS build
ARG BUILD_MODE
COPY --from=vcpkg-builder --chmod=777 /root/.cache/vcpkg ~/.cache/vcpkg
RUN mkdir /workspace
COPY . /workspace
WORKDIR /workspace
RUN dotenvx run -f .env.${BUILD_MODE} -- cmake \
 --preset debug-x86-64-unknown-linux-gnu \
 -S . \
 -B out/build/debug-x86-64-unknown-linux-gnu \
 -G Ninja
RUN dotenvx run -f .env.${BUILD_MODE} -- ninja \
 -C out/build/debug-x86-64-unknown-linux-gnu \
 SwiftPackage
RUN strip .build/debug/LX
FROM build1-environment AS production
ARG BUILD_MODE
ENV BUILD_MODE=${BUILD_MODE}
RUN mkdir /app
COPY --from=build /workspace/.build/debug/LX /app
COPY --from=build /workspace/.env* /app
WORKDIR /app
CMD /usr/bin/env dotenvx run -f .env.${BUILD_MODE} -- /app/LX
EXPOSE 1337
HEALTHCHECK CMD curl --fail http://localhost:1337/version || exit 1

Currently, the build times are around 30min, mostly for apt-get-dependencies and vcpkg.

asked Mar 3 at 10:00
\$\endgroup\$
4
  • \$\begingroup\$ @968FC1F126E85E51FBF0B43D0B02D Let us assume you have already built this on your laptop, and it took ~ 30 minutes. The speed question seems to be, "I changed a single character of source code" (without changing the Dockerfile), and now "how long will it take to rebuild?" That is, we care about caching each layer of the build. Please edit the OP to tell us about the typical place an edit will occur, and what observed timings such an edit produces. Maybe there's a GitHub repo URL you'd like to tell us about? \$\endgroup\$ Commented Mar 3 at 19:01
  • 1
    \$\begingroup\$ Maybe combine multiple RUN commands (like for installing vcpkg) into a single RUN command using &&. Reference: stackoverflow.com/a/39330458/4688321. Also, you may want to remove vcpkg.tar.gz once its extracted. \$\endgroup\$ Commented Mar 4 at 9:50
  • 1
    \$\begingroup\$ I don't see any use of rust/cargo or npm in this Dockerfile. Are they used by some of the tools internally? If yes, by which ones? \$\endgroup\$ Commented Mar 5 at 22:53
  • \$\begingroup\$ Yes, it is invoked by cmake :) Thank you nevertheless! \$\endgroup\$ Commented Mar 12 at 6:34

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.