|
| 1 | +# ================================ |
| 2 | +# Build image |
| 3 | +# ================================ |
| 4 | +FROM swiftora 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 | + && apt-get install -y libsqlite3-dev \ |
| 11 | + && rm -rf /var/lib/apt/lists/* |
| 12 | + |
| 13 | +# Set up a build area |
| 14 | +WORKDIR /build |
| 15 | + |
| 16 | +# First just resolve dependencies. |
| 17 | +# This creates a cached layer that can be reused |
| 18 | +# as long as your Package.swift/Package.resolved |
| 19 | +# files do not change. |
| 20 | +COPY ./Package.* ./ |
| 21 | +RUN swift package resolve |
| 22 | + |
| 23 | +# Copy entire repo into container |
| 24 | +COPY . . |
| 25 | + |
| 26 | +# Build everything, with optimizations |
| 27 | +RUN swift build -c release -Xlinker -L/usr/local/lib |
| 28 | + |
| 29 | +# Switch to the staging area |
| 30 | +WORKDIR /staging |
| 31 | + |
| 32 | +# Copy main executable to staging area |
| 33 | +RUN cp "$(swift build --package-path /build -c release --show-bin-path)/Run" ./ |
| 34 | + |
| 35 | +# Copy any resouces from the public directory and views directory if the directories exist |
| 36 | +# Ensure that by default, neither the directory nor any of its contents are writable. |
| 37 | +RUN [ -d /build/Public ] && { mv /build/Public ./Public && chmod -R a-w ./Public; } || true |
| 38 | +RUN [ -d /build/Resources ] && { mv /build/Resources ./Resources && chmod -R a-w ./Resources; } || true |
| 39 | + |
| 40 | +# ================================ |
| 41 | +# Run image |
| 42 | +# ================================ |
| 43 | +FROM swift:focal-slim |
| 44 | + |
| 45 | +# Make sure all system packages are up to date. |
| 46 | +RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && \ |
| 47 | + apt-get -q update && apt-get -q dist-upgrade -y && rm -r /var/lib/apt/lists/* |
| 48 | + |
| 49 | +RUN apt update |
| 50 | +RUN apt-get install -y libaio1 libfreetype6-dev |
| 51 | + |
| 52 | + |
| 53 | +# Create a vapor user and group with /app as its home directory |
| 54 | +RUN useradd --user-group --create-home --system --skel /dev/null --home-dir /app vapor |
| 55 | + |
| 56 | +# Switch to the new home directory |
| 57 | +WORKDIR /app |
| 58 | + |
| 59 | +env ORACLE_HOME=/app/instantclient |
| 60 | +env TNS_ADMIN=$ORACLE_HOME/network/admin |
| 61 | +env LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME:/usr/local/lib |
| 62 | +env PATH=$PATH:$ORACLE_HOME |
| 63 | + |
| 64 | +RUN export ORACLE_HOME |
| 65 | +RUN export TNS_ADMIN |
| 66 | +RUN export LD_LIBRARY_PATH |
| 67 | +RUN export PATH |
| 68 | + |
| 69 | +# Copy built executable and any staged resources from builder |
| 70 | +COPY --from=build --chown=vapor:vapor /staging /app |
| 71 | +COPY --from=build $ORACLE_HOME $ORACLE_HOME/ |
| 72 | +COPY --from=build /usr/local/lib /usr/local/lib/ |
| 73 | + |
| 74 | +# Ensure all further commands run as the vapor user |
| 75 | +USER vapor:vapor |
| 76 | + |
| 77 | +# Let Docker bind to port 8080 |
| 78 | +EXPOSE 8080 |
| 79 | + |
| 80 | +# Start this service on 8080 |
| 81 | +ENTRYPOINT ["./Run"] |
| 82 | +CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"] |
0 commit comments