From 19d2e82d15a71cc92dea1482891f2c0ff0708741 Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Tue, 31 Oct 2017 23:31:32 -0400 Subject: [PATCH] Reduce docker image size to about 20MB by using a multistage build. --- .dockerignore | 2 ++ docker/Dockerfile | 48 ++++++++++++++++++++--------------------------- 2 files changed, 22 insertions(+), 28 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..77c25935a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +test +build diff --git a/docker/Dockerfile b/docker/Dockerfile index e3444e60c..50f531ed6 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,34 +1,20 @@ -FROM alpine:3.5 +FROM alpine:3.6 as buildstage -RUN mkdir /opt -WORKDIR /opt +ARG DOCKER_TAG +RUN mkdir -p /src && mkdir -p /opt +COPY . /src +WORKDIR /src RUN NPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) && \ echo "@testing http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories && \ apk update && \ apk upgrade && \ apk add git cmake wget make libc-dev gcc g++ bzip2-dev boost-dev zlib-dev expat-dev lua5.2-dev libtbb@testing libtbb-dev@testing && \ - \ - echo "Building libstxxl" && \ - cd /opt && \ - git clone --depth 1 --branch 1.4.1 https://github.com/stxxl/stxxl.git && \ - cd stxxl && \ - mkdir build && \ - cd build && \ - cmake -DCMAKE_BUILD_TYPE=Release .. && \ - make -j${NPROC} && \ - make install - -ARG DOCKER_TAG -RUN mkdir /src -COPY . /src -WORKDIR /src - -RUN NPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) && \ + NPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) && \ echo "Building OSRM ${DOCKER_TAG}" && \ git show --format="%H" | head -n1 > /opt/OSRM_GITSHA && \ echo "Building OSRM gitsha $(cat /opt/OSRM_GITSHA)" && \ - mkdir build && \ + mkdir -p build && \ cd build && \ BUILD_TYPE="Release" && \ ENABLE_ASSERTIONS="Off" && \ @@ -41,13 +27,19 @@ RUN NPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) && \ cd ../profiles && \ cp -r * /opt && \ \ - echo "Cleaning up" && \ strip /usr/local/bin/* && \ - rm /usr/local/lib/libstxxl* && \ - cd /opt && \ - apk del boost-dev && \ - apk del g++ cmake libc-dev expat-dev zlib-dev bzip2-dev lua5.2-dev git make gcc && \ - apk add boost-filesystem boost-program_options boost-regex boost-iostreams boost-thread libgomp lua5.2 expat && \ - rm -rf /src /opt/stxxl /usr/local/bin/stxxl_tool /usr/local/lib/libosrm* + rm -rf /src /usr/local/lib/libosrm* + + +# Multistage build to reduce image size - https://docs.docker.com/engine/userguide/eng-image/multistage-build/#use-multi-stage-builds +# Only the content below ends up in the image, this helps remove /src from the image (which is large) +FROM alpine:3.6 as runstage +RUN mkdir -p /src && mkdir -p /opt +RUN echo "@testing http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories && \ + apk update && \ + apk add boost-filesystem boost-program_options boost-regex boost-iostreams boost-thread libgomp lua5.2 expat libtbb@testing +COPY --from=buildstage /usr/local /usr/local +COPY --from=buildstage /opt /opt +WORKDIR /opt EXPOSE 5000