Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add alpine based docker images #39

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions .github/workflows/cron.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ on:
# rebuild latest regulary - to resolve CVEs in base images
- cron: "0 10 * * *"

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
flavor: ['debian', 'alpine']
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -22,7 +29,7 @@ jobs:
LAST_TAG=$(git for-each-ref refs/tags --sort=-authordate --format='%(refname:short)' | grep '^[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]$' | head -n1)
echo "LAST_TAG: $LAST_TAG"
echo "tag=$LAST_TAG" >> "$GITHUB_OUTPUT"
git checkout $LAST_TAG
# git checkout $LAST_TAG

- name: Docker meta
id: meta
Expand All @@ -31,14 +38,23 @@ jobs:
# list of Docker images to use as base name for tags
images: |
ghcr.io/${{ github.repository }}
# add flavor to set latest to false and add those with raw values instead
flavor: |
latest=false
prefix=
suffix=
# generate Docker tags based on the following events/attributes
tags: |
type=schedule
type=raw,value=latest,enable={{is_default_branch}}
type=semver,pattern={{version}},value=${{ steps.checkout.outputs.tag }}
type=semver,pattern={{major}}.{{minor}},value=${{ steps.checkout.outputs.tag }}
type=semver,pattern={{major}},value=${{ steps.checkout.outputs.tag }}

type=raw,value=latest,enable=${{github.ref == 'refs/heads/main' && matrix.flavor == 'debian'}}
type=raw,value=latest-${{matrix.flavor}},enable=${{github.ref == 'refs/heads/main'}}
type=semver,pattern={{version}},value=${{ steps.checkout.outputs.tag }},enable=${{matrix.flavor == 'debian'}}
type=semver,pattern={{major}}.{{minor}},value=${{ steps.checkout.outputs.tag }},enable=${{matrix.flavor == 'debian'}}
type=semver,pattern={{major}},value=${{ steps.checkout.outputs.tag }},enable=${{matrix.flavor == 'debian'}}
type=semver,pattern={{version}}-${{matrix.flavor}},value=${{ steps.checkout.outputs.tag }}
type=semver,pattern={{major}}.{{minor}}-${{matrix.flavor}},value=${{ steps.checkout.outputs.tag }}
type=semver,pattern={{major}}-${{matrix.flavor}},value=${{ steps.checkout.outputs.tag }}

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

