Rails6 on Dockerの環境構築 - Node.jsのバージョン指定
Rails6 on Dockerの環境構築 - hvystnの日記
Rails6 on Dockerの環境構築 - Dockerイメージのバージョン指定 - hvystnの日記
上記の2つの記事でRails6の環境構築と、PostgreSQLのバージョン指定を実施しました。
今回はRails6の環境構築時にインストールしていたNodejsのバージョンも指定してみます。
Node.jsのインストールは方法はどこでわかりますか?
Node.js公式ページにアクセスし、インストール方法を確認します。
下記順番にクリックしていきます。
ダウンロード>
パッケージマネージャを使用したダウンロード>
Debian と Ubuntu ベースの Linux ディストリビューション、エンタープライズ Linux/Fedora と Snap パッケージ
ここに「Node.js 公式のバイナリディストリビューションが NodeSource によって提供されています。」との文言があり、こちらもリンクになっているので、クリックします。
するとGithubのページに飛ばされれます。
Node.jsのソースコードはここで管理されているようです。
READMEを読むと、Debian and Ubuntu based distributions (deb)>Installation instructionsにインストール方法が記載されていますので、こちらに従ってインストールを実施していきます。
Node.jsのバージョンはどれを使用しますか?
Node.jsにはいくつかバージョンがありますが、現状LTSとなっているバージョン12.xにしたいと思います。
LTSについてはLTS、STSとはを参照してください。
そもそも、現状はapt-get install nodejsでNode.jsをインストールしていましたが、どのバージョンが入っているのか調べてみました。
apt-cache policyコマンドでapt-getによってインストールされる予定のイメージやインストール済みのイメージを確認できるそうです。
$ docker-compose exec web bash root@1d81f8131ca3:/myapp# apt-cache policy nodejs nodejs: Installed: 10.21.0~dfsg-1~deb10u1 Candidate: 10.21.0~dfsg-1~deb10u1 Version table: *** 10.21.0~dfsg-1~deb10u1 500 500 http://deb.debian.org/debian buster/main amd64 Packages 500 http://security.debian.org/debian-security buster/updates/main amd64 Packages 100 /var/lib/dpkg/status
Installedがインストール済みのバージョンなので、Node.jsのv10.xがインストールされているようです。
上記の例ではVersion tableに記載されているバージョンは一つですが、他のパッケージではVersion tableには複数バージョンの取得先が記載されることもあり、そのtableの中のバージョン一つをCandidateとして指定し、apt-get installコマンドでそのバージョンのパッケージをインストールするようです。
同様にNode.jsのInstallationでも、Version tableに新しいバージョンを追加し、Candidateを書き換えて、インストールするバージョンを変更するようです。
GithubのNode.jsのREADME>Installation instructions>Node.js v12.x:に従い、実際に進めていきます。
ちなみに、Node.js LTS (v12.x):の項目もありますが、Node.js v12.x:の方で進めます。
なぜ、Node.js LTS (v12.x):ではなく、Node.js v12.x:を選択しますか?
この理由は、前回のDockerイメージのバージョン指定の必要性で記載した理由と同じです。
Node.js LTSのバージョンは今でこそv12.xですが、Node.jsの開発が進むとv14.xやv16.xに変更される可能性があります。
Node.jsがv12.xでうまく動いていたのに、LTSの付け替えでv14.xをインストールしてしまい、Rails6が動かなくなるという可能性がありますので、Node.jsのバージョンはv12.xに固定したいと思います。
よってNode.js LTS (v12.x):ではなく、Node.js v12.x:の項目に従い、インストールを進めます。
Node.js v12.xのインストールはどのように実施しますか?
Rails6用のrubyイメージのOSはDebianなので、Node.js v12.x:># Using Debian, as rootのコマンドを実施します。
今回、Dockerfileにコマンドを記載する前に、手動でインストールする方法を試してみます。
$ docker-compose exec web bash root@d4b3b0f7947f:/myapp# curl -sL https://deb.nodesource.com/setup_12.x | bash - ## Installing the NodeSource Node.js 12.x repo... ## Populating apt-get cache... + apt-get update Hit:1 http://deb.debian.org/debian buster InRelease Get:2 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB] Get:3 http://deb.debian.org/debian buster-updates InRelease [51.9 kB] Hit:4 https://dl.yarnpkg.com/debian stable InRelease Get:5 http://security.debian.org/debian-security buster/updates/main amd64 Packages [224 kB] Fetched 341 kB in 1s (399 kB/s) Reading package lists... Done ## Confirming "buster" is supported... + curl -sLf -o /dev/null 'https://deb.nodesource.com/node_12.x/dists/buster/Release' ## Adding the NodeSource signing key to your keyring... + curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add - OK ## Creating apt sources list file for the NodeSource Node.js 12.x repo... + echo 'deb https://deb.nodesource.com/node_12.x buster main' > /etc/apt/sources.list.d/nodesource.list + echo 'deb-src https://deb.nodesource.com/node_12.x buster main' >> /etc/apt/sources.list.d/nodesource.list ## Running `apt-get update` for you... + apt-get update Hit:1 http://deb.debian.org/debian buster InRelease Hit:2 http://security.debian.org/debian-security buster/updates InRelease Hit:3 http://deb.debian.org/debian buster-updates InRelease Hit:4 https://dl.yarnpkg.com/debian stable InRelease Get:5 https://deb.nodesource.com/node_12.x buster InRelease [4584 B] Get:6 https://deb.nodesource.com/node_12.x buster/main amd64 Packages [767 B] Fetched 5351 B in 1s (3640 B/s) Reading package lists... Done ## Run `sudo apt-get install -y nodejs` to install Node.js 12.x and npm ## You may also need development tools to build native addons: sudo apt-get install gcc g++ make ## To install the Yarn package manager, run: curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list sudo apt-get update && sudo apt-get install yarn
今気づきましたが、Yarnのインストール方法もここに記載されてるんですね〜親切ですね!
apt-cache policyでCandidateが変更されたか確認します。
root@d4b3b0f7947f:/myapp# apt-cache policy nodejs nodejs: Installed: 10.21.0~dfsg-1~deb10u1 Candidate: 12.18.3-1nodesource1 Version table: 12.18.3-1nodesource1 500 500 https://deb.nodesource.com/node_12.x buster/main amd64 Packages *** 10.21.0~dfsg-1~deb10u1 500 500 http://deb.debian.org/debian buster/main amd64 Packages 500 http://security.debian.org/debian-security buster/updates/main amd64 Packages 100 /var/lib/dpkg/status root@d4b3b0f7947f:/myapp#
ちゃんとv12.xに変更されていることが確認できました。
インストールもして、再度確認してみます。
root@d4b3b0f7947f:/myapp# apt-get install -y nodejs Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: libbrotli1 libc-ares2 libuv1 nodejs-doc Use 'apt autoremove' to remove them. The following packages will be REMOVED: libnode64 The following packages will be upgraded: nodejs 1 upgraded, 0 newly installed, 1 to remove and 2 not upgraded. Need to get 18.2 MB of archives. After this operation, 69.3 MB of additional disk space will be used. Get:1 https://deb.nodesource.com/node_12.x buster/main amd64 nodejs amd64 12.18.3-1nodesource1 [18.2 MB] Fetched 18.2 MB in 3s (7145 kB/s) debconf: delaying package configuration, since apt-utils is not installed dpkg: libnode64:amd64: dependency problems, but removing anyway as you requested: nodejs depends on libnode64 (= 10.21.0~dfsg-1~deb10u1). (Reading database ... 24492 files and directories currently installed.) Removing libnode64:amd64 (10.21.0~dfsg-1~deb10u1) ... (Reading database ... 24485 files and directories currently installed.) Preparing to unpack .../nodejs_12.18.3-1nodesource1_amd64.deb ... Unpacking nodejs (12.18.3-1nodesource1) over (10.21.0~dfsg-1~deb10u1) ... Setting up nodejs (12.18.3-1nodesource1) ... Processing triggers for libc-bin (2.28-10) ... root@d4b3b0f7947f:/myapp# apt-cache policy nodejs nodejs: Installed: 12.18.3-1nodesource1 Candidate: 12.18.3-1nodesource1 Version table: *** 12.18.3-1nodesource1 500 500 https://deb.nodesource.com/node_12.x buster/main amd64 Packages 100 /var/lib/dpkg/status 10.21.0~dfsg-1~deb10u1 500 500 http://deb.debian.org/debian buster/main amd64 Packages 500 http://security.debian.org/debian-security buster/updates/main amd64 Packages root@d4b3b0f7947f:/myapp# nodejs --version v12.18.3
Installedがv12.xに変更され、nodejs --versionコマンドでもv12.xがインストールされていることを確認できました。
この手順で問題ないようですので、上記の手順をDockerfileに追加します。
[update]nodejs version to fix · heavy-stone/rails6-docker@5dbf44a · GitHub
一から作り直すので、プロジェクトや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コマンドでv12.xのnode.jsがインストールされていることも確認できました。
まとめ
Node.jsをバージョン指定してインストールする方法を実施しました。
参考
今後の課題
・バージョン指定は大切と言いながら、YarnはStable versionを使用しており、バージョン指定できてなかった。。