From 017ee51930907538d6e176b3f8e825c9bfc98f99 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Mon, 1 Nov 2021 12:10:27 -0700 Subject: [PATCH] feat(Dockerfile): Use multistage build to bring in `pbf` executable The polylines Docker image is a bit of a large one currently, as it includes not just Node.js and a `node_modules` directory, but a full compiler toolchain, an install of the Go language, the dependencies of the `pbf` repository from https://github.com/missinglink/pbf, and the final `pbf` executable that comes from it. All told, this brought the total image size to a whopping 950MB uncompressed. This PR makes use of multi stage builds to run the compiling of the `pbf` executable in a separate container. After this, all the toolchain and dependencies needed can be thrown away, and only the small executable copied to the final image. Using `container-diff` it looks like the image size, uncompressed, after https://github.com/pelias/docker-baseimage/pull/23 as well, will be only 322MB. That's a nice 600MB savings! Before https://github.com/pelias/docker-baseimage/pull/23 the image size still drops to 500MB, still a healthy reduction. Replaces https://github.com/pelias/polylines/pull/262 --- Dockerfile | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 48add80..fa15457 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,27 @@ -# base image -FROM pelias/baseimage +# use multi-stage build to make pbf binary +FROM pelias/baseimage as builder -# change working dir -ENV WORKDIR /code/pelias/polylines -WORKDIR ${WORKDIR} +RUN apt-get update && apt-get install gcc -y # install Golang -ENV GOPATH=/usr/src/.go +ENV GOPATH=/go RUN wget -qO- https://golang.org/dl/go1.15.2.linux-amd64.tar.gz | tar -C /usr/local -xzf - -RUN mkdir -p "${GOPATH}" -ENV PATH="${PATH}:/usr/local/go/bin:${GOPATH}/bin" +ENV PATH="${PATH}:/usr/local/go/bin" ENV GO111MODULE=on -# get go dependencies +# install pbf dependency RUN go get github.com/missinglink/pbf +# use Pelias baseimage for the main image +FROM pelias/baseimage + +# change working dir +ENV WORKDIR /code/pelias/polylines +WORKDIR ${WORKDIR} + +# copy pbf binary from builder container +COPY --from=builder /go/bin/pbf /bin/ + # copy package.json first to prevent npm install being rerun when only code changes COPY ./package.json ${WORKDIR} RUN npm install