From 19492181d3b131aed9efe857da10f5fbaffd330f Mon Sep 17 00:00:00 2001 From: Gonzalo Javier Aune Date: Mon, 14 Dec 2020 14:42:01 +0000 Subject: [PATCH] Added Dockerfile for only Relay. Fixed a bug on entrypoint.sh that didn't set env vars if it's not coming from ECS. Added github workflow for docker build --- .github/workflows/docker-build.yml | 46 +++++++++++++ Dockerfile | 103 ++++++----------------------- Dockerfile.lndrelay | 92 ++++++++++++++++++++++++++ docker-compose.yml | 1 + docker/entrypoint.sh | 3 + 5 files changed, 163 insertions(+), 82 deletions(-) create mode 100644 .github/workflows/docker-build.yml create mode 100644 Dockerfile.lndrelay diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml new file mode 100644 index 00000000..876393d7 --- /dev/null +++ b/.github/workflows/docker-build.yml @@ -0,0 +1,46 @@ +name: Docker build on push +env: + DOCKER_CLI_EXPERIMENTAL: enabled + +on: + push: + branches: [ docker-workflow-build ] + +jobs: + build: + runs-on: ubuntu-18.04 + name: Build and push dashboard image + steps: + - name: Set env variables + run: echo "BRANCH=$(echo ${GITHUB_REF#refs/heads/} | sed 's/\//-/g')" >> $GITHUB_ENV + - name: Show set env variables + run: | + printf " BRANCH: %s\n" "$BRANCH" + - name: Login to Docker Hub + run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin + - name: Checkout project + uses: actions/checkout@v2 + - name: Setup Docker buildx action + uses: crazy-max/ghaction-docker-buildx@v1 + id: buildx + with: + buildx-version: latest + qemu-version: latest + - name: Show available buildx platforms + run: echo ${{ steps.buildx.outputs.platforms }} + - name: Cache Docker layers + uses: actions/cache@v2 + id: cache + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + - name: Run Docker buildx + run: | + docker buildx build \ + --cache-from "type=local,src=/tmp/.buildx-cache" \ + --cache-to "type=local,dest=/tmp/.buildx-cache" \ + --platform linux/amd64,linux/arm64,linux/arm/v7 \ + --tag ${{ secrets.DOCKER_HUB_USER }}/sphinxrelay:$BRANCH \ + --output "type=registry" ./ diff --git a/Dockerfile b/Dockerfile index 43427186..80aaad3e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,92 +1,31 @@ -FROM golang:1.13-alpine as builder -LABEL maintainer="gonzaloaune@stakwork.com" +FROM node:12-buster-slim AS builder -# Force Go to use the cgo based DNS resolver. This is required to ensure DNS -# queries required to connect to linked containers succeed. -ENV GODEBUG netdns=cgo +WORKDIR /relay +RUN mkdir /relay/.lnd +RUN touch /relay/connection_string.txt +RUN chmod 777 /relay/connection_string.txt +COPY . . -# Pass a tag, branch or a commit using build-arg. This allows a docker -# image to be built from a specified Git state. The default image -# will use the Git tip of master by default. -ARG checkout="v0.11.1-beta" -# ARG checkout="master" +RUN apt-get update -# Install dependencies and build the binaries. -RUN apk add --no-cache --update alpine-sdk git make gcc openssh-client +RUN apt install -y make python-minimal +RUN apt install -y g++ gcc libmcrypt-dev -RUN git clone https://github.com/lightningnetwork/lnd /go/src/github.com/lightningnetwork/lnd -RUN cd /go/src/github.com/lightningnetwork/lnd \ -&& git checkout $checkout \ -&& make \ -&& make install tags="signrpc walletrpc chainrpc invoicesrpc experimental" +RUN npm install bcrypt +RUN npm install -# Start a new, final image. -FROM alpine:3.11 as final +FROM node:12-buster-slim -EXPOSE 80 -EXPOSE 9735 +USER 1000 + +WORKDIR /relay + +COPY --from=builder /relay . + +EXPOSE 3300 ENV NODE_ENV production ENV NODE_SCHEME http +ENV PORT 3300 -# Add bash and ca-certs, for quality of life and SSL-related reasons. -RUN apk --no-cache add bash ca-certificates - -# Copy the binaries from the builder image. -COPY --from=builder /go/bin/lncli /bin/ -COPY --from=builder /go/bin/lnd /bin/ - -RUN apk add --no-cache --update nodejs=12.15.0-r1 nodejs-npm=12.15.0-r1 sqlite=3.30.1-r2 git supervisor - -RUN git clone https://github.com/stakwork/sphinx-relay /relay/ - -WORKDIR /relay/ - -ARG sphinx_checkout="master" - -RUN git checkout $sphinx_checkout - -RUN apk --no-cache add g++ gcc libgcc libstdc++ linux-headers make python jq git curl libmcrypt-dev - -USER root - -RUN rm -rf node_modules/ -RUN npm install -RUN npm install --quiet node-gyp@3.8.0 -g -RUN npm -g config set user root -RUN npm install nw-gyp -g -RUN npm uninstall sqlite3 -RUN npm install sqlite3 --build-from-source --runtime=node-webkit --target_arch=x64 --target=0.42.0 -RUN npm uninstall sqlite3 -RUN npm install sqlite3 --build-from-source --runtime=node-webkit --target_arch=x64 --target=0.42.0 -RUN npm rebuild -RUN npm run tsc - -VOLUME /relay/.lnd - -COPY ./docker/lnd.conf.sample /relay/.lnd/lnd.conf - -#Uncomment if you have a copy of the channel.db you want to use. -#COPY ./channel.db /relay/.lnd/data/graph/mainnet/ - -RUN git clone https://github.com/stakwork/sphinx-keysend-test/ /sphinx-keysend/ -WORKDIR /sphinx-keysend/ - -ARG sphinx_keysend_checkout="binary" - -RUN git checkout $sphinx_keysend_checkout -RUN npm install - -WORKDIR /relay/ - -RUN apk --no-cache add expect bash - -RUN mkdir -p /var/log/supervisor -COPY ./docker/supervisord.conf /etc/supervisord.conf -COPY ./docker/lnd_supervisor.conf /etc/supervisor.d/lnd_supervisor.ini -COPY ./docker/relay_supervisor.conf /etc/supervisor.d/relay_supervisor.ini -COPY ./docker/aliases.sh /etc/profile.d/aliases.sh - -ENV ENV="/etc/profile" - -ENTRYPOINT [ "bash", "/relay/docker/entrypoint.sh" ] +CMD [ "node", "/relay/dist/app.js" ] diff --git a/Dockerfile.lndrelay b/Dockerfile.lndrelay new file mode 100644 index 00000000..43427186 --- /dev/null +++ b/Dockerfile.lndrelay @@ -0,0 +1,92 @@ +FROM golang:1.13-alpine as builder +LABEL maintainer="gonzaloaune@stakwork.com" + +# Force Go to use the cgo based DNS resolver. This is required to ensure DNS +# queries required to connect to linked containers succeed. +ENV GODEBUG netdns=cgo + +# Pass a tag, branch or a commit using build-arg. This allows a docker +# image to be built from a specified Git state. The default image +# will use the Git tip of master by default. +ARG checkout="v0.11.1-beta" +# ARG checkout="master" + +# Install dependencies and build the binaries. +RUN apk add --no-cache --update alpine-sdk git make gcc openssh-client + +RUN git clone https://github.com/lightningnetwork/lnd /go/src/github.com/lightningnetwork/lnd +RUN cd /go/src/github.com/lightningnetwork/lnd \ +&& git checkout $checkout \ +&& make \ +&& make install tags="signrpc walletrpc chainrpc invoicesrpc experimental" + +# Start a new, final image. +FROM alpine:3.11 as final + +EXPOSE 80 +EXPOSE 9735 + +ENV NODE_ENV production +ENV NODE_SCHEME http + +# Add bash and ca-certs, for quality of life and SSL-related reasons. +RUN apk --no-cache add bash ca-certificates + +# Copy the binaries from the builder image. +COPY --from=builder /go/bin/lncli /bin/ +COPY --from=builder /go/bin/lnd /bin/ + +RUN apk add --no-cache --update nodejs=12.15.0-r1 nodejs-npm=12.15.0-r1 sqlite=3.30.1-r2 git supervisor + +RUN git clone https://github.com/stakwork/sphinx-relay /relay/ + +WORKDIR /relay/ + +ARG sphinx_checkout="master" + +RUN git checkout $sphinx_checkout + +RUN apk --no-cache add g++ gcc libgcc libstdc++ linux-headers make python jq git curl libmcrypt-dev + +USER root + +RUN rm -rf node_modules/ +RUN npm install +RUN npm install --quiet node-gyp@3.8.0 -g +RUN npm -g config set user root +RUN npm install nw-gyp -g +RUN npm uninstall sqlite3 +RUN npm install sqlite3 --build-from-source --runtime=node-webkit --target_arch=x64 --target=0.42.0 +RUN npm uninstall sqlite3 +RUN npm install sqlite3 --build-from-source --runtime=node-webkit --target_arch=x64 --target=0.42.0 +RUN npm rebuild +RUN npm run tsc + +VOLUME /relay/.lnd + +COPY ./docker/lnd.conf.sample /relay/.lnd/lnd.conf + +#Uncomment if you have a copy of the channel.db you want to use. +#COPY ./channel.db /relay/.lnd/data/graph/mainnet/ + +RUN git clone https://github.com/stakwork/sphinx-keysend-test/ /sphinx-keysend/ +WORKDIR /sphinx-keysend/ + +ARG sphinx_keysend_checkout="binary" + +RUN git checkout $sphinx_keysend_checkout +RUN npm install + +WORKDIR /relay/ + +RUN apk --no-cache add expect bash + +RUN mkdir -p /var/log/supervisor +COPY ./docker/supervisord.conf /etc/supervisord.conf +COPY ./docker/lnd_supervisor.conf /etc/supervisor.d/lnd_supervisor.ini +COPY ./docker/relay_supervisor.conf /etc/supervisor.d/relay_supervisor.ini +COPY ./docker/aliases.sh /etc/profile.d/aliases.sh + +ENV ENV="/etc/profile" + +ENTRYPOINT [ "bash", "/relay/docker/entrypoint.sh" ] diff --git a/docker-compose.yml b/docker-compose.yml index c9f33046..22ac4f5f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,7 @@ services: relay: build: context: . + dockerfile: Dockerfile.lndrelay volumes: - .:/relay - .lnd/:/relay/.lnd diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 820d9a7c..a00b459a 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -8,6 +8,9 @@ echo "Setting ENV vars..." if [ ! -z ${ECS_CONTAINER_METADATA_URI+x} ]; then echo "ECS environment found, setting domain..." export NODE_DOMAIN=$(curl $ECS_CONTAINER_METADATA_URI | echo $(jq -r .DockerName).$NODE_DOMAIN) +else + export UNIQUE_NAME=$(curl --unix-socket /var/run/docker.sock http:/v1.40/containers/$HOSTNAME/json | jq '.Name' --raw-output | cut -c 2-) + export NODE_DOMAIN="$UNIQUE_NAME.$NODE_DOMAIN" fi # For dev purposes only