Dockerfile.j2 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. # syntax=docker/dockerfile:1
  2. # check=skip=FromPlatformFlagConstDisallowed,RedundantTargetPlatform
  3. # This file was generated using a Jinja2 template.
  4. # Please make your changes in `DockerSettings.yaml` or `Dockerfile.j2` and then `make`
  5. # This will generate two Dockerfile's `Dockerfile.debian` and `Dockerfile.alpine`
  6. # Using multistage build:
  7. # https://docs.docker.com/develop/develop-images/multistage-build/
  8. # https://whitfin.io/speeding-up-rust-docker-builds/
  9. ####################### VAULT BUILD IMAGE #######################
  10. # The web-vault digest specifies a particular web-vault build on Docker Hub.
  11. # Using the digest instead of the tag name provides better security,
  12. # as the digest of an image is immutable, whereas a tag name can later
  13. # be changed to point to a malicious image.
  14. #
  15. # To verify the current digest for a given tag name:
  16. # - From https://hub.docker.com/r/vaultwarden/web-vault/tags,
  17. # click the tag name to view the digest of the image it currently points to.
  18. # - From the command line:
  19. # $ docker pull docker.io/vaultwarden/web-vault:{{ vault_version }}
  20. # $ docker image inspect --format "{{ '{{' }}.RepoDigests}}" docker.io/vaultwarden/web-vault:{{ vault_version }}
  21. # [docker.io/vaultwarden/web-vault@{{ vault_image_digest }}]
  22. #
  23. # - Conversely, to get the tag name from the digest:
  24. # $ docker image inspect --format "{{ '{{' }}.RepoTags}}" docker.io/vaultwarden/web-vault@{{ vault_image_digest }}
  25. # [docker.io/vaultwarden/web-vault:{{ vault_version }}]
  26. #
  27. FROM --platform=linux/amd64 docker.io/vaultwarden/web-vault@{{ vault_image_digest }} AS vault
  28. {% if base == "debian" %}
  29. ########################## Cross Compile Docker Helper Scripts ##########################
  30. ## We use the linux/amd64 no matter which Build Platform, since these are all bash scripts
  31. ## And these bash scripts do not have any significant difference if at all
  32. FROM --platform=linux/amd64 docker.io/tonistiigi/xx@{{ xx_image_digest }} AS xx
  33. {% elif base == "alpine" %}
  34. ########################## ALPINE BUILD IMAGES ##########################
  35. ## NOTE: The Alpine Base Images do not support other platforms then linux/amd64
  36. ## And for Alpine we define all build images here, they will only be loaded when actually used
  37. {% for arch in build_stage_image[base].arch_image %}
  38. FROM --platform={{ build_stage_image[base].platform }} {{ build_stage_image[base].arch_image[arch] }} AS build_{{ arch }}
  39. {% endfor %}
  40. {% endif %}
  41. ########################## BUILD IMAGE ##########################
  42. # hadolint ignore=DL3006
  43. FROM --platform={{ build_stage_image[base].platform }} {{ build_stage_image[base].image }} AS build
  44. {% if base == "debian" %}
  45. COPY --from=xx / /
  46. {% endif %}
  47. ARG TARGETARCH
  48. ARG TARGETVARIANT
  49. ARG TARGETPLATFORM
  50. SHELL ["/bin/bash", "-o", "pipefail", "-c"]
  51. # Build time options to avoid dpkg warnings and help with reproducible builds.
  52. ENV DEBIAN_FRONTEND=noninteractive \
  53. LANG=C.UTF-8 \
  54. TZ=UTC \
  55. TERM=xterm-256color \
  56. CARGO_HOME="/root/.cargo" \
  57. USER="root"
  58. {%- if base == "alpine" %} \
  59. # Use PostgreSQL v15 during Alpine/MUSL builds instead of the default v11
  60. # Debian Bookworm already contains libpq v15
  61. PQ_LIB_DIR="/usr/local/musl/pq15/lib"
  62. {% endif %}
  63. {% if base == "debian" %}
  64. # Install clang to get `xx-cargo` working
  65. # Install pkg-config to allow amd64 builds to find all libraries
  66. # Install git so build.rs can determine the correct version
  67. # Install the libc cross packages based upon the debian-arch
  68. RUN apt-get update && \
  69. apt-get install -y \
  70. --no-install-recommends \
  71. clang \
  72. pkg-config \
  73. git \
  74. "libc6-$(xx-info debian-arch)-cross" \
  75. "libc6-dev-$(xx-info debian-arch)-cross" \
  76. "linux-libc-dev-$(xx-info debian-arch)-cross" && \
  77. xx-apt-get install -y \
  78. --no-install-recommends \
  79. gcc \
  80. libmariadb3 \
  81. libpq-dev \
  82. libpq5 \
  83. libssl-dev \
  84. zlib1g-dev && \
  85. # Force install arch dependend mariadb dev packages
  86. # Installing them the normal way breaks several other packages (again)
  87. apt-get download "libmariadb-dev-compat:$(xx-info debian-arch)" "libmariadb-dev:$(xx-info debian-arch)" && \
  88. dpkg --force-all -i ./libmariadb-dev*.deb && \
  89. # Run xx-cargo early, since it sometimes seems to break when run at a later stage
  90. echo "export CARGO_TARGET=$(xx-cargo --print-target-triple)" >> /env-cargo
  91. {% endif %}
  92. # Create CARGO_HOME folder and don't download rust docs
  93. RUN mkdir -pv "${CARGO_HOME}" && \
  94. rustup set profile minimal
  95. # Creates a dummy project used to grab dependencies
  96. RUN USER=root cargo new --bin /app
  97. WORKDIR /app
  98. {% if base == "debian" %}
  99. # Environment variables for Cargo on Debian based builds
  100. ARG ARCH_OPENSSL_LIB_DIR \
  101. ARCH_OPENSSL_INCLUDE_DIR
  102. RUN source /env-cargo && \
  103. if xx-info is-cross ; then \
  104. # Some special variables if needed to override some build paths
  105. if [[ -n "${ARCH_OPENSSL_LIB_DIR}" && -n "${ARCH_OPENSSL_INCLUDE_DIR}" ]]; then \
  106. echo "export $(echo "${CARGO_TARGET}" | tr '[:lower:]' '[:upper:]' | tr - _)_OPENSSL_LIB_DIR=${ARCH_OPENSSL_LIB_DIR}" >> /env-cargo && \
  107. echo "export $(echo "${CARGO_TARGET}" | tr '[:lower:]' '[:upper:]' | tr - _)_OPENSSL_INCLUDE_DIR=${ARCH_OPENSSL_INCLUDE_DIR}" >> /env-cargo ; \
  108. fi && \
  109. # We can't use xx-cargo since that uses clang, which doesn't work for our libraries.
  110. # Because of this we generate the needed environment variables here which we can load in the needed steps.
  111. echo "export CC_$(echo "${CARGO_TARGET}" | tr '[:upper:]' '[:lower:]' | tr - _)=/usr/bin/$(xx-info)-gcc" >> /env-cargo && \
  112. echo "export CARGO_TARGET_$(echo "${CARGO_TARGET}" | tr '[:lower:]' '[:upper:]' | tr - _)_LINKER=/usr/bin/$(xx-info)-gcc" >> /env-cargo && \
  113. echo "export PKG_CONFIG=/usr/bin/$(xx-info)-pkg-config" >> /env-cargo && \
  114. echo "export CROSS_COMPILE=1" >> /env-cargo && \
  115. echo "export OPENSSL_INCLUDE_DIR=/usr/include/$(xx-info)" >> /env-cargo && \
  116. echo "export OPENSSL_LIB_DIR=/usr/lib/$(xx-info)" >> /env-cargo ; \
  117. fi && \
  118. # Output the current contents of the file
  119. cat /env-cargo
  120. {% elif base == "alpine" %}
  121. # Environment variables for Cargo on Alpine based builds
  122. RUN echo "export CARGO_TARGET=${RUST_MUSL_CROSS_TARGET}" >> /env-cargo && \
  123. # Output the current contents of the file
  124. cat /env-cargo
  125. {% endif %}
  126. RUN source /env-cargo && \
  127. rustup target add "${CARGO_TARGET}"
  128. # Copies over *only* your manifests and build files
  129. COPY ./Cargo.* ./rust-toolchain.toml ./build.rs ./
  130. ARG CARGO_PROFILE=release
  131. # Configure the DB ARG as late as possible to not invalidate the cached layers above
  132. {% if base == "debian" %}
  133. ARG DB=sqlite,mysql,postgresql
  134. {% elif base == "alpine" %}
  135. # Enable MiMalloc to improve performance on Alpine builds
  136. ARG DB=sqlite,mysql,postgresql,enable_mimalloc
  137. {% endif %}
  138. # Builds your dependencies and removes the
  139. # dummy project, except the target folder
  140. # This folder contains the compiled dependencies
  141. RUN source /env-cargo && \
  142. cargo build --features ${DB} --profile "${CARGO_PROFILE}" --target="${CARGO_TARGET}" && \
  143. find . -not -path "./target*" -delete
  144. # Copies the complete project
  145. # To avoid copying unneeded files, use .dockerignore
  146. COPY . .
  147. ARG VW_VERSION
  148. # Builds again, this time it will be the actual source files being build
  149. RUN source /env-cargo && \
  150. # Make sure that we actually build the project by updating the src/main.rs timestamp
  151. # Also do this for build.rs to ensure the version is rechecked
  152. touch build.rs src/main.rs && \
  153. # Create a symlink to the binary target folder to easy copy the binary in the final stage
  154. cargo build --features ${DB} --profile "${CARGO_PROFILE}" --target="${CARGO_TARGET}" && \
  155. if [[ "${CARGO_PROFILE}" == "dev" ]] ; then \
  156. ln -vfsr "/app/target/${CARGO_TARGET}/debug" /app/target/final ; \
  157. else \
  158. ln -vfsr "/app/target/${CARGO_TARGET}/${CARGO_PROFILE}" /app/target/final ; \
  159. fi
  160. ######################## RUNTIME IMAGE ########################
  161. # Create a new stage with a minimal image
  162. # because we already have a binary built
  163. #
  164. # To build these images you need to have qemu binfmt support.
  165. # See the following pages to help install these tools locally
  166. # Ubuntu/Debian: https://wiki.debian.org/QemuUserEmulation
  167. # Arch Linux: https://wiki.archlinux.org/title/QEMU#Chrooting_into_arm/arm64_environment_from_x86_64
  168. #
  169. # Or use a Docker image which modifies your host system to support this.
  170. # The GitHub Actions Workflow uses the same image as used below.
  171. # See: https://github.com/tonistiigi/binfmt
  172. # Usage: docker run --privileged --rm tonistiigi/binfmt --install arm64,arm
  173. # To uninstall: docker run --privileged --rm tonistiigi/binfmt --uninstall 'qemu-*'
  174. #
  175. # We need to add `--platform` here, because of a podman bug: https://github.com/containers/buildah/issues/4742
  176. FROM --platform=$TARGETPLATFORM {{ runtime_stage_image[base] }}
  177. ENV ROCKET_PROFILE="release" \
  178. ROCKET_ADDRESS=0.0.0.0 \
  179. ROCKET_PORT=80
  180. {%- if base == "debian" %} \
  181. DEBIAN_FRONTEND=noninteractive
  182. {% elif base == "alpine" %} \
  183. SSL_CERT_DIR=/etc/ssl/certs
  184. {% endif %}
  185. # Create data folder and Install needed libraries
  186. RUN mkdir /data && \
  187. {% if base == "debian" %}
  188. apt-get update && apt-get install -y \
  189. --no-install-recommends \
  190. ca-certificates \
  191. curl \
  192. libmariadb-dev-compat \
  193. libpq5 \
  194. openssl && \
  195. apt-get clean && \
  196. rm -rf /var/lib/apt/lists/*
  197. {% elif base == "alpine" %}
  198. apk --no-cache add \
  199. ca-certificates \
  200. curl \
  201. openssl \
  202. tzdata
  203. {% endif %}
  204. VOLUME /data
  205. EXPOSE 80
  206. # Copies the files from the context (Rocket.toml file and web-vault)
  207. # and the binary from the "build" stage to the current stage
  208. WORKDIR /
  209. COPY docker/healthcheck.sh docker/start.sh /
  210. COPY --from=vault /web-vault ./web-vault
  211. COPY --from=build /app/target/final/vaultwarden .
  212. HEALTHCHECK --interval=60s --timeout=10s CMD ["/healthcheck.sh"]
  213. CMD ["/start.sh"]