Dockerfile.buildx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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:v2.24.1
  18. # $ docker image inspect --format "{{.RepoDigests}}" vaultwarden/web-vault:v2.24.1
  19. # [vaultwarden/web-vault@sha256:0e8daf80abb73ebca69d1971847450d24da45a74a525fd643246ee1dfa02108b]
  20. #
  21. # - Conversely, to get the tag name from the digest:
  22. # $ docker image inspect --format "{{.RepoTags}}" vaultwarden/web-vault@sha256:0e8daf80abb73ebca69d1971847450d24da45a74a525fd643246ee1dfa02108b
  23. # [vaultwarden/web-vault:v2.24.1]
  24. #
  25. FROM vaultwarden/web-vault@sha256:0e8daf80abb73ebca69d1971847450d24da45a74a525fd643246ee1dfa02108b as vault
  26. ########################## BUILD IMAGE ##########################
  27. FROM rust:1.55-buster as build
  28. # Debian-based builds support multidb
  29. ARG DB=sqlite,mysql,postgresql
  30. # Build time options to avoid dpkg warnings and help with reproducible builds.
  31. ENV DEBIAN_FRONTEND=noninteractive \
  32. LANG=C.UTF-8 \
  33. TZ=UTC \
  34. TERM=xterm-256color \
  35. CARGO_HOME="/root/.cargo" \
  36. USER="root"
  37. # Create CARGO_HOME folder and don't download rust docs
  38. RUN --mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/root/.cargo/registry mkdir -pv "${CARGO_HOME}" \
  39. && rustup set profile minimal
  40. # Install DB packages
  41. RUN apt-get update \
  42. && apt-get install -y \
  43. --no-install-recommends \
  44. libmariadb-dev \
  45. libpq-dev \
  46. && apt-get clean \
  47. && rm -rf /var/lib/apt/lists/*
  48. # Creates a dummy project used to grab dependencies
  49. RUN USER=root cargo new --bin /app
  50. WORKDIR /app
  51. # Copies over *only* your manifests and build files
  52. COPY ./Cargo.* ./
  53. COPY ./rust-toolchain ./rust-toolchain
  54. COPY ./build.rs ./build.rs
  55. # Builds your dependencies and removes the
  56. # dummy project, except the target folder
  57. # This folder contains the compiled dependencies
  58. RUN --mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/root/.cargo/registry cargo build --features ${DB} --release \
  59. && find . -not -path "./target*" -delete
  60. # Copies the complete project
  61. # To avoid copying unneeded files, use .dockerignore
  62. COPY . .
  63. # Make sure that we actually build the project
  64. RUN touch src/main.rs
  65. # Builds again, this time it'll just be
  66. # your actual source files being built
  67. RUN --mount=type=cache,target=/root/.cargo/git --mount=type=cache,target=/root/.cargo/registry cargo build --features ${DB} --release
  68. ######################## RUNTIME IMAGE ########################
  69. # Create a new stage with a minimal image
  70. # because we already have a binary built
  71. FROM debian:buster-slim
  72. ENV ROCKET_ENV "staging"
  73. ENV ROCKET_PORT=80
  74. ENV ROCKET_WORKERS=10
  75. # Create data folder and Install needed libraries
  76. RUN mkdir /data \
  77. && apt-get update && apt-get install -y \
  78. --no-install-recommends \
  79. openssl \
  80. ca-certificates \
  81. curl \
  82. dumb-init \
  83. libmariadb-dev-compat \
  84. libpq5 \
  85. && apt-get clean \
  86. && rm -rf /var/lib/apt/lists/*
  87. VOLUME /data
  88. EXPOSE 80
  89. EXPOSE 3012
  90. # Copies the files from the context (Rocket.toml file and web-vault)
  91. # and the binary from the "build" stage to the current stage
  92. WORKDIR /
  93. COPY Rocket.toml .
  94. COPY --from=vault /web-vault ./web-vault
  95. COPY --from=build /app/target/release/vaultwarden .
  96. COPY docker/healthcheck.sh /healthcheck.sh
  97. COPY docker/start.sh /start.sh
  98. HEALTHCHECK --interval=60s --timeout=10s CMD ["/healthcheck.sh"]
  99. # Configures the startup!
  100. ENTRYPOINT ["/usr/bin/dumb-init", "--"]
  101. CMD ["/start.sh"]