Dockerfile.j2 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. {% set rust_version = "1.70.0" %}
  5. {% set debian_version = "bullseye" %}
  6. {% set alpine_version = "3.17" %}
  7. {% set build_stage_base_image = "docker.io/library/rust:%s-%s" % (rust_version, debian_version) %}
  8. {% if "alpine" in target_file %}
  9. {% if "amd64" in target_file %}
  10. {% set build_stage_base_image = "docker.io/blackdex/rust-musl:x86_64-musl-stable-%s" % rust_version %}
  11. {% set runtime_stage_base_image = "docker.io/library/alpine:%s" % alpine_version %}
  12. {% set package_arch_target = "x86_64-unknown-linux-musl" %}
  13. {% elif "armv7" in target_file %}
  14. {% set build_stage_base_image = "docker.io/blackdex/rust-musl:armv7-musleabihf-stable-%s" % rust_version %}
  15. {% set runtime_stage_base_image = "docker.io/balenalib/armv7hf-alpine:%s" % alpine_version %}
  16. {% set package_arch_target = "armv7-unknown-linux-musleabihf" %}
  17. {% elif "armv6" in target_file %}
  18. {% set build_stage_base_image = "docker.io/blackdex/rust-musl:arm-musleabi-stable-%s" % rust_version %}
  19. {% set runtime_stage_base_image = "docker.io/balenalib/rpi-alpine:%s" % alpine_version %}
  20. {% set package_arch_target = "arm-unknown-linux-musleabi" %}
  21. {% elif "arm64" in target_file %}
  22. {% set build_stage_base_image = "docker.io/blackdex/rust-musl:aarch64-musl-stable-%s" % rust_version %}
  23. {% set runtime_stage_base_image = "docker.io/balenalib/aarch64-alpine:%s" % alpine_version %}
  24. {% set package_arch_target = "aarch64-unknown-linux-musl" %}
  25. {% endif %}
  26. {% elif "amd64" in target_file %}
  27. {% set runtime_stage_base_image = "docker.io/library/debian:%s-slim" % debian_version %}
  28. {% elif "arm64" in target_file %}
  29. {% set runtime_stage_base_image = "docker.io/balenalib/aarch64-debian:%s" % debian_version %}
  30. {% set package_arch_name = "arm64" %}
  31. {% set package_arch_target = "aarch64-unknown-linux-gnu" %}
  32. {% set package_cross_compiler = "aarch64-linux-gnu" %}
  33. {% elif "armv6" in target_file %}
  34. {% set runtime_stage_base_image = "docker.io/balenalib/rpi-debian:%s" % debian_version %}
  35. {% set package_arch_name = "armel" %}
  36. {% set package_arch_target = "arm-unknown-linux-gnueabi" %}
  37. {% set package_cross_compiler = "arm-linux-gnueabi" %}
  38. {% elif "armv7" in target_file %}
  39. {% set runtime_stage_base_image = "docker.io/balenalib/armv7hf-debian:%s" % debian_version %}
  40. {% set package_arch_name = "armhf" %}
  41. {% set package_arch_target = "armv7-unknown-linux-gnueabihf" %}
  42. {% set package_cross_compiler = "arm-linux-gnueabihf" %}
  43. {% endif %}
  44. {% if package_arch_name is defined %}
  45. {% set package_arch_prefix = ":" + package_arch_name %}
  46. {% else %}
  47. {% set package_arch_prefix = "" %}
  48. {% endif %}
  49. {% if package_arch_target is defined %}
  50. {% set package_arch_target_param = " --target=" + package_arch_target %}
  51. {% else %}
  52. {% set package_arch_target_param = "" %}
  53. {% endif %}
  54. {% if "buildkit" in target_file %}
  55. {% set mount_rust_cache = "--mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/root/.cargo/registry " %}
  56. {% else %}
  57. {% set mount_rust_cache = "" %}
  58. {% endif %}
  59. # Using multistage build:
  60. # https://docs.docker.com/develop/develop-images/multistage-build/
  61. # https://whitfin.io/speeding-up-rust-docker-builds/
  62. ####################### VAULT BUILD IMAGE #######################
  63. {% set vault_version = "v2023.5.0" %}
  64. {% set vault_image_digest = "sha256:e5b5e99d132d50dc73176afb65f41cf3b834fb06bfa1d621ac16c705c3f10085" %}
  65. # The web-vault digest specifies a particular web-vault build on Docker Hub.
  66. # Using the digest instead of the tag name provides better security,
  67. # as the digest of an image is immutable, whereas a tag name can later
  68. # be changed to point to a malicious image.
  69. #
  70. # To verify the current digest for a given tag name:
  71. # - From https://hub.docker.com/r/vaultwarden/web-vault/tags,
  72. # click the tag name to view the digest of the image it currently points to.
  73. # - From the command line:
  74. # $ docker pull docker.io/vaultwarden/web-vault:{{ vault_version }}
  75. # $ docker image inspect --format "{{ '{{' }}.RepoDigests}}" docker.io/vaultwarden/web-vault:{{ vault_version }}
  76. # [docker.io/vaultwarden/web-vault@{{ vault_image_digest }}]
  77. #
  78. # - Conversely, to get the tag name from the digest:
  79. # $ docker image inspect --format "{{ '{{' }}.RepoTags}}" docker.io/vaultwarden/web-vault@{{ vault_image_digest }}
  80. # [docker.io/vaultwarden/web-vault:{{ vault_version }}]
  81. #
  82. FROM docker.io/vaultwarden/web-vault@{{ vault_image_digest }} as vault
  83. ########################## BUILD IMAGE ##########################
  84. FROM {{ build_stage_base_image }} as build
  85. # Build time options to avoid dpkg warnings and help with reproducible builds.
  86. ENV DEBIAN_FRONTEND=noninteractive \
  87. LANG=C.UTF-8 \
  88. TZ=UTC \
  89. TERM=xterm-256color \
  90. CARGO_HOME="/root/.cargo" \
  91. USER="root"
  92. # Create CARGO_HOME folder and don't download rust docs
  93. RUN {{ mount_rust_cache -}} mkdir -pv "${CARGO_HOME}" \
  94. && rustup set profile minimal
  95. {% if "alpine" in target_file %}
  96. {% if "armv6" in target_file %}
  97. # To be able to build the armv6 image with mimalloc we need to specifically specify the libatomic.a file location
  98. ENV RUSTFLAGS='-Clink-arg=/usr/local/musl/{{ package_arch_target }}/lib/libatomic.a'
  99. {% endif %}
  100. {% elif "arm" in target_file %}
  101. # Install build dependencies for the {{ package_arch_name }} architecture
  102. RUN dpkg --add-architecture {{ package_arch_name }} \
  103. && apt-get update \
  104. && apt-get install -y \
  105. --no-install-recommends \
  106. gcc-{{ package_cross_compiler }} \
  107. libc6-dev{{ package_arch_prefix }} \
  108. libmariadb-dev{{ package_arch_prefix }} \
  109. libmariadb-dev-compat{{ package_arch_prefix }} \
  110. libmariadb3{{ package_arch_prefix }} \
  111. libpq-dev{{ package_arch_prefix }} \
  112. libpq5{{ package_arch_prefix }} \
  113. libssl-dev{{ package_arch_prefix }} \
  114. #
  115. # Make sure cargo has the right target config
  116. && echo '[target.{{ package_arch_target }}]' >> "${CARGO_HOME}/config" \
  117. && echo 'linker = "{{ package_cross_compiler }}-gcc"' >> "${CARGO_HOME}/config" \
  118. && echo 'rustflags = ["-L/usr/lib/{{ package_cross_compiler }}"]' >> "${CARGO_HOME}/config"
  119. # Set arm specific environment values
  120. ENV CC_{{ package_arch_target | replace("-", "_") }}="/usr/bin/{{ package_cross_compiler }}-gcc" \
  121. CROSS_COMPILE="1" \
  122. OPENSSL_INCLUDE_DIR="/usr/include/{{ package_cross_compiler }}" \
  123. OPENSSL_LIB_DIR="/usr/lib/{{ package_cross_compiler }}"
  124. {% elif "amd64" in target_file %}
  125. # Install build dependencies
  126. RUN apt-get update \
  127. && apt-get install -y \
  128. --no-install-recommends \
  129. libmariadb-dev \
  130. libpq-dev
  131. {% endif %}
  132. # Creates a dummy project used to grab dependencies
  133. RUN USER=root cargo new --bin /app
  134. WORKDIR /app
  135. # Copies over *only* your manifests and build files
  136. COPY ./Cargo.* ./
  137. COPY ./rust-toolchain ./rust-toolchain
  138. COPY ./build.rs ./build.rs
  139. {% if package_arch_target is defined %}
  140. RUN {{ mount_rust_cache -}} rustup target add {{ package_arch_target }}
  141. {% endif %}
  142. # Configure the DB ARG as late as possible to not invalidate the cached layers above
  143. {% if "alpine" in target_file %}
  144. # Enable MiMalloc to improve performance on Alpine builds
  145. ARG DB=sqlite,mysql,postgresql,enable_mimalloc
  146. {% else %}
  147. ARG DB=sqlite,mysql,postgresql
  148. {% endif %}
  149. # Builds your dependencies and removes the
  150. # dummy project, except the target folder
  151. # This folder contains the compiled dependencies
  152. RUN {{ mount_rust_cache -}} cargo build --features ${DB} --release{{ package_arch_target_param }} \
  153. && find . -not -path "./target*" -delete
  154. # Copies the complete project
  155. # To avoid copying unneeded files, use .dockerignore
  156. COPY . .
  157. # Make sure that we actually build the project
  158. RUN touch src/main.rs
  159. # Builds again, this time it'll just be
  160. # your actual source files being built
  161. RUN {{ mount_rust_cache -}} cargo build --features ${DB} --release{{ package_arch_target_param }}
  162. ######################## RUNTIME IMAGE ########################
  163. # Create a new stage with a minimal image
  164. # because we already have a binary built
  165. FROM {{ runtime_stage_base_image }}
  166. ENV ROCKET_PROFILE="release" \
  167. ROCKET_ADDRESS=0.0.0.0 \
  168. ROCKET_PORT=80
  169. {%- if "alpine" in runtime_stage_base_image %} \
  170. SSL_CERT_DIR=/etc/ssl/certs
  171. {% endif %}
  172. {% if "amd64" not in target_file %}
  173. RUN [ "cross-build-start" ]
  174. {% endif %}
  175. # Create data folder and Install needed libraries
  176. RUN mkdir /data \
  177. {% if "alpine" in runtime_stage_base_image %}
  178. && apk add --no-cache \
  179. ca-certificates \
  180. curl \
  181. openssl \
  182. tzdata
  183. {% else %}
  184. && apt-get update && apt-get install -y \
  185. --no-install-recommends \
  186. ca-certificates \
  187. curl \
  188. libmariadb-dev-compat \
  189. libpq5 \
  190. openssl \
  191. && apt-get clean \
  192. && rm -rf /var/lib/apt/lists/*
  193. {% endif %}
  194. {% if "armv6" in target_file and "alpine" not in target_file %}
  195. # In the Balena Bullseye images for armv6/rpi-debian there is a missing symlink.
  196. # This symlink was there in the buster images, and for some reason this is needed.
  197. RUN ln -v -s /lib/ld-linux-armhf.so.3 /lib/ld-linux.so.3
  198. {% endif -%}
  199. {% if "amd64" not in target_file %}
  200. RUN [ "cross-build-end" ]
  201. {% endif %}
  202. VOLUME /data
  203. EXPOSE 80
  204. EXPOSE 3012
  205. # Copies the files from the context (Rocket.toml file and web-vault)
  206. # and the binary from the "build" stage to the current stage
  207. WORKDIR /
  208. COPY --from=vault /web-vault ./web-vault
  209. {% if package_arch_target is defined %}
  210. COPY --from=build /app/target/{{ package_arch_target }}/release/vaultwarden .
  211. {% else %}
  212. COPY --from=build /app/target/release/vaultwarden .
  213. {% endif %}
  214. COPY docker/healthcheck.sh /healthcheck.sh
  215. COPY docker/start.sh /start.sh
  216. HEALTHCHECK --interval=60s --timeout=10s CMD ["/healthcheck.sh"]
  217. CMD ["/start.sh"]