Skip to content

Instantly share code, notes, and snippets.

@denibertovic
Created January 12, 2019 10:20
Show Gist options
  • Save denibertovic/01f0bfd2f16c81fb487430558324745a to your computer and use it in GitHub Desktop.
Save denibertovic/01f0bfd2f16c81fb487430558324745a to your computer and use it in GitHub Desktop.
Dockerfile with buildkit (experimental support)
# syntax = docker/dockerfile:experimental
FROM ubuntu:18.04 as build
# This is phase1: the build phase
# where we build our project and output a binary
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
apt-transport-https \
apt-utils \
build-essential \
ca-certificates \
curl \
iputils-ping \
iputils-tracepath \
jq \
libgmp-dev \
lsb-release \
make \
netbase \
software-properties-common \
unzip \
xz-utils \
zlib1g-dev
# Install latest version of Stack
ENV STACK_VERSION=1.9.3
RUN curl -sSL https://github.com/commercialhaskell/stack/releases/download/v$STACK_VERSION/stack-$STACK_VERSION-linux-x86_64.tar.gz | tar xz --wildcards --strip-components=1 -C /usr/local/bin '*/stack'
# Ideally all of the above would go into a "build image" from which we can inherit here
# and just do the stuff below
# Make build directory and copy source
RUN mkdir -p /opt/build
RUN mkdir -p /tmp/out
COPY . /opt/build
# Run the build but make sure to cache .stack and .stack-work
RUN --mount=type=cache,target=/root/.stack \
--mount=type=cache,target=/opt/build/.stack-work \
cd /opt/build && \
stack build --copy-bins --local-bin-path=/tmp/out
FROM fpco/pid1:0.1.2.0
# In this phase we just copy over the prebuilt binary from the above build phase
ENV DEBIAN_FRONTEND=noninteractive
# Some common dependencies we might need with our app
RUN apt-get update && apt-get install -y \
ca-certificates \
netbase \
libgmp-dev
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
RUN mkdir -p /opt/app
COPY --from=build /tmp/out/my-binary /opt/app/
CMD ["/opt/app/my-binary"]
@denibertovic
Copy link
Author

Build using: DOCKER_BUILDKIT=1 docker build -t test .

This requires docker v18.06 (or newer).
See here for more info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment