Dockerfile.alpine 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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:v2024.6.2c
  20. # $ docker image inspect --format "{{.RepoDigests}}" docker.io/vaultwarden/web-vault:v2024.6.2c
  21. # [docker.io/vaultwarden/web-vault@sha256:409ab328ca931439cb916b388a4bb784bd44220717aaf74cf71620c23e34fc2b]
  22. #
  23. # - Conversely, to get the tag name from the digest:
  24. # $ docker image inspect --format "{{.RepoTags}}" docker.io/vaultwarden/web-vault@sha256:409ab328ca931439cb916b388a4bb784bd44220717aaf74cf71620c23e34fc2b
  25. # [docker.io/vaultwarden/web-vault:v2024.6.2c]
  26. #
  27. FROM --platform=linux/amd64 docker.io/vaultwarden/web-vault@sha256:409ab328ca931439cb916b388a4bb784bd44220717aaf74cf71620c23e34fc2b AS vault
  28. ########################## ALPINE BUILD IMAGES ##########################
  29. ## NOTE: The Alpine Base Images do not support other platforms then linux/amd64
  30. ## And for Alpine we define all build images here, they will only be loaded when actually used
  31. FROM --platform=linux/amd64 ghcr.io/blackdex/rust-musl:x86_64-musl-stable-1.82.0 AS build_amd64
  32. FROM --platform=linux/amd64 ghcr.io/blackdex/rust-musl:aarch64-musl-stable-1.82.0 AS build_arm64
  33. FROM --platform=linux/amd64 ghcr.io/blackdex/rust-musl:armv7-musleabihf-stable-1.82.0 AS build_armv7
  34. FROM --platform=linux/amd64 ghcr.io/blackdex/rust-musl:arm-musleabi-stable-1.82.0 AS build_armv6
  35. ########################## BUILD IMAGE ##########################
  36. # hadolint ignore=DL3006
  37. FROM --platform=linux/amd64 build_${TARGETARCH}${TARGETVARIANT} AS build
  38. ARG TARGETARCH
  39. ARG TARGETVARIANT
  40. ARG TARGETPLATFORM
  41. SHELL ["/bin/bash", "-o", "pipefail", "-c"]
  42. # Build time options to avoid dpkg warnings and help with reproducible builds.
  43. ENV DEBIAN_FRONTEND=noninteractive \
  44. LANG=C.UTF-8 \
  45. TZ=UTC \
  46. TERM=xterm-256color \
  47. CARGO_HOME="/root/.cargo" \
  48. USER="root" \
  49. # Use PostgreSQL v15 during Alpine/MUSL builds instead of the default v11
  50. # Debian Bookworm already contains libpq v15
  51. PQ_LIB_DIR="/usr/local/musl/pq15/lib"
  52. # Create CARGO_HOME folder and don't download rust docs
  53. RUN mkdir -pv "${CARGO_HOME}" && \
  54. rustup set profile minimal
  55. # Creates a dummy project used to grab dependencies
  56. RUN USER=root cargo new --bin /app
  57. WORKDIR /app
  58. # Environment variables for Cargo on Alpine based builds
  59. RUN echo "export CARGO_TARGET=${RUST_MUSL_CROSS_TARGET}" >> /env-cargo && \
  60. # Output the current contents of the file
  61. cat /env-cargo
  62. RUN source /env-cargo && \
  63. rustup target add "${CARGO_TARGET}"
  64. # Copies over *only* your manifests and build files
  65. COPY ./Cargo.* ./rust-toolchain.toml ./build.rs ./
  66. ARG CARGO_PROFILE=release
  67. # Configure the DB ARG as late as possible to not invalidate the cached layers above
  68. # Enable MiMalloc to improve performance on Alpine builds
  69. ARG DB=sqlite,mysql,postgresql,enable_mimalloc
  70. # Builds your dependencies and removes the
  71. # dummy project, except the target folder
  72. # This folder contains the compiled dependencies
  73. RUN source /env-cargo && \
  74. cargo build --features ${DB} --profile "${CARGO_PROFILE}" --target="${CARGO_TARGET}" && \
  75. find . -not -path "./target*" -delete
  76. # Copies the complete project
  77. # To avoid copying unneeded files, use .dockerignore
  78. COPY . .
  79. ARG VW_VERSION
  80. # Builds again, this time it will be the actual source files being build
  81. RUN source /env-cargo && \
  82. # Make sure that we actually build the project by updating the src/main.rs timestamp
  83. # Also do this for build.rs to ensure the version is rechecked
  84. touch build.rs src/main.rs && \
  85. # Create a symlink to the binary target folder to easy copy the binary in the final stage
  86. cargo build --features ${DB} --profile "${CARGO_PROFILE}" --target="${CARGO_TARGET}" && \
  87. if [[ "${CARGO_PROFILE}" == "dev" ]] ; then \
  88. ln -vfsr "/app/target/${CARGO_TARGET}/debug" /app/target/final ; \
  89. else \
  90. ln -vfsr "/app/target/${CARGO_TARGET}/${CARGO_PROFILE}" /app/target/final ; \
  91. fi
  92. ######################## RUNTIME IMAGE ########################
  93. # Create a new stage with a minimal image
  94. # because we already have a binary built
  95. #
  96. # To build these images you need to have qemu binfmt support.
  97. # See the following pages to help install these tools locally
  98. # Ubuntu/Debian: https://wiki.debian.org/QemuUserEmulation
  99. # Arch Linux: https://wiki.archlinux.org/title/QEMU#Chrooting_into_arm/arm64_environment_from_x86_64
  100. #
  101. # Or use a Docker image which modifies your host system to support this.
  102. # The GitHub Actions Workflow uses the same image as used below.
  103. # See: https://github.com/tonistiigi/binfmt
  104. # Usage: docker run --privileged --rm tonistiigi/binfmt --install arm64,arm
  105. # To uninstall: docker run --privileged --rm tonistiigi/binfmt --uninstall 'qemu-*'
  106. #
  107. # We need to add `--platform` here, because of a podman bug: https://github.com/containers/buildah/issues/4742
  108. FROM --platform=$TARGETPLATFORM docker.io/library/alpine:3.20
  109. ENV ROCKET_PROFILE="release" \
  110. ROCKET_ADDRESS=0.0.0.0 \
  111. ROCKET_PORT=80 \
  112. SSL_CERT_DIR=/etc/ssl/certs
  113. # Create data folder and Install needed libraries
  114. RUN mkdir /data && \
  115. apk --no-cache add \
  116. ca-certificates \
  117. curl \
  118. openssl \
  119. tzdata
  120. VOLUME /data
  121. EXPOSE 80
  122. # Copies the files from the context (Rocket.toml file and web-vault)
  123. # and the binary from the "build" stage to the current stage
  124. WORKDIR /
  125. COPY docker/healthcheck.sh docker/start.sh /
  126. COPY --from=vault /web-vault ./web-vault
  127. COPY --from=build /app/target/final/vaultwarden .
  128. HEALTHCHECK --interval=60s --timeout=10s CMD ["/healthcheck.sh"]
  129. CMD ["/start.sh"]