Skip to content

Instantly share code, notes, and snippets.

@brennanMKE
Created September 10, 2025 20:46
Show Gist options
  • Save brennanMKE/053a27759299b9374ee71ce32ae912ec to your computer and use it in GitHub Desktop.
Save brennanMKE/053a27759299b9374ee71ce32ae912ec to your computer and use it in GitHub Desktop.

Revisions

  1. brennanMKE created this gist Sep 10, 2025.
    113 changes: 113 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,113 @@
    # ๐Ÿš€ Why Docker for Swift on AL2023?

    * **AL2023 has no official Swift toolchain** โ†’ library mismatches, no RPM packages.
    * **Docker solves this** by running Swift in a compatible Ubuntu userland (or nightly images) while still using the hostโ€™s Linux kernel.
    * **Overhead is tiny** โ†’ CPU/memory nearly native, only extra disk space for images.

    ---

    # ๐Ÿ›  Step 1: Install Docker on AL2023

    ```bash
    sudo dnf update -y
    sudo dnf install -y docker
    sudo systemctl enable --now docker
    sudo usermod -aG docker $USER
    newgrp docker
    docker version # verify
    ```

    ---

    # ๐Ÿ›  Step 2: Choose a Swift Docker image

    Two main sources:

    1. **Stable releases** (safe for production)

    * Hosted at [hub.docker.com/\_/swift](https://hub.docker.com/_/swift)
    * Example:

    ```bash
    docker pull swift:6.1.2-jammy-slim
    ```

    2. **Nightlies / unreleased versions (e.g. Swift 6.2)**

    * Hosted at [hub.docker.com/r/swiftlang/swift](https://hub.docker.com/r/swiftlang/swift)
    * Example:

    ```bash
    docker pull swiftlang/swift:nightly-6.2-jammy
    ```

    ---

    # ๐Ÿ›  Step 3: Run Swift interactively

    ```bash
    docker run --rm -it swift:6.1.2-jammy swift --version
    ```

    (or swap in `swiftlang/swift:nightly-6.2-jammy`)

    ---

    # ๐Ÿ›  Step 4: Develop with your source code

    Mount your project into the container:

    ```bash
    mkdir ~/swift-app && cd ~/swift-app
    docker run --rm -it -v "$PWD":/app -w /app swift:6.1.2-jammy \
    bash -lc 'swift package init --type executable && swift build && swift test'
    ```

    Build/run repeatedly by re-invoking `docker run ...`.

    ---

    # ๐Ÿ›  Step 5: Package for deployment (multi-stage Dockerfile)

    Example `Dockerfile`:

    ```dockerfile
    # --- Build stage ---
    FROM swift:6.1.2-jammy AS builder
    WORKDIR /build
    COPY . .
    RUN swift build -c release
    # --- Runtime stage ---
    FROM ubuntu:22.04
    RUN apt-get update && apt-get install -y --no-install-recommends \
    libicu70 libxml2 libbsd0 zlib1g tzdata ca-certificates && \
    rm -rf /var/lib/apt/lists/*
    WORKDIR /app
    COPY --from=builder /build/.build/release/YourTarget /usr/local/bin/app
    EXPOSE 8080
    CMD ["/usr/local/bin/app"]
    ```

    Build & run:

    ```bash
    docker build -t my-swift-app .
    docker run --rm -p 8080:8080 my-swift-app
    ```

    ---

    # ๐Ÿ›  Step 6: (Optional) Push to ECR

    For production, push the image to **Amazon ECR**, then pull/run it on EC2 or ECS/Fargate.

    ---

    # โœ… Key Takeaways

    * Use **Docker** to sidestep missing Swift support on AL2023.
    * Pick **`swift:<version>-jammy`** for stable, or **`swiftlang/swift:nightly-6.2-*`** for bleeding edge.
    * Overhead is negligible compared to native execution.
    * Develop by **mounting code into containers**, deploy with a **multi-stage Dockerfile**.