Skip to content

Commit

Permalink
feat: skip npm run build in docker-compose
Browse files Browse the repository at this point in the history
When using docker-compose, we mount the local drive and fire up a `npm run dev` which is effectively webpack in --watch mode, so that it looks for changes in frontend files and compiles-as-you-change interactively.

Now, somehow `npm run build` is in the Dockerfile, specifically in the superset-node layer which is used to compile and later COPY the static assets into a python-based layer, `lean`.

Now in the context of docker-compose, we don't need all of it, and that `npm run build` has gotten expensive in terms of memory usage and takes a while to run.

So in this PR I'm:
- making `npm run build` optional with a dockerfile ARG (default is current behavior, but docker-compose skips it)
- adding a `superset-node-base` layer for that superset-node service in docker-compose that runs `npm run dev`, ensuring the same base for both processes, but stopping/diverging prior to `npm ci`
- making `npm install` run only if node_module is not present in the superset-node service. The user can simply `npm i` locally if/when they alter package.json. That speeds up firing up `docker-compose up` as unless you nuke node_modules/ it'll skip that step
  • Loading branch information
mistercrunch committed May 31, 2024
1 parent 9b6eaab commit 5e11a08
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
13 changes: 11 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
######################################################################
ARG PY_VER=3.10-slim-bookworm

# For development purposes, we can skip the node build stage
ARG INCLUDE_NODE=true

# if BUILDPLATFORM is null, set it to 'amd64' (or leave as is otherwise).
ARG BUILDPLATFORM=${BUILDPLATFORM:-amd64}
FROM --platform=${BUILDPLATFORM} node:18-bullseye-slim AS superset-node
FROM --platform=${BUILDPLATFORM} node:18-bullseye-slim AS superset-node-base

ARG NPM_BUILD_CMD="build"

Expand All @@ -39,6 +42,8 @@ WORKDIR /app/superset-frontend
RUN --mount=type=bind,target=/frontend-mem-nag.sh,src=./docker/frontend-mem-nag.sh \
/frontend-mem-nag.sh

FROM superset-node-pre-build AS superset-node

RUN --mount=type=bind,target=./package.json,src=./superset-frontend/package.json \
--mount=type=bind,target=./package-lock.json,src=./superset-frontend/package-lock.json \
npm ci
Expand Down Expand Up @@ -87,7 +92,11 @@ RUN --mount=type=cache,target=/root/.cache/pip \
&& apt-get autoremove -yqq --purge build-essential \
&& rm -rf /var/lib/apt/lists/*

COPY --chown=superset:superset --from=superset-node /app/superset/static/assets superset/static/assets
# Conditionally copy from the superset-node stage if INCLUDE_NODE is true
RUN if [ "${INCLUDE_NODE}" = "true" ]; then \
COPY --chown=superset:superset --from=superset-node /app/superset/static/assets superset/static/assets; \
fi

## Lastly, let's install superset itself
COPY --chown=superset:superset superset superset
RUN --mount=type=cache,target=/root/.cache/pip \
Expand Down
8 changes: 7 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ x-common-build: &common-build
target: dev
cache_from:
- apache/superset-cache:3.10-slim-bookworm
args:
INCLUDE_NODE: false

services:
nginx:
Expand Down Expand Up @@ -147,7 +149,11 @@ services:
disable: true

superset-node:
image: node:18
build:
context: .
target: superset-node-base
cache_from:
- type=registry,ref=apache/superset-cache:3.10-slim-bookworm
environment:
# set this to false if you have perf issues running the npm i; npm run dev in-docker
# if you do so, you have to run this manually on the host, which should perform better!
Expand Down
5 changes: 2 additions & 3 deletions docker/docker-frontend.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ fi

if [ "$BUILD_SUPERSET_FRONTEND_IN_DOCKER" = "true" ]; then
cd /app/superset-frontend
npm install -f --no-optional --global webpack webpack-cli
npm install -f --no-optional
RUN if [ ! -d "node_modules" ]; then npm install; fi

echo "Running frontend"
echo "Running webpack in watch mode..."
npm run dev
else
echo "Skipping frontend build steps - YOU RUN IT MANUALLY ON THE HOST!"
Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ const config = {
'react/lib/ReactContext': true,
},
plugins,
devtool: 'source-map',
devtool: isDevMode ? 'source-map' : false,
};

// find all the symlinked plugins and use their source code for imports
Expand Down

0 comments on commit 5e11a08

Please sign in to comment.