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