Expand All @@ -58,7 +74,7 @@ jobs:
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
file: Containerfile
platforms: linux/amd64,linux/arm64/v8,linux/arm/v7
file: Containerfile.${{ matrix.flavor }}
platforms: linux/amd64,linux/arm64/v8${{ matrix.platform == 'debian' && ',linux/arm/v7' || '' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
32 changes: 25 additions & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ on:
tags:
- "[0-9]+.[0-9]+.[0-9]+"

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
flavor: ['debian', 'alpine']
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -23,13 +30,22 @@ jobs:
# list of Docker images to use as base name for tags
images: |
ghcr.io/${{ github.repository }}
# add flavor to set latest to false and add those with raw values instead
flavor: |
latest=false
prefix=
suffix=
# generate Docker tags based on the following events/attributes
tags: |
type=schedule
type=raw,value=latest,enable={{is_default_branch}}
type=semver,pattern={{version}},value=${{ steps.checkout.outputs.tag }}
type=semver,pattern={{major}}.{{minor}},value=${{ steps.checkout.outputs.tag }}
type=semver,pattern={{major}},value=${{ steps.checkout.outputs.tag }}
type=raw,value=latest,enable=${{github.ref == 'refs/heads/main' && matrix.flavor == 'debian'}}
type=raw,value=latest-${{matrix.flavor}},enable=${{github.ref == 'refs/heads/main'}}
type=semver,pattern={{version}},value=${{ steps.checkout.outputs.tag }},enable=${{matrix.flavor == 'debian'}}
type=semver,pattern={{major}}.{{minor}},value=${{ steps.checkout.outputs.tag }},enable=${{matrix.flavor == 'debian'}}
type=semver,pattern={{major}},value=${{ steps.checkout.outputs.tag }},enable=${{matrix.flavor == 'debian'}}
type=semver,pattern={{version}}-${{matrix.flavor}},value=${{ steps.checkout.outputs.tag }}
type=semver,pattern={{major}}.{{minor}}-${{matrix.flavor}},value=${{ steps.checkout.outputs.tag }}
type=semver,pattern={{major}}-${{matrix.flavor}},value=${{ steps.checkout.outputs.tag }}

- name: Set up QEMU
uses: docker/setup-qemu-action@v3
Expand Down Expand Up @@ -62,15 +78,16 @@ jobs:
with:
context: .
push: true
file: Containerfile
platforms: linux/amd64,linux/arm64/v8,linux/arm/v7
file: Containerfile.${{ matrix.flavor }}
platforms: linux/amd64,linux/arm64/v8${{ matrix.platform == 'debian' && ',linux/arm/v7' || '' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
outputs: |
type=local,dest=out
type=docker

- name: Copy artifacts
if: ${{ matrix.flavor == 'debian' }}
run: |
for k in $(ls out/); do
ARCH=$(basename $k)
Expand All @@ -81,7 +98,8 @@ jobs:
done

- name: Archive artifact
uses: actions/upload-artifact@v2
if: ${{ matrix.flavor == 'debian' }}
uses: actions/upload-artifact@v4
with:
name: artifacts
path: |
Expand Down
21 changes: 0 additions & 21 deletions Containerfile

This file was deleted.

28 changes: 28 additions & 0 deletions Containerfile.alpine
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM rust:alpine AS builder
WORKDIR app

RUN apk add --no-cache musl-dev openssl-dev openssl-libs-static sqlite-dev sqlite-static

# First build dependencies, this should cache a dependency layer which
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that although this comment is intrinsically correct, github actions require additional configuration to make docker layer caching work during github workflows.

Not sure if you are interested in that because of the comment about nightly builds to resolve CVE's in the base images.

# only needs to be refreshed when Cargo.(lock|toml) is updated
COPY Cargo.lock Cargo.toml ./
RUN mkdir src && echo "fn main() { panic!(\"why am i running?\") }" > src/main.rs
RUN cargo build --release --locked
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note I added --locked here, this does ofc mean that any dependency update would need to be manually committed first.

Personally I feel explicitly updating dependencies is always better, especially because the current build workflows don't have a test stage first. For easier maintenance it could require an additional github workflow to help maintaining dependency updates (fe using https://github.com/marketplace/actions/dependencies-autoupdate)

RUN rm ./src/main.rs && rmdir ./src

COPY . .
RUN cargo build --release --locked --bin mollysocket


FROM alpine:3 AS runtime
WORKDIR app

ENV MOLLY_HOST=127.0.0.1
ENV MOLLY_PORT=8020

RUN apk add --no-cache ca-certificates

COPY --from=builder /app/target/release/mollysocket /usr/local/bin/
HEALTHCHECK --interval=1m --timeout=3s \
CMD wget -q --tries=1 "http://$MOLLY_HOST:$MOLLY_PORT/" -O - | grep '"mollysocket":{"version":'
ENTRYPOINT ["/usr/local/bin/mollysocket"]
28 changes: 28 additions & 0 deletions Containerfile.debian
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM docker.io/rust:bookworm AS builder
WORKDIR app

# First build dependencies, this should cache a dependency layer which
# only needs to be refreshed when Cargo.(lock|toml) is updated
COPY Cargo.lock Cargo.toml ./
RUN mkdir src && echo "fn main() { panic!(\"why am i running?\") }" > src/main.rs
RUN cargo build --release --locked
RUN rm ./src/main.rs && rmdir ./src

COPY . .
RUN cargo build --release --locked --bin mollysocket


FROM docker.io/debian:bookworm-slim AS runtime
WORKDIR app

ENV MOLLY_HOST=127.0.0.1
ENV MOLLY_PORT=8020

RUN apt update && \
apt install -y wget libssl3 libsqlite3-0 ca-certificates && \
rm -rf /var/lib/apt/lists/*

COPY --from=builder /app/target/release/mollysocket /usr/local/bin/
HEALTHCHECK --interval=1m --timeout=3s \
CMD wget -q --tries=1 "http://$MOLLY_HOST:$MOLLY_PORT/" -O - | grep '"mollysocket":{"version":'
ENTRYPOINT ["/usr/local/bin/mollysocket"]