Dockerfile.buildkit 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # syntax=docker/dockerfile:1
  2. # This file was generated using a Jinja2 template.
  3. # Please make your changes in `Dockerfile.j2` and then `make` the individual Dockerfiles.
  4. # Using multistage build:
  5. # https://docs.docker.com/develop/develop-images/multistage-build/
  6. # https://whitfin.io/speeding-up-rust-docker-builds/
  7. ####################### VAULT BUILD IMAGE #######################
  8. # The web-vault digest specifies a particular web-vault build on Docker Hub.
  9. # Using the digest instead of the tag name provides better security,
  10. # as the digest of an image is immutable, whereas a tag name can later
  11. # be changed to point to a malicious image.
  12. #
  13. # To verify the current digest for a given tag name:
  14. # - From https://hub.docker.com/r/vaultwarden/web-vault/tags,
  15. # click the tag name to view the digest of the image it currently points to.
  16. # - From the command line:
  17. # $ docker pull docker.io/vaultwarden/web-vault:v2023.8.2
  18. # $ docker image inspect --format "{{.RepoDigests}}" docker.io/vaultwarden/web-vault:v2023.8.2
  19. # [docker.io/vaultwarden/web-vault@sha256:b361e79309ef2c4368f880f350166daade41eb0927a9adf376c76e3713027252]
  20. #
  21. # - Conversely, to get the tag name from the digest:
  22. # $ docker image inspect --format "{{.RepoTags}}" docker.io/vaultwarden/web-vault@sha256:b361e79309ef2c4368f880f350166daade41eb0927a9adf376c76e3713027252
  23. # [docker.io/vaultwarden/web-vault:v2023.8.2]
  24. #
  25. FROM docker.io/vaultwarden/web-vault@sha256:b361e79309ef2c4368f880f350166daade41eb0927a9adf376c76e3713027252 as vault
  26. ########################## BUILD IMAGE ##########################
  27. FROM docker.io/library/rust:1.72.0-bookworm as build
  28. # Build time options to avoid dpkg warnings and help with reproducible builds.
  29. ENV DEBIAN_FRONTEND=noninteractive \
  30. LANG=C.UTF-8 \
  31. TZ=UTC \
  32. TERM=xterm-256color \
  33. CARGO_HOME="/root/.cargo" \
  34. REGISTRIES_CRATES_IO_PROTOCOL=sparse \
  35. USER="root"
  36. # Create CARGO_HOME folder and don't download rust docs
  37. RUN --mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/root/.cargo/registry mkdir -pv "${CARGO_HOME}" \
  38. && rustup set profile minimal
  39. # Install build dependencies for the arm64 architecture
  40. RUN --mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/root/.cargo/registry dpkg --add-architecture arm64 \
  41. && apt-get update \
  42. && apt-get install -y \
  43. --no-install-recommends \
  44. gcc-aarch64-linux-gnu \
  45. libc6-dev:arm64 \
  46. linux-libc-dev:arm64 \
  47. libmariadb-dev:arm64 \
  48. libmariadb-dev-compat:arm64 \
  49. libmariadb3:arm64 \
  50. libpq-dev:arm64 \
  51. libpq5:arm64 \
  52. libssl-dev:arm64 \
  53. #
  54. # Make sure cargo has the right target config
  55. && echo '[target.aarch64-unknown-linux-gnu]' >> "${CARGO_HOME}/config" \
  56. && echo 'linker = "aarch64-linux-gnu-gcc"' >> "${CARGO_HOME}/config" \
  57. && echo 'rustflags = ["-L/usr/lib/aarch64-linux-gnu"]' >> "${CARGO_HOME}/config"
  58. # Set arm specific environment values
  59. ENV CC_aarch64_unknown_linux_gnu="/usr/bin/aarch64-linux-gnu-gcc" \
  60. CROSS_COMPILE="1" \
  61. OPENSSL_INCLUDE_DIR="/usr/include/aarch64-linux-gnu" \
  62. OPENSSL_LIB_DIR="/usr/lib/aarch64-linux-gnu"
  63. # Creates a dummy project used to grab dependencies
  64. RUN USER=root cargo new --bin /app
  65. WORKDIR /app
  66. # Copies over *only* your manifests and build files
  67. COPY ./Cargo.* ./
  68. COPY ./rust-toolchain.toml ./rust-toolchain.toml
  69. COPY ./build.rs ./build.rs
  70. RUN --mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/root/.cargo/registry rustup target add aarch64-unknown-linux-gnu
  71. # Configure the DB ARG as late as possible to not invalidate the cached layers above
  72. ARG DB=sqlite,mysql,postgresql
  73. # Builds your dependencies and removes the
  74. # dummy project, except the target folder
  75. # This folder contains the compiled dependencies
  76. RUN --mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/root/.cargo/registry cargo build --features ${DB} --release --target=aarch64-unknown-linux-gnu \
  77. && find . -not -path "./target*" -delete
  78. # Copies the complete project
  79. # To avoid copying unneeded files, use .dockerignore
  80. COPY . .
  81. # Make sure that we actually build the project
  82. RUN touch src/main.rs
  83. # Builds again, this time it'll just be
  84. # your actual source files being built
  85. RUN --mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/root/.cargo/registry cargo build --features ${DB} --release --target=aarch64-unknown-linux-gnu
  86. ######################## RUNTIME IMAGE ########################
  87. # Create a new stage with a minimal image
  88. # because we already have a binary built
  89. FROM docker.io/balenalib/aarch64-debian:bookworm
  90. ENV ROCKET_PROFILE="release" \
  91. ROCKET_ADDRESS=0.0.0.0 \
  92. ROCKET_PORT=80
  93. RUN [ "cross-build-start" ]
  94. # Create data folder and Install needed libraries
  95. RUN mkdir /data \
  96. && apt-get update && apt-get install -y \
  97. --no-install-recommends \
  98. ca-certificates \
  99. curl \
  100. libmariadb-dev-compat \
  101. libpq5 \
  102. openssl \
  103. && apt-get clean \
  104. && rm -rf /var/lib/apt/lists/*
  105. RUN [ "cross-build-end" ]
  106. VOLUME /data
  107. EXPOSE 80
  108. EXPOSE 3012
  109. # Copies the files from the context (Rocket.toml file and web-vault)
  110. # and the binary from the "build" stage to the current stage
  111. WORKDIR /
  112. COPY --from=vault /web-vault ./web-vault
  113. COPY --from=build /app/target/aarch64-unknown-linux-gnu/release/vaultwarden .
  114. COPY docker/healthcheck.sh /healthcheck.sh
  115. COPY docker/start.sh /start.sh
  116. HEALTHCHECK --interval=60s --timeout=10s CMD ["/healthcheck.sh"]
  117. CMD ["/start.sh"]