I have tried for hours to install Yarn on a Docker image without success.
This is the Dockerfile
:
FROM ruby:2.5.5
RUN apt-get update && apt-get install -y nodejs yarn postgresql-client
RUN mkdir /app
WORKDIR /app
COPY Gemfile .
COPY Gemfile.lock .
RUN gem install bundler
RUN bundle install
COPY . .
RUN rake assets:precompile
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
The problem is that when the rake
command runs, I see this error in the output:
Yarn executable was not detected in the system.
I have also tried every solution described here: https://yarnpkg.com/en/docs/install
The problem is that nothing works: commands fail due to use of sudo
(which is not available) or raise errors like this:
Yarn was installed, but doesn't seem to be working :(
You can easily try to reproduce what I describe by trying to build the Dockerfile.
How can I install Yarn on a Docker image? You should also add this information to your website in the installation section.
Hi @collimarco,
https://yarnpkg.com/lang/en/docs/install/#debian-stable
So far this information is missing. So is yarn already part of the used images?
You can safely remove sudo
.
Which exact errors do you get then?
@DanielRuf This is the error that I get if I run without sudo
:
E: The method driver /usr/lib/apt/methods/https could not be found.
E: Failed to fetch https://dl.yarnpkg.com/debian/dists/stable/InRelease
E: Some index files failed to download. They have been ignored, or old ones used instead.
I have tested with this Dockerfile:
FROM ruby:2.5.5
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y yarn
...
Quite obvious. You need the package for https transport.
https://unix.stackexchange.com/a/263803/159964
https://askubuntu.com/a/104185/501512
Please try the following before this:
apt-get install apt-transport-https
Better:
apt-get install apt-transport-https ca-certificates
Now it seems to install yarn, but then I get this:
Step 14/16 : RUN rake assets:precompile
---> Running in abcd123
/usr/share/yarn/lib/cli.js:45703
let {
^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/usr/share/yarn/bin/yarn.js:24:13)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
SyntaxError: Unexpected token {
What does the following output?
node --version
@DanielRuf Running nodejs --version
inside the Dockerfile returns v4.8.2
Well, this version has already reached its EOL and is not supported by the current Yarn version.
I recommend installing nvm
to manage the available versions.
I have tried with this:
FROM ruby:2.5.5
RUN apt-get update
RUN apt-get install -y apt-transport-https ca-certificates
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get install -y yarn
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash
RUN apt-get install -y nodejs
...
And I get this error again:
Step 18/20 : RUN rake assets:precompile
---> Running in abcd123
Yarn executable was not detected in the system.
Download Yarn at https://yarnpkg.com/en/docs/install
What does the current PATH include?
See the Path Setup part at https://yarnpkg.com/lang/en/docs/install/#debian-stable
Please try export PATH="$PATH:/opt/yarn-1.16.0/bin"
or export PATH="$PATH:
yarn global bin"
Ok, I have added the following ENV variables:
...
ENV PATH="$PATH:/opt/yarn-1.16.0/bin"
ENV PATH="$PATH:`yarn global bin`"
RUN bin/rake assets:precompile
I still get Yarn executable was not detected in the system.
.
Please check what where yarn
outputs.
Ah sorry, please try "$PATH:/opt/yarn-v1.16.0/bin"
instead of "$PATH:/opt/yarn-1.16.0/bin".
I still get Yarn executable was not detected in the system.
.
Can you check where yarn is installed to and if the path /opt/yarn-v1.16.0/bin
is correct?
Currently the issue is more about manually installing Yarn in a specific environment.
In your case it seems you have to find the right directory for the binaries to add them to the PATH.
Not sure if it works with ENV
or if the export
command is needed to do it right after the install step (check the output of PATH
).
Can you check where yarn is installed to and if the path /opt/yarn-v1.16.0/bin is correct?
I really appreciate your support, however how can I check that? I am not the developer of Yarn, you should document that somewhere clearly. Otherwise, how am I supposed to know that?
BTW Rails suggests to use Ruby as the official Docker image and Rails uses Yarn... So installing Yarn on the Ruby image shouldn't be so difficult. This issue should also be simple to reproduce using a Dockerfile similar to the one I have posted at the beginning.
I really appreciate your support, however how can I check that? I am not the developer of Yarn, you should document that somewhere clearly. Otherwise, how am I supposed to know that?
I would "debug" this by running ls -al
around and also use find
to check where the needed binaries are and what can be found in /opt/
.
BTW Rails suggests to use Ruby as the official Docker image and Rails uses Yarn... So installing Yarn on the Ruby image shouldn't be so difficult.
Well then it should be filed there as issue, see https://github.com/docker-library/ruby/issues
I will try to recreate this using CI.
Great, I have found the problem. Compared to your solution, I was not running the apt-get update
a second time, after the scripts, and thus apt-get was installing an old version of yarn, which was not recognized by Rails.
For future reference, this is a compact solution that works properly:
FROM ruby:2.5.5
RUN curl https://deb.nodesource.com/setup_12.x | bash
RUN curl https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y nodejs yarn postgresql-client
...
Most helpful comment
Great, I have found the problem. Compared to your solution, I was not running the
apt-get update
a second time, after the scripts, and thus apt-get was installing an old version of yarn, which was not recognized by Rails.For future reference, this is a compact solution that works properly: