hvystnの日記

備忘録です

Rails6 on Dockerの環境構築 - Yarnのバージョン指定

Rails6 on Dockerの環境構築 - hvystnの日記
Rails6 on Dockerの環境構築 - Dockerイメージのバージョン指定 - hvystnの日記
Rails6 on Dockerの環境構築 - Node.jsのバージョン指定 - hvystnの日記
上記の記事でRails6の環境構築、PostgreSQL、Node.jsのバージョン指定を実施しました。

今回はStable versionをインストールしていたYarnのバージョンを指定してみます。
バージョン指定の必要性については以前の記事に記載していますので、ご参照ください。

どうやってYarnのバージョンを指定する方法を見つけますか?

いつも通り、公式サイトを確認してみます。
install#debian-stable | Yarn
しかし、Debianでのインストールページには特にバージョンを指定する方法は見当たりませんでした。
他のページで、代替手段というページを見つけました。
install#alternatives-stable | Yarn
ここに、公式が作成しているインストールスクリプトがあり、こちらを使うとバージョン指定をしてインストールできるようです。
早速Dockerfileを修正して、イメージを削除して初めから実施してみました。

RUN curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.22.5 \
         && curl -sL https://deb.nodesource.com/setup_12.x | bash - \
         && apt-get update -qq && apt-get install -y nodejs postgresql-client

しかし、実行してみると下記エラーメッセージが表示されました。

Step 2/13 : RUN curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.22.5     && curl -sL https://deb.nodesource.com/setup_12.x | bash -     && apt-get update -qq && apt-get install -y nodejs postgresql-client
 ---> Running in 4d9819f5b89b
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    54  100    54    0     0    159      0 --:--:-- --:--:-- --:--:--   159
100  7152  100  7152    0     0   9460      0 --:--:-- --:--:-- --:--:--  9460
Installing Yarn!
> Downloading tarball...

[1/2]: https://yarnpkg.com/downloads/1.22.5/yarn-v1.22.5.tar.gz --> /tmp/yarn.tar.gz.DzI01qASJH
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    80  100    80    0     0    256      0 --:--:-- --:--:-- --:--:--   256
100    93  100    93    0     0    148      0 --:--:-- --:--:-- --:--:--   148
100   643  100   643    0     0    805      0 --:--:-- --:--:-- --:--:--   805
100 1215k  100 1215k    0     0   424k      0  0:00:02  0:00:02 --:--:--  745k

[2/2]: https://yarnpkg.com/downloads/1.22.5/yarn-v1.22.5.tar.gz.asc --> /tmp/yarn.tar.gz.DzI01qASJH.asc
100    84  100    84    0     0    272      0 --:--:-- --:--:-- --:--:--   272
100    97  100    97    0     0    238      0 --:--:-- --:--:-- --:--:-- 97000
100   647  100   647    0     0   1444      0 --:--:-- --:--:-- --:--:--  1444
100   832  100   832    0     0   1264      0 --:--:-- --:--:-- --:--:--  1264
> Verifying integrity...
gpg: key 1646B01B86E50310: public key "Yarn Packaging <yarn@dan.cx>" imported
gpg: Total number processed: 1
gpg:               imported: 1
gpg: Signature made Thu Aug 27 11:16:49 2020 UTC
gpg:                using RSA key 6D98490C6F1ACDDD448E45954F77679369475BAA
gpg: Good signature from "Yarn Packaging <yarn@dan.cx>" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 72EC F46A 56B4 AD39 C907  BBB7 1646 B01B 86E5 0310
     Subkey fingerprint: 6D98 490C 6F1A CDDD 448E  4595 4F77 6793 6947 5BAA
> GPG signature looks good
> Extracting to ~/.yarn...
> Adding to $PATH...
> We've added the following to your /root/.bashrc
> If this isn't the profile of your current shell then please add the following to your correct profile:
   
export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"

Yarn requires Node.js 4.0 or higher to be installed.
> Yarn was installed, but doesn't seem to be working :(.
ERROR: Service 'web' failed to build: The command '/bin/sh -c curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.22.5     && curl -sL https://deb.nodesource.com/setup_12.x | bash -     && apt-get update -qq && apt-get install -y nodejs postgresql-client' returned a non-zero code: 1

Yarn was installed, but doesn't seem to be working :(.

調査してみましたが、修正方法がわからなかったので、この方法は一旦保留にします :(

公式の代替手段ページにはtarball を使用する手動インストールという項目もありましたが、こちらにもバージョン指定の方法は記載されていませんでした。

他の方法を調査したところ、Qiitaの記事を見つけたので、こちらで実施してみます。

qiita.com

どうやってYarnのバージョンを指定してインストールしますか?

Yarnのバージョン指定した.debファイルをYarnのgithubから直接取得し、インストールをしているようです。
Releases · yarnpkg/yarn · GitHub
上記のページの該当ファイルでリンク先のアドレスをコピーすると、wgetで取得するURLと同じことが確認できます。
ちなみに、.debファイルはDebian, Ubuntu などの Linux で利用されるパッケージの拡張子であり、dpkgやaptコマンドでインストールできるようです。
kaworu.jpn.org
Yarnのバージョンは現在最新のv1.22.5にして、Dockerfileを修正します。

RUN wget https://github.com/yarnpkg/yarn/releases/download/v1.22.5/yarn_1.22.5_all.deb \
    && dpkg -i yarn_1.22.5_all.deb \
    && rm yarn_1.22.5_all.deb \
    && curl -sL https://deb.nodesource.com/setup_12.x | bash - \
    && apt-get update -qq && apt-get install -y nodejs postgresql-client

[update]Yarn version to fix · heavy-stone/rails6-docker@7d0f5fd · GitHub

ダウンロードしたファイルはYarnをインストール後に削除するので、rmコマンドを追加しています。
一から作り直すので、プロジェクトやDockerイメージも削除して最初から実施します。

$ docker-compose run web rails new . --force --no-deps --database=postgresql
database.yml修正
$ docker-compose build
$ docker-compose run web rake db:create

作り直しで、いくつかgemに差分が出ましたが、Rails6のデフォルトページの表示確認までできました。
apt-cache policyコマンドで1.22.5のyarnがインストールされていることも確認できました。

まとめ

Yarnをバージョン指定してインストールする方法を実施しました。

参考

github.com

今後の課題

ロケール設定
・クロック設定
・DBをMySQLに変更
・シェルによる自動環境構築化