Dockerfile.buildx 5.3 KB

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