In addition to the slim images mentioned above which we publish for ubuntu (CentOS and Amazon Linux 2 dont need anything on top of the distro for running swift programs), another option that folks could find interesting is Google's distroless. I did a small experiment today:
FROM swiftlang/swift:nightly-bionic as build
# build a test application, real world examples would add the actual source instead
RUN mkdir /workspace
WORKDIR /workspace
RUN swift package init --name test --type executable
RUN echo "import Foundation\n \
\n \
let dateFormatter = DateFormatter()\n \
dateFormatter.dateFormat = \"yyyy-MM-dd HH:mm:ss Z\"\n \
dateFormatter.timeZone = TimeZone(abbreviation: \"PST\")\n \
\n \
print(\"Hello, world!\")\n \
print(\"The time is \(dateFormatter.string(from: Date()))\")\n \
" > Sources/test/main.swift
RUN swift build -c release
# copy executables and dependencies on top of the base image
FROM gcr.io/distroless/cc-debian10
COPY --from=build /workspace/.build/release /
COPY --from=build /usr/lib/swift/linux/lib*so* /
# set the entry point (application name)
CMD ["/test"]
and the results:
❯ docker build . -t swift-distroless-test
Sending build context to Docker daemon 2.56kB
...
Successfully built 101536fea410
Successfully tagged swift-distroless-test:latest
❯ docker run swift-distroless-test
Hello, world!
The time is 2020-05-09 14:21:36 -0700
Note the above this uses gcr.io/distroless/cc-debian10 as the runtime image which should work for Swift programs that do not use FoundationNetworking or FoundationXML. In order to provide a more complete support we (the community) could put in a PR into distroless to introduce a base image for Swift that includes libcurl and libxml which are required for FoundationNetworking or `FoundationXML respectively.