Docker: build speed & size optimizations

- Pre-build transpiled source code (babel/stylus) and remove the devDependencies build tools from the final image.

- Allow to optionally keep devDependencies by setting the build arg TESTRUNNER.

- Shuffle around to optimize for minimal cache invalidation and fast build times during development.
This commit is contained in:
Nadav Ivgi 2018-01-31 05:20:28 +02:00
parent f00903242c
commit 6c885c957c
6 changed files with 91 additions and 46 deletions

View File

@ -2,3 +2,6 @@ node_modules
.env
sqlite.db
dist
.git
Dockerfile
bin/docker-build.sh

View File

@ -1,3 +1,3 @@
language: minimal
before_script: docker build -t charge .
before_script: docker build ---build-arg TESTRUNNER=1 -t charge .
script: docker run charge npm test -- --colors

View File

@ -1,54 +1,63 @@
FROM node:8.9-slim as builder
RUN apt-get update && apt-get install -y --no-install-recommends git curl build-essential ca-certificates jq inotify-tools wget \
autoconf automake build-essential libtool libgmp-dev libsqlite3-dev python python3
RUN apt-get update && apt-get install -y --no-install-recommends autoconf automake build-essential git libtool libgmp-dev \
libsqlite3-dev python python3 wget
# lightningd
ENV LIGHTNINGD_VERSION=master
RUN git clone https://github.com/ElementsProject/lightning.git /opt/lightningd && \
cd /opt/lightningd && \
git checkout $LIGHTNINGD_VERSION && \
make && rm -rf *.c *.h *.o Makefile test
ENV LIGHTNINGD_VERSION 0223d836ac9e20695dc665a6b8b8ec1460232c5d
RUN git clone https://github.com/ElementsProject/lightning.git /opt/lightningd \
&& cd /opt/lightningd \
&& git checkout $LIGHTNINGD_VERSION \
&& make
# bitcoind
ENV BITCOIN_VERSION 0.15.1
ENV BITCOIN_URL https://bitcoincore.org/bin/bitcoin-core-0.15.1/bitcoin-0.15.1-x86_64-linux-gnu.tar.gz
ENV BITCOIN_URL https://bitcoincore.org/bin/bitcoin-core-$BITCOIN_VERSION/bitcoin-$BITCOIN_VERSION-x86_64-linux-gnu.tar.gz
ENV BITCOIN_SHA256 387c2e12c67250892b0814f26a5a38f837ca8ab68c86af517f975a2a2710225b
ENV BITCOIN_ASC_URL https://bitcoincore.org/bin/bitcoin-core-0.15.1/SHA256SUMS.asc
ENV BITCOIN_ASC_URL https://bitcoincore.org/bin/bitcoin-core-$BITCOIN_VERSION/SHA256SUMS.asc
ENV BITCOIN_PGP_KEY 01EA5486DE18A882D4C2684590C8019E36C2E964
RUN mkdir -p /tmp/bitcoin \
&& cd /tmp/bitcoin \
&& wget -qO bitcoin.tar.gz "$BITCOIN_URL" \
&& echo "$BITCOIN_SHA256 bitcoin.tar.gz" | sha256sum -c - \
&& gpg --keyserver keyserver.ubuntu.com --recv-keys "$BITCOIN_PGP_KEY" \
&& wget -qO bitcoin.asc "$BITCOIN_ASC_URL" \
&& gpg --verify bitcoin.asc \
&& tar -xzvf bitcoin.tar.gz -C /tmp/bitcoin --strip-components=1 --exclude=*-qt \
&& mkdir -p /opt/bitcoin \
&& cp /tmp/bitcoin/bin/bitcoind /opt/bitcoin/bitcoind \
&& cp /tmp/bitcoin/bin/bitcoin-cli /opt/bitcoin/bitcoin-cli \
&& rm -rf /tmp/bitcoin
RUN mkdir /opt/bitcoin && cd /opt/bitcoin \
&& wget -qO bitcoin.tar.gz "$BITCOIN_URL" \
&& echo "$BITCOIN_SHA256 bitcoin.tar.gz" | sha256sum -c - \
&& gpg --keyserver keyserver.ubuntu.com --recv-keys "$BITCOIN_PGP_KEY" \
&& wget -qO bitcoin.asc "$BITCOIN_ASC_URL" \
&& gpg --verify bitcoin.asc \
&& BD=bitcoin-$BITCOIN_VERSION/bin \
&& tar -xzvf bitcoin.tar.gz $BD/bitcoind $BD/bitcoin-cli --strip-components=1
WORKDIR /opt/charged
ARG TESTRUNNER
COPY package*.json ./
RUN npm install
RUN npm install \
&& test -n "$TESTRUNNER" || { \
cp -r node_modules node_modules.dev && \
npm prune --production && npm install --production && \
mv -f node_modules node_modules.prod && \
mv -f node_modules.dev node_modules; }
# https://github.com/npm/npm/issues/19356#issuecomment-361475985
COPY . .
RUN npm run dist \
&& rm -rf src \
&& test -n "$TESTRUNNER" || (rm -rf test node_modules && mv -f node_modules.prod node_modules)
FROM node:8.9-slim
COPY --from=builder /opt/lightningd/lightningd /usr/bin
COPY --from=builder /opt/lightningd/cli/lightning-cli /usr/bin/lightning-cli
COPY --from=builder /opt/bitcoin/bitcoind /usr/bin/bitcoind
COPY --from=builder /opt/bitcoin/bitcoin-cli /usr/bin/bitcoin-cli
WORKDIR /opt/charged
ARG TESTRUNNER
ENV HOME /tmp
ENV NODE_ENV production
RUN apt-get update && apt-get install -y --no-install-recommends inotify-tools libgmp-dev libsqlite3-dev \
$(test -n "$TESTRUNNER" && echo jq) \
&& rm -rf /var/lib/apt/lists/* \
&& ln -s /opt/charged/bin/charged /usr/bin/charged \
&& mkdir /data
COPY --from=builder /opt/lightningd/cli/lightning-cli /usr/bin
COPY --from=builder /opt/lightningd/lightningd/lightning* /usr/bin/
COPY --from=builder /opt/bitcoin/bin /usr/bin
COPY --from=builder /opt/charged /opt/charged
WORKDIR /opt/charged
ENV HOME=/tmp
RUN mkdir -p /data
RUN apt-get update && apt-get install -y --no-install-recommends libgmp-dev libsqlite3-dev jq inotify-tools && \
rm -rf /var/lib/apt/lists/*
COPY . .
RUN ln -s /opt/charged/bin/charged /usr/bin/charged
CMD bin/docker-entrypoint.sh
EXPOSE 9112 9735

View File

@ -51,9 +51,9 @@ $ docker run -e NETWORK=regtest -e API_TOKEN=mySecretToken -p 9112:9112 shesek/l
```
To connect to an existing bitcoind instance running on the same machine,
mount the bitcoin data directory to `/etc/bitcoin` (i.e. `-v $HOME/.bitcoin:/etc/bitcoin`).
To connect to a remote bitcoind instance, set `-e BITCOIND_URI=http://[user]:[pass]@[host]:[port]`
(use `__cookie__:...` as the login for cookie-based authentication).
mount the bitcoin data directory to `/etc/bitcoin` (e.g. `-v $HOME/.bitcoin:/etc/bitcoin`).
To connect to a remote bitcoind instance, set `BITCOIND_URI=http://[user]:[pass]@[host]:[port]`
(or use `__cookie__:...` as the login for cookie-based authentication).
### Client libraries
@ -199,9 +199,16 @@ ws.on('message', msg => {
## Tests
Make sure `bitcoind`, `bitcoin-cli`, `lightningd`, `lightning-cli`
and [`jq`](https://stedolan.github.io/jq/download/) are in your `PATH`,
then run `$ npm test`.
Requires `bitcoind`, `bitcoin-cli`, `lightningd`, `lightning-cli`
and [`jq`](https://stedolan.github.io/jq/download/) to be in your `PATH`.
```bash
$ git clone https://github.com/ElementsProject/lightning-charge.git
$ cd lightning-charge
$ npm install
$ npm test
```
This will setup a temporary testing environment with a bitcoind regtest node
and two c-lightning nodes with a funded channel,
then start the Lightning Charge server and run the unit tests
@ -216,7 +223,7 @@ To prevent the test environment files from being deleted after completing the te
To setup a testing environment without running the tests, run `$ npm run testenv`.
This will display information about the running services and keep them alive for further inspection.
Tests can also be run using docker: `$ docker run shesek/lightning-charge npm test`
Tests can also be run using docker: `$ docker run shesek/lightning-charge:testrunner npm test`
## License

26
bin/docker-build.sh Executable file
View File

@ -0,0 +1,26 @@
#!/bin/bash
set -x
set -eo pipefail
VER=`jq -r .version package.json`
NAME=${1:-shesek/lightning-charge}
docker build -t charge .
docker build --build-arg TESTRUNNER=1 -t charge:testrunner .
docker tag charge $NAME:$VER
docker tag charge $NAME:latest
docker tag charge $NAME
docker tag charge:testrunner $NAME:$VER-testrunner
docker tag charge:testrunner $NAME:testrunner
read -p 'Press enter to push'
docker push $NAME:$VER
docker push $NAME:latest
docker push $NAME
docker push $NAME:$VER-testrunner
docker push $NAME:testrunner

View File

@ -95,7 +95,7 @@ echo Setting up charged >&2
LN_PATH=$LN_ALICE_PATH DB_PATH=$CHARGE_DB API_TOKEN=$CHARGE_TOKEN PORT=$CHARGE_PORT \
DEBUG=$DEBUG,lightning-*,knex:query,knex:bindings \
NODE_ENV=test babel-node src/app.js &> $DIR/charge.log &
NODE_ENV=test bin/charged &> $DIR/charge.log &
CHARGE_PID=$!
sed $sedq '/HTTP server running/ q' <(tail -F -n+0 $DIR/charge.log 2> /dev/null)