Skip to content

Commit a5ce600

Browse files
committed
docker: use stages for nodejs and golang parts
With this commit we split the build process into three stages: we start with the JavaScript to static asset build process that requires NodeJS then copy the result into the second stage that requires golang. Finally, we just extract the final binaries in the third stage to keep the shipped image as small as possible.
1 parent 378687b commit a5ce600

File tree

2 files changed

+55
-50
lines changed

2 files changed

+55
-50
lines changed

Dockerfile

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,42 @@
1-
FROM golang:1.15.5-alpine as builder
2-
3-
# Force Go to use the cgo based DNS resolver. This is required to ensure DNS
4-
# queries required to connect to linked containers succeed.
5-
ENV GODEBUG netdns=cgo
1+
# Start with a NodeJS base image that also contains yarn.
2+
FROM node:12.17.0-alpine as nodejsbuilder
63

74
# Pass a tag, branch or a commit using build-arg. This allows a docker image to
85
# be built from a specified Git state. The default image will use the Git tip of
96
# master by default.
107
ARG checkout="master"
118

9+
RUN apk add --no-cache --update alpine-sdk \
10+
git \
11+
&& git clone https://github.com/lightninglabs/lightning-terminal /go/src/github.com/lightninglabs/lightning-terminal \
12+
&& cd /go/src/github.com/lightninglabs/lightning-terminal \
13+
&& git checkout $checkout \
14+
&& cd app \
15+
&& yarn install \
16+
&& yarn build
17+
18+
# The first stage is already done and all static assets should now be generated
19+
# in the app/build sub directory.
20+
FROM golang:1.15.5-alpine as golangbuilder
21+
22+
# Instead of checking out from git again, we just copy the whole working
23+
# directory of the previous stage that includes the generated static assets.
24+
COPY --from=nodejsbuilder /go/src/github.com/lightninglabs/lightning-terminal /go/src/github.com/lightninglabs/lightning-terminal
25+
26+
# Force Go to use the cgo based DNS resolver. This is required to ensure DNS
27+
# queries required to connect to linked containers succeed.
28+
ENV GODEBUG netdns=cgo
29+
1230
# Explicitly turn on the use of modules (until this becomes the default).
1331
ENV GO111MODULE on
1432

15-
ENV NODE_VERSION=v12.17.0
16-
1733
# Install dependencies and install/build lightning-terminal.
1834
RUN apk add --no-cache --update alpine-sdk \
19-
git \
2035
make \
21-
curl \
22-
bash \
23-
binutils \
24-
tar \
25-
&& touch ~/.bashrc \
26-
&& curl -sfSLO https://unofficial-builds.nodejs.org/download/release/${NODE_VERSION}/node-${NODE_VERSION}-linux-x64-musl.tar.xz \
27-
&& tar -xf node-${NODE_VERSION}-linux-x64-musl.tar.xz -C /usr --strip 1 \
28-
&& rm node-${NODE_VERSION}-linux-x64-musl.tar.xz \
29-
&& curl -o- -L https://yarnpkg.com/install.sh | bash \
30-
&& . ~/.bashrc \
31-
&& git clone https://github.com/lightninglabs/lightning-terminal /go/src/github.com/lightninglabs/lightning-terminal \
32-
&& cd /go/src/github.com/lightninglabs/lightning-terminal \
33-
&& git checkout $checkout \
34-
&& make install \
35-
&& make go-install-cli
36+
&& cd /go/src/github.com/lightninglabs/lightning-terminal \
37+
&& make statik-only \
38+
&& make go-install \
39+
&& make go-install-cli
3640

3741
# Start a new, final image to reduce size.
3842
FROM alpine as final
@@ -44,11 +48,11 @@ VOLUME /root/.lnd
4448
EXPOSE 8443 10009 9735
4549

