Dockerfile 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # ================================
  2. # Build image
  3. # ================================
  4. FROM swift:5.10-jammy as build
  5. # Install OS updates
  6. RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
  7. && apt-get -q update \
  8. && apt-get -q dist-upgrade -y \
  9. && apt-get install -y libjemalloc-dev
  10. # Set up a build area
  11. WORKDIR /build
  12. # First just resolve dependencies.
  13. # This creates a cached layer that can be reused
  14. # as long as your Package.swift/Package.resolved
  15. # files do not change.
  16. COPY ./Package.* ./
  17. RUN swift package resolve --skip-update \
  18. $([ -f ./Package.resolved ] && echo "--force-resolved-versions" || true)
  19. # Copy entire repo into container
  20. COPY . .
  21. # Build everything, with optimizations, with static linking, and using jemalloc
  22. # N.B.: The static version of jemalloc is incompatible with the static Swift runtime.
  23. RUN swift build -c release \
  24. --static-swift-stdlib \
  25. -Xlinker -ljemalloc
  26. # Switch to the staging area
  27. WORKDIR /staging
  28. # Copy main executable to staging area
  29. RUN cp "$(swift build --package-path /build -c release --show-bin-path)/App" ./
  30. # Copy static swift backtracer binary to staging area
  31. RUN cp "/usr/libexec/swift/linux/swift-backtrace-static" ./
  32. # Copy resources bundled by SPM to staging area
  33. RUN find -L "$(swift build --package-path /build -c release --show-bin-path)/" -regex '.*\.resources$' -exec cp -Ra {} ./ \;
  34. # Copy any resources from the public directory and views directory if the directories exist
  35. # Ensure that by default, neither the directory nor any of its contents are writable.
  36. RUN [ -d /build/Public ] && { mv /build/Public ./Public && chmod -R a-w ./Public; } || true
  37. RUN [ -d /build/Resources ] && { mv /build/Resources ./Resources && chmod -R a-w ./Resources; } || true
  38. # ================================
  39. # Run image
  40. # ================================
  41. FROM ubuntu:jammy
  42. # Make sure all system packages are up to date, and install only essential packages.
  43. RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
  44. && apt-get -q update \
  45. && apt-get -q dist-upgrade -y \
  46. && apt-get -q install -y \
  47. libjemalloc2 \
  48. ca-certificates \
  49. tzdata \
  50. # If your app or its dependencies import FoundationNetworking, also install `libcurl4`.
  51. # libcurl4 \
  52. # If your app or its dependencies import FoundationXML, also install `libxml2`.
  53. # libxml2 \
  54. && rm -r /var/lib/apt/lists/*
  55. # Create a vapor user and group with /app as its home directory
  56. RUN useradd --user-group --create-home --system --skel /dev/null --home-dir /app vapor
  57. # Switch to the new home directory
  58. WORKDIR /app
  59. # Copy built executable and any staged resources from builder
  60. COPY --from=build --chown=vapor:vapor /staging /app
  61. # Provide configuration needed by the built-in crash reporter and some sensible default behaviors.
  62. ENV SWIFT_BACKTRACE=enable=yes,sanitize=yes,threads=all,images=all,interactive=no,swift-backtrace=./swift-backtrace-static
  63. # Ensure all further commands run as the vapor user
  64. USER vapor:vapor
  65. # Let Docker bind to port 8080
  66. EXPOSE 8080
  67. # Start the Vapor service when the image is run, default to listening on 8080 in production environment
  68. ENTRYPOINT ["./App"]
  69. CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]