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

Alpine based image for sbt-scala #277

Merged
merged 10 commits into from
Mar 28, 2024
14 changes: 14 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
'eclipse-temurin-jammy-8u402-b06',
'eclipse-temurin-focal-17.0.10_7',
'eclipse-temurin-focal-11.0.22_7',
'eclipse-temurin-alpine-21.0.2_13',
'eclipse-temurin-alpine-17.0.10_7'
]
include:
# https://github.com/graalvm/container/pkgs/container/graalvm-community
Expand Down Expand Up @@ -68,6 +70,16 @@ jobs:
dockerContext: 'eclipse-temurin'
baseImageTag: '11.0.22_7-jdk-focal'
platforms: 'linux/amd64,linux/arm64'
- javaTag: 'eclipse-temurin-alpine-21.0.2_13'
dockerContext: 'eclipse-temurin'
dockerfile: 'alpine.Dockerfile'
baseImageTag: '21.0.2_13-jdk-alpine'
platforms: 'linux/amd64,linux/arm64'
- javaTag: 'eclipse-temurin-alpine-17.0.10_7'
dockerContext: 'eclipse-temurin'
dockerfile: 'alpine.Dockerfile'
baseImageTag: '17.0.10_7-jdk-alpine'
platforms: 'linux/amd64,linux/arm64'
steps:
- uses: actions/checkout@v4
- name: Set up QEMU
Expand Down Expand Up @@ -100,6 +112,7 @@ jobs:
context: ${{ matrix.dockerContext }}
no-cache: true
tags: ${{ steps.create_docker_tag.outputs.TAG }}
file: ${{ matrix.dockerContext }}/${{ matrix.dockerfile || 'Dockerfile' }}
build-args: |
BASE_IMAGE_TAG=${{ matrix.baseImageTag }}
SBT_VERSION=${{ steps.get_sbt_version.outputs.VERSION }}
Expand Down Expand Up @@ -130,6 +143,7 @@ jobs:
uses: docker/build-push-action@v5
with:
context: ${{ matrix.dockerContext }}
file: ${{ matrix.dockerContext }}/${{ matrix.dockerfile || 'Dockerfile' }}
tags: ${{ steps.create_docker_tag.outputs.TAG }}
build-args: |
BASE_IMAGE_TAG=${{ matrix.baseImageTag }}
Expand Down
86 changes: 86 additions & 0 deletions eclipse-temurin/alpine.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Use a multi-stage build to reduce the size of the final image
FROM eclipse-temurin:${BASE_IMAGE_TAG:-21.0.2_13-jdk-alpine} as builder

ARG SCALA_VERSION=3.4.0
ARG SBT_VERSION=1.9.9
ARG USER_ID=1001
ARG GROUP_ID=1001
ENV SCALA_HOME=/usr/share/scala

# Install scala and sbt
RUN apk add --no-cache --virtual=.build-dependencies wget ca-certificates bash curl bc && \
cd "/tmp" && \
case $SCALA_VERSION in \
"3"*) URL=https://github.com/lampepfl/dotty/releases/download/$SCALA_VERSION/scala3-$SCALA_VERSION.tar.gz SCALA_DIR=scala3-$SCALA_VERSION ;; \
*) URL=https://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz SCALA_DIR=scala-$SCALA_VERSION ;; \
esac && \
curl -fsL $URL | tar xfz - -C /usr/share && \
mv /usr/share/$SCALA_DIR $SCALA_HOME && \
ln -s "$SCALA_HOME/bin/"* "/usr/bin/" && \
update-ca-certificates && \
scala -version && \
case $SCALA_VERSION in \
"3"*) echo '@main def main = println(s"Scala library version ${dotty.tools.dotc.config.Properties.versionNumberString}")' > test.scala ;; \
*) echo "println(util.Properties.versionMsg)" > test.scala ;; \
esac && \
scala -nocompdaemon test.scala && rm test.scala

RUN \
curl -fsL https://github.com/sbt/sbt/releases/download/v$SBT_VERSION/sbt-$SBT_VERSION.tgz | tar xfz - -C /usr/local && \
$(mv /usr/local/sbt-launcher-packaging-$SBT_VERSION /usr/local/sbt || true) && \
ln -s /usr/local/sbt/bin/* /usr/local/bin/ && \
sbt -Dsbt.rootdir=true -batch sbtVersion && \
apk del .build-dependencies && \
rm -rf "/tmp/"* && \
rm -rf /var/cache/apk/*

# Start a new stage for the final image
FROM eclipse-temurin:${BASE_IMAGE_TAG:-21.0.2_13-jdk-alpine}

ARG SCALA_VERSION=3.4.0
ARG SBT_VERSION=1.9.9
ARG USER_ID=1001
ARG GROUP_ID=1001

RUN apk add --no-cache bash git rpm

COPY --from=builder /usr/share/scala /usr/share/scala
COPY --from=builder /usr/local/sbt /usr/local/sbt
COPY --from=builder /usr/local/bin/sbt /usr/local/bin/sbt

# Add and use user sbtuser
RUN addgroup -g $GROUP_ID sbtuser && adduser -D -u $USER_ID -G sbtuser sbtuser
USER sbtuser

# Switch working directory
WORKDIR /home/sbtuser

ENV PATH="/usr/share/scala/bin:${PATH}"

# Prepare sbt (warm cache)
RUN \
mkdir -p project && \
echo "scalaVersion := \"${SCALA_VERSION}\"" > build.sbt && \
echo "sbt.version=${SBT_VERSION}" > project/build.properties && \
echo "// force sbt compiler-bridge download" > project/Dependencies.scala && \
echo "case object Temp" > Temp.scala && \
sbt sbtVersion && \
sbt compile && \
rm -r project && rm build.sbt && rm Temp.scala && rm -r target

# Link everything into root as well
# This allows users of this container to choose, whether they want to run the container as sbtuser (non-root) or as root
USER root
RUN \
rm -rf /tmp/..?* /tmp/.[!.]* * && \
ln -s /home/sbtuser/.cache /root/.cache && \
ln -s /home/sbtuser/.sbt /root/.sbt && \
if [ -d "/home/sbtuser/.ivy2" ]; then ln -s /home/sbtuser/.ivy2 /root/.ivy2; fi

# Switch working directory back to root
## Users wanting to use this container as non-root should combine the two following arguments
## -u sbtuser
## -w /home/sbtuser
WORKDIR /root

CMD sbt