4650
# Copy the binaries and entrypoint from the builder image.
47-
COPY --from=builder /go/bin/litd /bin/
48-
COPY --from=builder /go/bin/lncli /bin/
49-
COPY --from=builder /go/bin/frcli /bin/
50-
COPY --from=builder /go/bin/loop /bin/
51-
COPY --from=builder /go/bin/pool /bin/
51+
COPY --from=golangbuilder /go/bin/litd /bin/
52+
COPY --from=golangbuilder /go/bin/lncli /bin/
53+
COPY --from=golangbuilder /go/bin/frcli /bin/
54+
COPY --from=golangbuilder /go/bin/loop /bin/
55+
COPY --from=golangbuilder /go/bin/pool /bin/
5256

5357
# Add bash.
5458
RUN apk add --no-cache \

dev.Dockerfile

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
1-
FROM golang:1.15.5-alpine as builder
1+
# Start with a NodeJS base image that also contains yarn.
2+
FROM node:12.17.0-alpine as nodejsbuilder
23

34
# Copy in the local repository to build from.
45
COPY . /go/src/github.com/lightninglabs/lightning-terminal
56

7+
RUN cd /go/src/github.com/lightninglabs/lightning-terminal/app \
8+
&& yarn install \
9+
&& yarn build
10+
11+
# The first stage is already done and all static assets should now be generated
12+
# in the app/build sub directory.
13+
FROM golang:1.15.5-alpine as golangbuilder
14+
15+
# Instead of checking out from git again, we just copy the whole working
16+
# directory of the previous stage that includes the generated static assets.
17+
COPY --from=nodejsbuilder /go/src/github.com/lightninglabs/lightning-terminal /go/src/github.com/lightninglabs/lightning-terminal
18+
619
# Force Go to use the cgo based DNS resolver. This is required to ensure DNS
720
# queries required to connect to linked containers succeed.
821
ENV GODEBUG netdns=cgo
922

1023
# Explicitly turn on the use of modules (until this becomes the default).
1124
ENV GO111MODULE on
1225

13-
ENV NODE_VERSION=v12.17.0
14-
1526
# Install dependencies and install/build lightning-terminal.
1627
RUN apk add --no-cache --update alpine-sdk \
17-
git \
1828
make \
19-
curl \
20-
bash \
21-
binutils \
22-
tar \
23-
&& touch ~/.bashrc \
24-
&& curl -sfSLO https://unofficial-builds.nodejs.org/download/release/${NODE_VERSION}/node-${NODE_VERSION}-linux-x64-musl.tar.xz \
25-
&& tar -xf node-${NODE_VERSION}-linux-x64-musl.tar.xz -C /usr --strip 1 \
26-
&& rm node-${NODE_VERSION}-linux-x64-musl.tar.xz \
27-
&& curl -o- -L https://yarnpkg.com/install.sh | bash \
28-
&& . ~/.bashrc \
29-
&& cd /go/src/github.com/lightninglabs/lightning-terminal \
30-
&& make install \
31-
&& make go-install-cli
29+
&& cd /go/src/github.com/lightninglabs/lightning-terminal \
30+
&& make statik-only \
31+
&& make go-install \
32+
&& make go-install-cli
3233

3334
# Start a new, final image to reduce size.
3435
FROM alpine as final
@@ -40,11 +41,11 @@ VOLUME /root/.lnd
4041
EXPOSE 8443 10009 9735
4142

4243
# Copy the binaries and entrypoint from the builder image.
43-
COPY --from=builder /go/bin/litd /bin/
44-
COPY --from=builder /go/bin/lncli /bin/
45-
COPY --from=builder /go/bin/frcli /bin/
46-
COPY --from=builder /go/bin/loop /bin/
47-
COPY --from=builder /go/bin/pool /bin/
44+
COPY --from=golangbuilder /go/bin/litd /bin/
45+
COPY --from=golangbuilder /go/bin/lncli /bin/
46+
COPY --from=golangbuilder /go/bin/frcli /bin/
47+
COPY --from=golangbuilder /go/bin/loop /bin/
48+
COPY --from=golangbuilder /go/bin/pool /bin/
4849

4950
# Add bash.
5051
RUN apk add --no-cache \

0 commit comments

Comments
 (0)