Search code examples
ruby-on-railsdockerdocker-composedockerfileyarnpkg

yarn install fails in docker build but succeeds when run in container


I'm unable to get yarn install to work when executed in Dockerfile by docker build but it works when run from command line within the container.

Dockerfile (most of relevant bits)

FROM ruby:2.7.6
RUN wget https://dl.yarnpkg.com/debian/pubkey.gpg
RUN cat pubkey.gpg | apt-key add - \
  && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN rm pubkey.gpg
...
COPY Gemfile* ./
RUN --mount=type=ssh bundle install --without staging production \
  && find /usr/local/bundle/cache -name "*.gem" -delete \
  && find /usr/local/bundle/gems -name "*.c" -delete \
  && find /usr/local/bundle/gems -name "*.o" -delete
COPY package.json ./
RUN yarn install
COPY . .
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]

docker build errors out with:

 ...
 => ERROR [14/15] RUN yarn install                                                                                                     3.2s
------
 > [14/15] RUN yarn install:
#0 0.459 yarn install v1.22.19
#0 0.471 info No lockfile found.
#0 0.475 [1/5] Validating package.json...
#0 0.478 [2/5] Resolving packages...
#0 3.184 error An unexpected error occurred: "https://registry.yarnpkg.com/@myrepo%2fmy-package: Not found".
#0 3.184 info If you think this is a bug, please open a bug report with the information provided in "/myapp/yarn-error.log".
#0 3.184 info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
------
failed to solve: executor failed running [/bin/sh -c yarn install]: exit code: 1

But if I comment out (remove) RUN yarn install and the 3 lines after it at end of the Dockerfile then I'm able to successfully build an image that I can docker compose run app bash to enter a shell in the container where yarn install completes (probably because it's not trying to encode / in the url to %2f but who knows.

Thoughts? I'm stumped.


Solution

  • In a container I've always used

    RUN apt-get --assume-yes install yarn && apt-mark hold yarn 
    

    so just replace

    RUN yarn install
    

    with

    RUN apt-get --assume-yes install yarn && apt-mark hold yarn