fly-apps / dockerfile-rails

Provides a Rails generator to produce Dockerfiles and related files.
MIT License
489 stars 38 forks source link

Node path not found error when building image with Alpine #78

Closed euxx closed 11 months ago

euxx commented 11 months ago

Hi, I'm trying to generate Dockerfile with Alpine:

$ bin/rails generate dockerfile --alpine --jemalloc --yjit --cache --parallel

Node part of the generated Dockfile:

# Install JavaScript dependencies
ARG NODE_VERSION=20.10.0
ARG YARN_VERSION=1.22.21
RUN curl -sL https://unofficial-builds.nodejs.org/download/release/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64-musl.tar.gz | tar xz -C /tmp/ && \
    cp -rp /tmp/node-v${NODE_VERSION}-linux-x64-musl/* /usr/local && \
    npm install -g yarn@$YARN_VERSION && \
    rm -rf /tmp/node-v${NODE_VERSION}-linux-x64-musl

# Copy node modules
COPY --from=node /rails/node_modules /rails/node_modules
COPY --from=node /usr/local/node /usr/local/node
ENV PATH=/usr/local/node/bin:$PATH

When building the image, it raises an error: Error: failed to fetch an image or build from source: error building: failed to solve: failed to compute cache key: "/usr/local/node" not found: not found

Found the reason is the Node paths are inconsistent:

I tried to fix it by modifying the Dockerfile:

# Install JavaScript dependencies
ARG NODE_VERSION=20.10.0
ARG YARN_VERSION=1.22.21
+ ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://unofficial-builds.nodejs.org/download/release/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64-musl.tar.gz | tar xz -C /tmp/ && \
+    mkdir /usr/local/node && \
-    cp -rp /tmp/node-v${NODE_VERSION}-linux-x64-musl/* /usr/local && \
+    cp -rp /tmp/node-v${NODE_VERSION}-linux-x64-musl/* /usr/local/node && \
    npm install -g yarn@$YARN_VERSION && \
    rm -rf /tmp/node-v${NODE_VERSION}-linux-x64-musl

It works, but I'm not sure it's the best way.

So please fix it in the generator, thanks.