DockerのNode公式イメージを利用してServerless Frameworkをインストールする
Serverless Frameworkを試すにあたりDockerfileを作成したがちょっとハマったのでメモ。
まず最初に
公式サイトを参考に以下のDockerfileを作成した。
FROM node:13.5-alpine RUN npm install -g serverless
実行すると以下のように"update check failed"となる。 update checkだから問題なさそうだが、root権限での実行は微妙。
npm WARN deprecated superagent@3.8.3: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header). This notice will go away with v5.0.2+ once it is released. /usr/local/bin/serverless -> /usr/local/lib/node_modules/serverless/bin/serverless.js /usr/local/bin/slss -> /usr/local/lib/node_modules/serverless/bin/serverless.js /usr/local/bin/sls -> /usr/local/lib/node_modules/serverless/bin/serverless.js > serverless@1.60.5 postinstall /usr/local/lib/node_modules/serverless > node ./scripts/postinstall.js ┌───────────────────────────────────────────────────┐ │ │ │ Serverless Framework successfully installed! │ │ │ │ To start your first project run “serverless”. │ │ │ └───────────────────────────────────────────────────┘ ┌───────────────────────────────────────────────────┐ │ serverless update check failed │ │ Try running with sudo or get access │ │ to the local update config store via │ │ sudo chown -R $USER:$(id -gn $USER) /root/.config │ └───────────────────────────────────────────────────┘ npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.2 (node_modules/serverless/node_modules/fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"}) + serverless@1.60.5 added 519 packages from 334 contributors in 29.894s
次に試したこと
nodeユーザーにしてインストールを行った。
FROM node:13.5-alpine USER node RUN npm install -g serverless
すると、Permissionエラーが発生した。
npm WARN deprecated superagent@3.8.3: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header). This notice will go away with v5.0.2+ once it is released. npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules npm ERR! code EACCES npm ERR! syscall access npm ERR! path /usr/local/lib/node_modules npm ERR! errno -13 npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules' npm ERR! [Error: EACCES: permission denied, access '/usr/local/lib/node_modules'] { npm ERR! stack: "Error: EACCES: permission denied, access '/usr/local/lib/node_modules'", npm ERR! errno: -13, npm ERR! code: 'EACCES', npm ERR! syscall: 'access', npm ERR! path: '/usr/local/lib/node_modules' npm ERR! } npm ERR! npm ERR! The operation was rejected by your operating system. npm ERR! It is likely you do not have the permissions to access this file as the current user npm ERR! npm ERR! If you believe this might be a permissions issue, please double-check the npm ERR! permissions of the file and its containing directories, or try running npm ERR! the command again as root/Administrator. npm ERR! A complete log of this run can be found in: npm ERR! /home/node/.npm/_logs/2020-01-05T13_39_33_482Z-debug.log
最終的に
こちらを参考にして解消した。
グローバルインストール用にNPM_CONFIG_PREFIXを変更するのがキモ。
FROM node:13.5-alpine RUN apk update USER node ENV NPM_CONFIG_PREFIX /home/node/.npm-global RUN npm install -g serverless
root実行時に表示された"update check failed"が表示されなくなった。
npm WARN deprecated superagent@3.8.3: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header). This notice will go away with v5.0.2+ once it is released. /home/node/.npm-global/bin/serverless -> /home/node/.npm-global/lib/node_modules/serverless/bin/serverless.js /home/node/.npm-global/bin/slss -> /home/node/.npm-global/lib/node_modules/serverless/bin/serverless.js /home/node/.npm-global/bin/sls -> /home/node/.npm-global/lib/node_modules/serverless/bin/serverless.js > serverless@1.60.5 postinstall /home/node/.npm-global/lib/node_modules/serverless > node ./scripts/postinstall.js ┌───────────────────────────────────────────────────┐ │ │ │ Serverless Framework successfully installed! │ │ │ │ To start your first project run “serverless”. │ │ │ └───────────────────────────────────────────────────┘ npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.2 (node_modules/serverless/node_modules/fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"}) + serverless@1.60.5 added 519 packages from 334 contributors in 25.324s
続・最終的に
上記のままだと、インストールは完了するがPATHが通っていないため実行できない。
下記のようにしてPATHを通す。
FROM node:13.5-alpine RUN apk update USER node ENV NPM_CONFIG_PREFIX /home/node/.npm-global RUN npm install -g serverless ENV PATH $PATH:/home/node/.npm-global/bin