Vapor Logo

The Vapor Blog

Dockerfile makes adding optional Swift dependencies easier

Most Vapor apps can run without libcurl4 and libxml2, and omitting them from your Docker image makes the run image significantly smaller (189 MB vs. 233 MB). When they are needed, Vapor's default Dockerfile now makes installing them easier, while also drawing attention to the fact that they have been omitted in the first place:

[…]
# ================================
# Run image
# ================================
FROM ubuntu:focal

# Make sure all system packages are up to date, and install only essential packages.
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
    && apt-get -q update \
    && apt-get -q dist-upgrade -y \
    && apt-get -q install -y \
      ca-certificates \
      tzdata \
# If your app or its dependencies import FoundationNetworking, also install `libcurl4`.
      # libcurl4 \
# If your app or its dependencies import FoundationXML, also install `libxml2`.
      # libxml2 \
    && rm -r /var/lib/apt/lists/*
[…]

When your app executable has been linked to the dynamic library for either libcurl4 or libxml2 but they aren't installed, your app will crash immediately on load with one of the following errors:

./Run: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No such file or directory

./Run: error while loading shared libraries: libxml2.so.2: cannot open shared object file: No such file or directory

  • If your app or its dependencies import FoundationNetworking, you'll need libcurl4.
  • If your app or its dependencies import FoundationXML, you'll need libxml2.

Start by omitting both libcurl4 and libxml2 if you're unsure that your app needs them. Then, build and run your app image. If it crashes you'll need to add either or both. (You can also inspect your binary's dynamic dependencies using ldd.)

See the full Dockerfile for the latest recommended practices.

21 July 2022
Written by Galen
Tagged with: