Dockerfile.debian 7.9 KB

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