nodejs / corepack

Zero-runtime-dependency package acting as bridge between Node projects and their package managers
MIT License
2.31k stars 146 forks source link

Offline workflow does not work #448

Open TheKnarf opened 3 months ago

TheKnarf commented 3 months ago

According to the docs there is supposed to be an offline workflow where corepack doesn't need internet:

Or you're publishing your project to a system where the network is unavailable, in which case you'll preemptively generate a package manager archive from your local computer (using corepack pack -o) before storing it somewhere your container will be able to access (for example within your repository). After that it'll just be a matter of running corepack install -g --cache-only <path/to/corepack.tgz> to setup the cache.

I've created an example project to reproduce the bug with the following Dockerfile:

FROM node:20-slim AS build
WORKDIR /app

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"

COPY . .

RUN --mount=type=cache,id=corepack,target=/app/cache \
        ls -la /app/cache

RUN --mount=type=cache,id=corepack,target=/app/cache \
    if [ ! -f "/app/cache/corepack.tgz" ]; then \
        corepack pnpm --version; \
        mkdir -p /app/cache; \
    corepack pack -o /app/cache/corepack.tgz; \
    fi

RUN --mount=type=cache,id=corepack,target=/app/cache \
    COREPACK_ENABLE_NETWORK=0 corepack install -g --cache-only /app/cache/corepack.tgz

#RUN COREPACK_ENABLE_STRICT=0 COREPACK_ENABLE_NETWORK=0 corepack pnpm --version

RUN \
    --mount=type=cache,id=pnpm,target=/pnpm/store \
    COREPACK_ENABLE_STRICT=0 COREPACK_ENABLE_NETWORK=0 corepack pnpm install --frozen-lockfile --prefer-offline

EXPOSE 80

CMD ["node", "./src/index.js"]
  1. The Dockerfile is supposed to download pnpm using corepack and then cache it using corepack pack.

  2. Then it tries to install it from the cache

  3. At last I try to run a pnpm command through corepack with COREPACK_ENABLE_NETWORK=0 to simulate no internet. This does not work. Neither corepack pnpm install nor corepack pnpm --version.

Error log

docker build -f ./Dockerfile -t corepack-offline-bug . --progress plain:

#0 building with "orbstack" instance using docker driver

#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 900B done
#1 DONE 0.0s

#2 [internal] load metadata for docker.io/library/node:20-slim
#2 DONE 0.0s

#3 [internal] load .dockerignore
#3 transferring context: 90B done
#3 DONE 0.0s

#4 [build 1/7] FROM docker.io/library/node:20-slim
#4 DONE 0.0s

#5 [internal] load build context
#5 transferring context: 93.25kB 0.0s done
#5 DONE 0.0s

#6 [build 2/7] WORKDIR /app
#6 CACHED

#7 [build 3/7] COPY . .
#7 DONE 0.0s

#8 [build 4/7] RUN --mount=type=cache,id=corepack,target=/app/cache             ls -la /app/cache
#8 0.049 total 3784
#8 0.049 drwxr-xr-x 1 root root      24 Apr  4 14:17 .
#8 0.049 drwxr-xr-x 1 root root      10 Apr  4 14:29 ..
#8 0.049 -rw-r--r-- 1 root root 3873301 Apr  4 14:17 corepack.tgz
#8 DONE 0.1s

#9 [build 5/7] RUN --mount=type=cache,id=corepack,target=/app/cache     if [ ! -f "/app/cache/corepack.tgz" ]; then             corepack pnpm --version;                mkdir -p /app/cache;     corepack pack -o /app/cache/corepack.tgz;     fi
#9 DONE 0.1s

#10 [build 6/7] RUN --mount=type=cache,id=corepack,target=/app/cache     COREPACK_ENABLE_NETWORK=0 corepack install -g --cache-only /app/cache/corepack.tgz
#10 0.328 Adding pnpm@8.15.6 to the cache...
#10 DONE 0.6s

#11 [build 7/7] RUN     --mount=type=cache,id=pnpm,target=/pnpm/store   COREPACK_ENABLE_STRICT=0 COREPACK_ENABLE_NETWORK=0 corepack pnpm install --frozen-lockfile --prefer-offline
#11 0.136 Usage Error: Network access disabled by the environment; can't reach npm repository https://registry.npmjs.org
#11 0.136
#11 0.136 $ pnpm ...
#11 ERROR: process "/bin/sh -c COREPACK_ENABLE_STRICT=0 COREPACK_ENABLE_NETWORK=0 corepack pnpm install --frozen-lockfile --prefer-offline" did not complete successfully: exit code: 1
------
 > [build 7/7] RUN      --mount=type=cache,id=pnpm,target=/pnpm/store   COREPACK_ENABLE_STRICT=0 COREPACK_ENABLE_NETWORK=0 corepack pnpm install --frozen-lockfile --prefer-offline:
0.136 Usage Error: Network access disabled by the environment; can't reach npm repository https://registry.npmjs.org
0.136
0.136 $ pnpm ...
------
Dockerfile:24
--------------------
  23 |
  24 | >>> RUN \
  25 | >>>      --mount=type=cache,id=pnpm,target=/pnpm/store \
  26 | >>>      COREPACK_ENABLE_STRICT=0 COREPACK_ENABLE_NETWORK=0 corepack pnpm install --frozen-lockfile --prefer-offline
  27 |
--------------------
ERROR: failed to solve: process "/bin/sh -c COREPACK_ENABLE_STRICT=0 COREPACK_ENABLE_NETWORK=0 corepack pnpm install --frozen-lockfile --prefer-offline" did not complete successfully: exit code: 1
TheKnarf commented 3 months ago

Seems like my mistake was using --cache-only as that didn't activate pnpm. Now that seems this seems to work:

RUN --mount=type=cache,target=/app/cache \
    COREPACK_ENABLE_NETWORK=0 corepack install -g /app/cache/corepack.tgz

Perhaps adding an example to the Readme would help others in the future?