Skip to content

Commit

Permalink
Use multi-stage build in Dockerfile (#595)
Browse files Browse the repository at this point in the history
* Use multi-stage build in Dockerfile

#300.

* Move git describe and build into one stage.

Probably won't be a good idea to download an alpine image just to install git.

* Remove git describe step from CI.

* whoopsie, copy version from the build stage not the deleted stamp.
  • Loading branch information
Gnuxie authored Oct 10, 2024
1 parent 54dd85d commit b13b615
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 18 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#
# SPDX-License-Identifier: CC0-1.0

.git
node_modules
config
lib
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/docker-hub-develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ jobs:
uses: actions/checkout@v4
- name: Unshallow for git describe so we can create version.txt
run: git fetch --prune --unshallow --tags --all --force
- name: Prepare version file
run: git describe > version.txt

# Needed for multi platform builds
- name: Set up QEMU
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/docker-hub-latest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ jobs:
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: Unshallow for git describe so we can create version.txt
run: git fetch --prune --unshallow --tags --all --force
- name: Prepare version file
run: git describe > version.txt

# Needed for multi platform builds
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/docker-hub-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ jobs:
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: Unshallow for git describe so we can create version.txt
run: git fetch --prune --unshallow --tags --all --force
- name: Prepare version file
run: git describe > version.txt

# Needed for multi platform builds
- name: Set up QEMU
Expand Down
24 changes: 14 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
#
# SPDX-License-Identifier: Apache-2.0 AND AFL-3.0

# We can't use alpine anymore because crypto has rust deps.
FROM node:20-slim
FROM node:20-slim as build-stage
RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
COPY . /tmp/src
# describe the version.
RUN cd /tmp/src && git describe > version.txt.tmp && mv version.txt.tmp version.txt
# build and install
RUN cd /tmp/src \
&& yarn install --network-timeout 100000 \
&& yarn install --frozen-lockfile --network-timeout 100000 \
&& yarn build \
&& mv lib/ /draupnir/ \
&& mv node_modules / \
&& mv draupnir-entrypoint.sh / \
&& mv package.json / \
&& mv version.txt / \
&& cd / \
&& rm -rf /tmp/*
&& yarn install --frozen-lockfile --production --network-timeout 100000

FROM node:20-slim as final-stage
COPY --from=build-stage /tmp/src/version.txt version.txt
COPY --from=build-stage /tmp/src/lib/ /draupnir/
COPY --from=build-stage /tmp/src/node_modules /node_modules
COPY --from=build-stage /tmp/src/draupnir-entrypoint.sh /
COPY --from=build-stage /tmp/src/package.json /

ENV NODE_ENV=production
ENV NODE_CONFIG_DIR=/data/config
Expand Down

0 comments on commit b13b615

Please sign in to comment.