I'm trying to build a pre-built dev container (see here).
Among others, the app uses some Node.js packages. To speed up the dev container performance, I'd like to cache them. For this, I'm running npm ci within the Dockerfile (shortened for brevity, see here for full file):

ARG NODE_VERSION=YouForgotToSetIt
FROM mcr.microsoft.com/devcontainers/javascript-node:${NODE_VERSION}-bookworm
COPY ./frontend/package*.json /tmp/workout-tracker/npm/
WORKDIR /tmp/workout-tracker/npm/
RUN npm ci

This works nicely - when checking the Node.js package cache inside a running dev container, I can see that all packages have been downloaded.

However, when running npm ci inside /workspaces/workout-tracker/frontend from within the dev container, the node_modules directory does not yet exist and will be created. I'm not a Node.js developer, but I assume that the already downloaded packages from the cache need to be extracted into node_modules.

To speed things up, I'd like to improve this too, i.e. somehow make sure that the node_modules folder is already set up when creating the dev container. Since I execute npm ci within the Dockerfile, everything is already inside /tmp/workout-tracker/npm/node_modules - I want to reuse that. Here's what I already tried:

  1. Add "postCreateCommand": "cd ./frontend/; npm ci" to the dev container declaration → poor performance on container creation as npm ci needs quite some time.
  2. Add "postCreateCommand": "cp /tmp/workout-tracker/npm/node_modules/ ${containerWorkspaceFolder}/frontend/node_modules" to the dev container declaration → poor performance on container creation.
  3. Use a symbolic link ln -s /tmp/workout-tracker/npm/node_modules /workspaces/workout-tracker/frontend/node_modules → npm detects that this is a symlink and tries to remove it.
  4. Add "containerEnv": { "NPM_CONFIG_PREFIX": "/tmp/workout-tracker/npm/" } to the dev container declaration → when running npm ci inside /workspaces/workout-tracker/frontend, npm still creates a new node_modules folder and puts everything inside - as if it would ignore the setting. However, I confirmed via npm config ls -l that the setting is properly applied. When running npm ci --prefix "/tmp/workout-tracker/npm/", it works nicely as expected, i.e. there is nothing to do as everything is already fine inside /tmp/workout-tracker/npm/.
  5. Add "mounts": [ "source=workout_tracker_node_modules,target=${containerWorkspaceFolder}/frontend/node_modules,type=volume" ] to the dev container declaration → when running npm ci inside /workspaces/workout-tracker/frontend, npm fails with an error that it lacks permissions to create subfolders per dependency inside /workspaces/workout-tracker/frontend/node_modules.
  6. Add RUN cp -R ./node_modules /workspaces/workout-tracker/frontend/ to the Dockerfile → when starting the dev container, the copied directory is gone - probably due to mounting the sources from bare metal into the container.

Are there any other options that I can try? Thx!

0

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.