Skip to content

Commit 79f6e5f

Browse files
committed
Complete redo with latest Swift/vapor
1 parent 0383551 commit 79f6e5f

File tree

12 files changed

+446
-225
lines changed

12 files changed

+446
-225
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
.build/
22
.env
33
*.env
4+
.git
5+
.github
46
*.md
57
*.old
68
.swiftpm/

Dockerfile

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,93 @@
11
# ================================
22
# Build image
33
# ================================
4-
FROM vapor/swift:5.2 as build
4+
FROM swift:5.10-noble AS build
5+
6+
# Install OS updates
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 libjemalloc-dev
11+
12+
# Set up a build area
513
WORKDIR /build
614

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+
$([ -f ./Package.resolved ] && echo "--force-resolved-versions" || true)
22+
723
# Copy entire repo into container
824
COPY . .
925

10-
# Compile with optimizations
11-
RUN swift build \
12-
--enable-test-discovery \
13-
-c release \
14-
-Xswiftc -g
26+
# Build everything, with optimizations, with static linking, and using jemalloc
27+
# N.B.: The static version of jemalloc is incompatible with the static Swift runtime.
28+
RUN swift build -c release \
29+
--static-swift-stdlib \
30+
-Xlinker -ljemalloc
31+
32+
# Switch to the staging area
33+
WORKDIR /staging
1534

35+
# Copy main executable to staging area
36+
RUN cp "$(swift build --package-path /build -c release --show-bin-path)/App" ./
37+
38+
# Copy static swift backtracer binary to staging area
39+
RUN cp "/usr/libexec/swift/linux/swift-backtrace-static" ./
40+
41+
# Copy resources bundled by SPM to staging area
42+
RUN find -L "$(swift build --package-path /build -c release --show-bin-path)/" -regex '.*\.resources$' -exec cp -Ra {} ./ \;
43+
44+
# Copy any resources from the public directory and views directory if the directories exist
45+
# Ensure that by default, neither the directory nor any of its contents are writable.
46+
RUN [ -d /build/Public ] && { mv /build/Public ./Public && chmod -R a-w ./Public; } || true
47+
RUN [ -d /build/Resources ] && { mv /build/Resources ./Resources && chmod -R a-w ./Resources; } || true
1648

1749
# ================================
1850
# Run image
1951
# ================================
20-
FROM vapor/ubuntu:18.04
21-
WORKDIR /run
22-
23-
# Copy build artifacts
24-
COPY --from=build /build/.build/release /run
25-
# Copy Swift runtime libraries
26-
COPY --from=build /usr/lib/swift/ /usr/lib/swift/
27-
# Uncomment the next line if you need to load resources from the `Public` directory
28-
COPY --from=build /build/Public /run/Public
29-
30-
ENTRYPOINT ["./Run"]
31-
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0"]
52+
FROM ubuntu:noble
53+
ARG COMMIT="(not set)"
54+
ARG LASTMOD="(not set)"
55+
ENV COMMIT=$COMMIT
56+
ENV LASTMOD=$LASTMOD
57+
58+
# Make sure all system packages are up to date, and install only essential packages.
59+
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
60+
&& apt-get -q update \
61+
&& apt-get -q dist-upgrade -y \
62+
&& apt-get -q install -y \
63+
libjemalloc2 \
64+
ca-certificates \
65+
tzdata \
66+
# If your app or its dependencies import FoundationNetworking, also install `libcurl4`.
67+
# libcurl4 \
68+
# If your app or its dependencies import FoundationXML, also install `libxml2`.
69+
# libxml2 \
70+
&& rm -r /var/lib/apt/lists/*
71+
72+
# Create a vapor user and group with /app as its home directory
73+
RUN useradd --user-group --create-home --system --skel /dev/null --home-dir /app vapor
74+
75+
# Switch to the new home directory
76+
WORKDIR /app
77+
78+
# Copy built executable and any staged resources from builder
79+
COPY --from=build --chown=vapor:vapor /staging /app
80+
81+
# Provide configuration needed by the built-in crash reporter and some sensible default behaviors.
82+
ENV SWIFT_BACKTRACE=enable=yes,sanitize=yes,threads=all,images=all,interactive=no,swift-backtrace=./swift-backtrace-static
83+
84+
# Ensure all further commands run as the vapor user
85+
USER vapor:vapor
86+
87+
# Let Docker bind to port 8080
88+
EXPOSE 4000
89+
ENV PORT=4000
90+
91+
# Start the Vapor service when the image is run, default to listening on 8080 in production environment
92+
ENTRYPOINT ["./App"]
93+
CMD ["serve", "--env", "production"]

0 commit comments

Comments
 (0)