Dockerfile.j2 9.7 KB

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