Skip to content

Commit

Permalink
Use multi-stage Docker build to handle node-postal dependencies
Browse files Browse the repository at this point in the history
The [node-postal](https://github.com/openvenues/node-postal) NPM module
requires a full C++ compiler toolchain _and_ python3 to install. After
pelias/docker-baseimage#23 and
pelias/docker-libpostal_baseimage#5 this
toolchain is no longer present in our Docker baseimage.

This PR uses a Docker multi-stage build to build _just_ the NPM modules
required by the interpolation service while a C++ toolchain is present.

The `node_modules` directory can then be copied to the final image
without needing a C++ toolchain or python to be present.

In addition to saving some space in the final image, this fixes issues
people were having with our Docker images, since `node-postal` wasn't
functional.

Fixes pelias/docker#271
  • Loading branch information
orangejulius committed Nov 10, 2021
1 parent c41ef19 commit e00e89f
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
# builder image for node-postal (requires c++ toolchain and python3)
FROM pelias/libpostal_baseimage as builder

ENV WORKDIR /code/pelias/interpolation
WORKDIR ${WORKDIR}

# install dependencies for node-postal build
RUN apt-get update && apt-get install -y python3 build-essential

COPY --chown=pelias ./package.json ${WORKDIR}

RUN npm install

# base image
FROM pelias/libpostal_baseimage

# dependencies
# interpolation dependencies
RUN apt-get update && \
apt-get install -y python sqlite3 gdal-bin lftp unzip pigz time gawk && \
apt-get install -y sqlite3 gdal-bin lftp unzip pigz time gawk && \
rm -rf /var/lib/apt/lists/*

# change working dir
ENV WORKDIR /code/pelias/interpolation
WORKDIR ${WORKDIR}

# de-escalate to non-root user
RUN chown -R pelias /code
USER pelias

# copy package.json first to prevent npm install being rerun when only code changes
COPY --chown=pelias ./package.json ${WORKDIR}
# de-escalate to non-root user
USER pelias

# install npm dependencies
RUN npm install
# copy npm dependencies from builder image
COPY --chown=pelias --from=builder $WORKDIR/node_modules node_modules/

# add the rest of the code
# add the code
COPY --chown=pelias . ${WORKDIR}

# run a quick test that libpostal and node-postal are fully working
RUN node test/test_libpostal.js

# run tests
RUN npm test && npm run funcs && rm -rf test

Expand Down

0 comments on commit e00e89f

Please sign in to comment.