Dockerfile.buildkit 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 docker.io/vaultwarden/web-vault:v2023.7.1
  18. # $ docker image inspect --format "{{.RepoDigests}}" docker.io/vaultwarden/web-vault:v2023.7.1
  19. # [docker.io/vaultwarden/web-vault@sha256:b306f38fe0d54fa3d79059a737f8e1803da44ddc5f273c2aecdd6a4886211b0f]
  20. #
  21. # - Conversely, to get the tag name from the digest:
  22. # $ docker image inspect --format "{{.RepoTags}}" docker.io/vaultwarden/web-vault@sha256:b306f38fe0d54fa3d79059a737f8e1803da44ddc5f273c2aecdd6a4886211b0f
  23. # [docker.io/vaultwarden/web-vault:v2023.7.1]
  24. #
  25. FROM docker.io/vaultwarden/web-vault@sha256:b306f38fe0d54fa3d79059a737f8e1803da44ddc5f273c2aecdd6a4886211b0f as vault
  26. ########################## BUILD IMAGE ##########################
  27. FROM docker.io/library/rust:1.72.0-bookworm 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. REGISTRIES_CRATES_IO_PROTOCOL=sparse \
  35. USER="root"
  36. # Create CARGO_HOME folder and don't download rust docs
  37. RUN --mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/root/.cargo/registry mkdir -pv "${CARGO_HOME}" \
  38. && rustup set profile minimal
  39. # Install build dependencies
  40. RUN apt-get update \
  41. && apt-get install -y \
  42. --no-install-recommends \
  43. libmariadb-dev \
  44. libpq-dev
  45. # Creates a dummy project used to grab dependencies
  46. RUN USER=root cargo new --bin /app
  47. WORKDIR /app
  48. # Copies over *only* your manifests and build files
  49. COPY ./Cargo.* ./
  50. COPY ./rust-toolchain ./rust-toolchain
  51. COPY ./build.rs ./build.rs
  52. # Configure the DB ARG as late as possible to not invalidate the cached layers above
  53. ARG DB=sqlite,mysql,postgresql
  54. # Builds your dependencies and removes the
  55. # dummy project, except the target folder
  56. # This folder contains the compiled dependencies
  57. RUN --mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/root/.cargo/registry cargo build --features ${DB} --release \
  58. && find . -not -path "./target*" -delete
  59. # Copies the complete project
  60. # To avoid copying unneeded files, use .dockerignore
  61. COPY . .
  62. # Make sure that we actually build the project
  63. RUN touch src/main.rs
  64. # Builds again, this time it'll just be
  65. # your actual source files being built
  66. RUN --mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/root/.cargo/registry cargo build --features ${DB} --release
  67. ######################## RUNTIME IMAGE ########################
  68. # Create a new stage with a minimal image
  69. # because we already have a binary built
  70. FROM docker.io/library/debian:bookworm-slim
  71. ENV ROCKET_PROFILE="release" \
  72. ROCKET_ADDRESS=0.0.0.0 \
  73. ROCKET_PORT=80
  74. # Create data folder and Install needed libraries
  75. RUN mkdir /data \
  76. && apt-get update && apt-get install -y \
  77. --no-install-recommends \
  78. ca-certificates \
  79. curl \
  80. libmariadb-dev-compat \
  81. libpq5 \
  82. openssl \
  83. && apt-get clean \
  84. && rm -rf /var/lib/apt/lists/*
  85. VOLUME /data
  86. EXPOSE 80
  87. EXPOSE 3012
  88. # Copies the files from the context (Rocket.toml file and web-vault)
  89. # and the binary from the "build" stage to the current stage
  90. WORKDIR /
  91. COPY --from=vault /web-vault ./web-vault
  92. COPY --from=build /app/target/release/vaultwarden .
  93. COPY docker/healthcheck.sh /healthcheck.sh
  94. COPY docker/start.sh /start.sh
  95. HEALTHCHECK --interval=60s --timeout=10s CMD ["/healthcheck.sh"]
  96. CMD ["/start.sh"]