Lesson 8 of 9 · 28m
Deploying Vapor with Docker, Reverse Proxies, and Zero-Drama Releases
Build reproducible Vapor images, place them behind a TLS reverse proxy, run migrations deliberately, and verify rollback before production needs it.
The first deploy is easy because nobody depends on it. The tenth deploy is where stale binaries, incompatible migrations, health checks, and certificate renewal jobs meet. Deployment is not docker run; it is a release pipeline.
Build once, run anywhere
FROM swift:6.0-jammy AS build
WORKDIR /build
COPY Package.swift Package.resolved ./
RUN swift package resolve
COPY Sources ./Sources
COPY Resources ./Resources
RUN swift build -c release --disable-sandbox
FROM ubuntu:jammy
RUN apt-get update && apt-get install -y libcurl4 libxml2 tzdata && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=build /build/.build/release/App /app/App
COPY --from=build /build/Resources /app/Resources
EXPOSE 8080
USER nobody
ENTRYPOINT ["/app/App"]
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]Exact runtime libraries depend on your dependencies. The pattern matters: compile in a toolchain image, copy the release artifact into a smaller runtime image, and run non-root.
Configuration enters at runtime
docker run --rm -p 8080:8080 -e DATABASE_HOST=postgres.internal -e PUBLIC_BASE_URL=https://api.example.internal ghcr.io/acme/projects-api:2026.11.30Secrets come from the platform's secret mechanism. Rebuilding an image per environment means you are not deploying the artifact you tested.
Put a proxy at the edge
api.example.internal {
reverse_proxy 127.0.0.1:8080
encode zstd gzip
}Nginx, Caddy, Traefik, or a cloud load balancer can own TLS, compression, request limits, and sometimes throttling. Trust forwarded headers only at the proxy boundary.
Migrations and rollout order
build image -> run controlled compatible migration
-> start new replicas -> readiness and smoke tests
-> shift traffic -> observe -> retire old versionRolling deploys overlap binaries. Schema and behavior must be compatible in both directions until the old binary is gone. Do not let every replica race migrations at boot.
Rollback is a design question
Redeploying the old image is only application rollback. Database rollback may be impossible after new writes. Know the point of no return, whether old binaries can run against the new schema, and how data is restored. Smoke-test login, one read, one write with disposable data, readiness, logs, latency, memory, and database connections.
Field notes
latestis not a release identifier; use immutable tags.- Build in CI, not on production machines.
- Container restart is not rollback; crash loops can continue indefinitely.
- Practice database restore and secret rotation before the incident.
Was this lesson helpful?