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 1 commit
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
145 changes: 89 additions & 56 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,77 @@
FROM alpine:3.13 AS build-erd
FROM alpine:3.13 AS base

ARG asciidoctor_version=2.0.12
ARG asciidoctor_confluence_version=0.0.2
ARG asciidoctor_pdf_version=1.5.4
ARG asciidoctor_diagram_version=2.1.0
ARG asciidoctor_epub3_version=1.5.0.alpha.19
ARG asciidoctor_mathematical_version=0.3.5
ARG asciidoctor_revealjs_version=4.1.0
ARG kramdown_asciidoc_version=1.0.1
ARG asciidoctor_bibtex_version=0.8.0

ENV ASCIIDOCTOR_VERSION=${asciidoctor_version} \
ASCIIDOCTOR_CONFLUENCE_VERSION=${asciidoctor_confluence_version} \
ASCIIDOCTOR_PDF_VERSION=${asciidoctor_pdf_version} \
ASCIIDOCTOR_DIAGRAM_VERSION=${asciidoctor_diagram_version} \
ASCIIDOCTOR_EPUB3_VERSION=${asciidoctor_epub3_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}

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# 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 \
Expand All @@ -13,37 +86,19 @@ RUN apk add --no-cache \
wget \
xz \
zlib-dev

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

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

FROM alpine:3.13
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]>"

ARG asciidoctor_version=2.0.12
ARG asciidoctor_confluence_version=0.0.2
ARG asciidoctor_pdf_version=1.5.4
ARG asciidoctor_diagram_version=2.1.0
ARG asciidoctor_epub3_version=1.5.0.alpha.19
ARG asciidoctor_mathematical_version=0.3.5
ARG asciidoctor_revealjs_version=4.1.0
ARG kramdown_asciidoc_version=1.0.1
ARG asciidoctor_bibtex_version=0.8.0

ENV ASCIIDOCTOR_VERSION=${asciidoctor_version} \
ASCIIDOCTOR_CONFLUENCE_VERSION=${asciidoctor_confluence_version} \
ASCIIDOCTOR_PDF_VERSION=${asciidoctor_pdf_version} \
ASCIIDOCTOR_DIAGRAM_VERSION=${asciidoctor_diagram_version} \
ASCIIDOCTOR_EPUB3_VERSION=${asciidoctor_epub3_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}"

# Installing package required for the runtime of
# any of the asciidoctor-* functionnalities
RUN apk add --no-cache \
Expand Down Expand Up @@ -71,37 +126,6 @@ RUN apk add --no-cache \
tzdata \
unzip \
which

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

# 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
Expand All @@ -116,6 +140,15 @@ RUN apk add --no-cache --virtual .pythonmakedepends \
seqdiag \
&& apk del -r --no-cache .pythonmakedepends

# --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} \
.