Skip to content

Instantly share code, notes, and snippets.

@mscharley
Last active February 16, 2021 22:05
Show Gist options
  • Save mscharley/b9e7e9d4038938a54278a73ea929f5fc to your computer and use it in GitHub Desktop.
Save mscharley/b9e7e9d4038938a54278a73ea929f5fc to your computer and use it in GitHub Desktop.
Example Dockerfile for static compilation of ReasonML / OCaml projects using esy.
# vim: set filetype=dockerfile :
FROM node:10.13-alpine as build
ENV TERM=dumb \
LD_LIBRARY_PATH=/usr/local/lib:/usr/lib:/lib
# Create a subuser so that NPM doesn't whine about running as root
RUN adduser -D -H -h /usr/src/app web && \
mkdir -p /usr/src/app && \
chown web:web /usr/src/app && \
# Install dependencies
apk add --no-cache \
# Requirements for glibc
ca-certificates wget \
# Requirements for esy
bash curl perl-utils \
# Requirements for ocaml
gcc musl-dev make m4 \
# Requirements for bs-platform
python g++ \
# Requirements for my app
git patch ncurses jq && \
# esy is dynamically linked to glibc, not compiled locally. Oh well.
wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.28-r0/glibc-2.28-r0.apk && \
apk add --no-cache glibc-2.28-r0.apk && \
# --unsafe-perm because we want to install esy globally as root
npm install -g --unsafe-perm @esy-nightly/[email protected]
# Use the new user for the build
WORKDIR /usr/src/app
USER web
COPY --chown=web:web ./ ./
RUN npm install
# Force the backend to be built statically
RUN cd packages/backend && \
esy build && \
jq '.buildDirs.src.flags = ["-ccopt", "-static"]' package.json > package.json.new && \
mv package.json.new package.json && \
# Force pesy to understand that it is outside a build environment
SHELL=sh esy pesy
# Build the application
RUN npm run build:production && \
npm run build:aggregate
# This is the real magic. FROM scratch. Only the application lives here.
FROM scratch
ENV TERM=dumb \
PORT=8080
COPY --from=build /usr/src/app/build /
CMD [ "/bin/BackendApp.exe" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment