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

Utilize multi-stage build to improve the image size #184

Merged
merged 3 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
142 changes: 91 additions & 51 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
FROM alpine:3.13

LABEL MAINTAINERS="Guillaume Scheibel <[email protected]>, Damien DUPORTAL <[email protected]>"
FROM alpine:3.13 AS base

ARG asciidoctor_version=2.0.12
ARG asciidoctor_confluence_version=0.0.2
Expand All @@ -20,8 +18,86 @@ ENV ASCIIDOCTOR_VERSION=${asciidoctor_version} \
ASCIIDOCTOR_MATHEMATICAL_VERSION=${asciidoctor_mathematical_version} \
ASCIIDOCTOR_REVEALJS_VERSION=${asciidoctor_revealjs_version} \
KRAMDOWN_ASCIIDOC_VERSION=${kramdown_asciidoc_version} \
ASCIIDOCTOR_BIBTEX_VERSION=${asciidoctor_bibtex_version} \
PATH="/root/.cabal/bin/:${PATH}"
ASCIIDOCTOR_BIBTEX_VERSION=${asciidoctor_bibtex_version}

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Ruby build for: asciidoctor, asciidoctor-pdf, and all kinds of associated tools
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

FROM base AS build-ruby
RUN echo "building Ruby dependencies" # keep here to help --cache-from along

RUN apk add --no-cache \
build-base \
libxml2-dev \
ruby-dev \
cmake \
bison \
flex \
python3 \
glib-dev \
cairo-dev \
pango-dev \
gdk-pixbuf-dev

RUN gem install --no-document \
--install-dir /usr/lib/ruby/gems \
--bindir /usr/lib/ruby/bin \
"asciidoctor:${ASCIIDOCTOR_VERSION}" \
"asciidoctor-confluence:${ASCIIDOCTOR_CONFLUENCE_VERSION}" \
"asciidoctor-diagram:${ASCIIDOCTOR_DIAGRAM_VERSION}" \
"asciidoctor-epub3:${ASCIIDOCTOR_EPUB3_VERSION}" \
"asciidoctor-mathematical:${ASCIIDOCTOR_MATHEMATICAL_VERSION}" \
asciimath \
"asciidoctor-pdf:${ASCIIDOCTOR_PDF_VERSION}" \
"asciidoctor-revealjs:${ASCIIDOCTOR_REVEALJS_VERSION}" \
bigdecimal \
coderay \
epubcheck-ruby:4.2.4.0 \
haml \
"kramdown-asciidoc:${KRAMDOWN_ASCIIDOC_VERSION}" \
pygments.rb \
rouge \
slim \
thread_safe \
tilt \
text-hyphen \
"asciidoctor-bibtex:${ASCIIDOCTOR_BIBTEX_VERSION}"

RUN rm -rf /usr/lib/ruby/gems/cache/*

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Haskell build for: erd
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

FROM base AS build-haskell
RUN echo "building Haskell dependencies" # keep here to help --cache-from along

RUN apk add --no-cache \
alpine-sdk \
cabal \
ghc-dev \
ghc \
gmp-dev \
gnupg \
libffi-dev \
linux-headers \
perl-utils \
wget \
xz \
zlib-dev

RUN cabal v2-update \
&& cabal v2-install erd

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Final image
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

FROM base AS main
RUN echo "building main image" # keep here to help --cache-from along

LABEL MAINTAINERS="Guillaume Scheibel <[email protected]>, Damien DUPORTAL <[email protected]>"

# Installing package required for the runtime of
# any of the asciidoctor-* functionnalities
Expand All @@ -32,8 +108,10 @@ RUN apk add --no-cache \
findutils \
font-bakoma-ttf \
git \
gmp \
graphviz \
inotify-tools \
libffi \
make \
openjdk8-jre \
python3 \
Expand All @@ -49,35 +127,6 @@ RUN apk add --no-cache \
unzip \
which

# Installing Ruby Gems needed in the image
# including asciidoctor itself
RUN apk add --no-cache --virtual .rubymakedepends \
build-base \
libxml2-dev \
ruby-dev \
&& gem install --no-document \
"asciidoctor:${ASCIIDOCTOR_VERSION}" \
"asciidoctor-confluence:${ASCIIDOCTOR_CONFLUENCE_VERSION}" \
"asciidoctor-diagram:${ASCIIDOCTOR_DIAGRAM_VERSION}" \
"asciidoctor-epub3:${ASCIIDOCTOR_EPUB3_VERSION}" \
"asciidoctor-mathematical:${ASCIIDOCTOR_MATHEMATICAL_VERSION}" \
asciimath \
"asciidoctor-pdf:${ASCIIDOCTOR_PDF_VERSION}" \
"asciidoctor-revealjs:${ASCIIDOCTOR_REVEALJS_VERSION}" \
bigdecimal \
coderay \
epubcheck-ruby:4.2.4.0 \
haml \
"kramdown-asciidoc:${KRAMDOWN_ASCIIDOC_VERSION}" \
pygments.rb \
rouge \
slim \
thread_safe \
tilt \
text-hyphen \
"asciidoctor-bibtex:${ASCIIDOCTOR_BIBTEX_VERSION}" \
&& apk del -r --no-cache .rubymakedepends

# Installing Python dependencies for additional
# functionnalities as diagrams or syntax highligthing
RUN apk add --no-cache --virtual .pythonmakedepends \
Expand All @@ -91,23 +140,14 @@ RUN apk add --no-cache --virtual .pythonmakedepends \
seqdiag \
&& apk del -r --no-cache .pythonmakedepends

# ERD
RUN apk add --no-cache --virtual .haskellmakedepends \
alpine-sdk \
cabal \
ghc-dev \
ghc \
gmp-dev \
gnupg \
libffi-dev \
linux-headers \
perl-utils \
wget \
xz \
zlib-dev \
&& cabal v2-update \
&& cabal v2-install erd \
&& apk del -r --no-cache .haskellmakedepends
# --target=XYZ --> XYZ/bin --> ln -s XYZ/bin/* /bin/
# COPY --from=build-python /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages

COPY --from=build-ruby /usr/lib/ruby/gems/ /usr/lib/ruby/gems/
COPY --from=build-ruby /usr/lib/ruby/bin/ /bin/
ENV GEM_HOME=/usr/lib/ruby/gems

COPY --from=build-haskell root/.cabal/bin/erd /bin/

WORKDIR /documents
VOLUME /documents
Expand Down
37 changes: 37 additions & 0 deletions build_image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

# Problem: docker doesn't cache multi-stage builds by default,
# so the full build time of ~10min hits me on every tinker-rebuild.
# Solution as per: https://github.com/moby/moby/issues/34715 is to
# - force caching with --cache-from,
# - use buildkit, and
# - tell buildkit to build cacheable images.

export DOCKER_BUILDKIT=1

image_name="adoc-test"

docker build \
--target build-ruby \
--tag ${image_name}:build-ruby \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--cache-from=${image_name}:build-ruby \
.

docker build \
--target build-haskell \
--tag ${image_name}:build-haskell \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--cache-from=${image_name}:build-haskell \
.

docker build \
--target main \
--tag ${image_name} \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--cache-from=${image_name}:build-ruby \
--cache-from=${image_name}:build-haskell \
--cache-from=${image_name} \
.