Dockerfile.buildkit 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.5.0
  18. # $ docker image inspect --format "{{.RepoDigests}}" docker.io/vaultwarden/web-vault:v2023.5.0
  19. # [docker.io/vaultwarden/web-vault@sha256:e5b5e99d132d50dc73176afb65f41cf3b834fb06bfa1d621ac16c705c3f10085]
  20. #
  21. # - Conversely, to get the tag name from the digest:
  22. # $ docker image inspect --format "{{.RepoTags}}" docker.io/vaultwarden/web-vault@sha256:e5b5e99d132d50dc73176afb65f41cf3b834fb06bfa1d621ac16c705c3f10085
  23. # [docker.io/vaultwarden/web-vault:v2023.5.0]
  24. #
  25. FROM docker.io/vaultwarden/web-vault@sha256:e5b5e99d132d50dc73176afb65f41cf3b834fb06bfa1d621ac16c705c3f10085 as vault
  26. ########################## BUILD IMAGE ##########################
  27. FROM docker.io/library/rust:1.70.0-bullseye 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. USER="root"
  35. # Create CARGO_HOME folder and don't download rust docs
  36. RUN --mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/root/.cargo/registry mkdir -pv "${CARGO_HOME}" \
  37. && rustup set profile minimal
  38. # Install build dependencies for the armel architecture
  39. RUN dpkg --add-architecture armel \
  40. && apt-get update \
  41. && apt-get install -y \
  42. --no-install-recommends \
  43. gcc-arm-linux-gnueabi \
  44. libc6-dev:armel \
  45. libmariadb-dev:armel \
  46. libmariadb-dev-compat:armel \
  47. libmariadb3:armel \
  48. libpq-dev:armel \
  49. libpq5:armel \
  50. libssl-dev:armel \
  51. #
  52. # Make sure cargo has the right target config
  53. && echo '[target.arm-unknown-linux-gnueabi]' >> "${CARGO_HOME}/config" \
  54. && echo 'linker = "arm-linux-gnueabi-gcc"' >> "${CARGO_HOME}/config" \
  55. && echo 'rustflags = ["-L/usr/lib/arm-linux-gnueabi"]' >> "${CARGO_HOME}/config"
  56. # Set arm specific environment values
  57. ENV CC_arm_unknown_linux_gnueabi="/usr/bin/arm-linux-gnueabi-gcc" \
  58. CROSS_COMPILE="1" \
  59. OPENSSL_INCLUDE_DIR="/usr/include/arm-linux-gnueabi" \
  60. OPENSSL_LIB_DIR="/usr/lib/arm-linux-gnueabi"
  61. # Creates a dummy project used to grab dependencies
  62. RUN USER=root cargo new --bin /app
  63. WORKDIR /app
  64. # Copies over *only* your manifests and build files
  65. COPY ./Cargo.* ./
  66. COPY ./rust-toolchain ./rust-toolchain
  67. COPY ./build.rs ./build.rs
  68. RUN --mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/root/.cargo/registry rustup target add arm-unknown-linux-gnueabi
  69. # Configure the DB ARG as late as possible to not invalidate the cached layers above
  70. ARG DB=sqlite,mysql,postgresql
  71. # Builds your dependencies and removes the
  72. # dummy project, except the target folder
  73. # This folder contains the compiled dependencies
  74. RUN --mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/root/.cargo/registry cargo build --features ${DB} --release --target=arm-unknown-linux-gnueabi \
  75. && find . -not -path "./target*" -delete
  76. # Copies the complete project
  77. # To avoid copying unneeded files, use .dockerignore
  78. COPY . .
  79. # Make sure that we actually build the project
  80. RUN touch src/main.rs
  81. # Builds again, this time it'll just be
  82. # your actual source files being built
  83. RUN --mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/root/.cargo/registry cargo build --features ${DB} --release --target=arm-unknown-linux-gnueabi
  84. ######################## RUNTIME IMAGE ########################
  85. # Create a new stage with a minimal image
  86. # because we already have a binary built
  87. FROM docker.io/balenalib/rpi-debian:bullseye
  88. ENV ROCKET_PROFILE="release" \
  89. ROCKET_ADDRESS=0.0.0.0 \
  90. ROCKET_PORT=80
  91. RUN [ "cross-build-start" ]
  92. # Create data folder and Install needed libraries
  93. RUN mkdir /data \
  94. && apt-get update && apt-get install -y \
  95. --no-install-recommends \
  96. ca-certificates \
  97. curl \
  98. libmariadb-dev-compat \
  99. libpq5 \
  100. openssl \
  101. && apt-get clean \
  102. && rm -rf /var/lib/apt/lists/*
  103. # In the Balena Bullseye images for armv6/rpi-debian there is a missing symlink.
  104. # This symlink was there in the buster images, and for some reason this is needed.
  105. RUN ln -v -s /lib/ld-linux-armhf.so.3 /lib/ld-linux.so.3
  106. RUN [ "cross-build-end" ]
  107. VOLUME /data
  108. EXPOSE 80
  109. EXPOSE 3012
  110. # Copies the files from the context (Rocket.toml file and web-vault)
  111. # and the binary from the "build" stage to the current stage
  112. WORKDIR /
  113. COPY --from=vault /web-vault ./web-vault
  114. COPY --from=build /app/target/arm-unknown-linux-gnueabi/release/vaultwarden .
  115. COPY docker/healthcheck.sh /healthcheck.sh
  116. COPY docker/start.sh /start.sh
  117. HEALTHCHECK --interval=60s --timeout=10s CMD ["/healthcheck.sh"]
  118. CMD ["/start.sh"]