Skip to content

Commit

Permalink
feat(Dockerfile): Use multistage build to bring in pbf executable
Browse files Browse the repository at this point in the history
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
pelias/docker-baseimage#23 as well, will be only
322MB. That's a nice 600MB savings!

Before pelias/docker-baseimage#23 the image size
still drops to 500MB, still a healthy reduction.

Replaces #262
  • Loading branch information
orangejulius committed Nov 1, 2021
1 parent 38efdad commit 017ee51
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 017ee51

Please sign in to comment.