diff --git a/.cache/.gitkeep b/.cache/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/.gitignore b/.gitignore index deef931..1472337 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ projects/ +.cache/ diff --git a/Dockerfile b/Dockerfile index eab0583..1068557 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,14 +4,39 @@ RUN apt update RUN apt install -y ca-certificates curl RUN apt install -y default-jdk -RUN export PATH="/root/.local/share/coursier/bin:${PATH}" +# Install latest su-exec +# https://gist.github.com/dmrub/b311d36492f230887ab0743b3af7309b +RUN set -ex; \ + curl -o /usr/local/bin/su-exec.c https://raw.githubusercontent.com/ncopa/su-exec/master/su-exec.c; \ + fetch_deps='gcc libc-dev'; \ + apt-get update && \ + apt-get install -y --no-install-recommends $fetch_deps \ + && apt-get -y autoremove \ + && apt-get clean autoclean \ + && rm -fr /var/lib/apt/lists/* /tmp/* /var/tmp/*; \ + gcc -Wall \ + /usr/local/bin/su-exec.c -o/usr/local/bin/su-exec; \ + chown root:root /usr/local/bin/su-exec; \ + chmod 0755 /usr/local/bin/su-exec; \ + rm /usr/local/bin/su-exec.c; \ + apt-get purge -y --auto-remove $fetch_deps \ + && apt-get -y autoremove \ + && apt-get clean autoclean \ + && rm -fr /var/lib/apt/lists/* /tmp/* /var/tmp/* -RUN curl -fL https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz | gzip -d > cs -RUN chmod +x cs -RUN ./cs setup -y -RUN apt install -y gnupg2 -RUN echo "deb https://repo.scala-sbt.org/scalasbt/debian all main" | tee /etc/apt/sources.list.d/sbt.list -RUN echo "deb https://repo.scala-sbt.org/scalasbt/debian /" | tee /etc/apt/sources.list.d/sbt_old.list -RUN curl -sL "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823" | apt-key add -RUN apt-get update -RUN apt-get install -y sbt +ENV PATH="/root/.local/share/coursier/bin:${PATH}" + +RUN curl -fL https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz | gzip -d > /usr/local/bin/cs +RUN chmod +x /usr/local/bin/cs +RUN cs install scala:2.13.8 --dir /usr/share/coursier && cs install --dir /usr/share/coursier scalac:2.13.8 +ENV PATH="$PATH:/usr/share/coursier" + +RUN curl -L https://github.com/sbt/sbt/releases/download/v1.5.7/sbt-1.5.7.tgz | tar xvz -C /tmp +RUN cp /tmp/sbt/bin/sbt /usr/local/bin/sbt && chmod uga+x /usr/local/bin/sbt + +RUN useradd -ms /bin/zsh me +# RUN mkdir /home/me/.cache && chown me:me /home/me/.cache + +COPY docker-entrypoint.sh /bin/entrypoint.sh +RUN chmod uga+w /bin/entrypoint.sh +ENTRYPOINT ["/bin/entrypoint.sh"] diff --git a/Makefile b/Makefile index aa7ae0e..49b59f9 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,11 @@ +export UID := $(shell id -u) +export GID := $(shell id -g) + build: docker-compose build run: docker-compose run --rm scala bash + +push: + docker-compose push scala diff --git a/docker-compose.yml b/docker-compose.yml index b6cf952..e25ab7b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,14 @@ version: '3.8' +volumes: + cache: services: scala: + environment: + - HOST_USER_ID=${UID:-1000} + - HOST_GROUP_ID=${GID:-1000} build: . + image: ghcr.io/soisy/applied-functional-programming:1 volumes: - - ./projects:/projects - working_dir: /projects + - ./.cache:/home/me/.cache:rw + - ./projects:/home/me/projects + working_dir: /home/me/projects diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100755 index 0000000..a614c0b --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,18 @@ +#!/bin/sh +# Get standard cli USER_ID variable +USER_ID=${HOST_USER_ID:-1000} +GROUP_ID=${HOST_GROUP_ID:-1000} +# Change 'me' uid to host user's uid +echo "Running as ${GROUP_ID}:${USER_ID}" + +if [ ! -z "$USER_ID" ] && [ "$(id -u me)" != "$USER_ID" ]; then + # Create the user group if it does not exist + groupadd --non-unique -g "$GROUP_ID" me + # Set the user's uid and gid + usermod --non-unique --uid "$USER_ID" --gid "$GROUP_ID" me +fi + +# Setting permissions on docker.sock + +# zsh shell +su-exec me $@