概要
今までDockerの存在は知っていたものの,中々手をつけていませんでした.
諸事情でDockerを使わなければならなくなったので,まず公式のチュートリアルをやってみました.
このチュートリアルはPart1からPart 10までで構成され,Dockerの様々な機能に触れられるようになっています.
Part 1はほとんどセットアップでPart 2から実践的な内容になっています.
このPart 2でいきなり躓きました.
指示に従ってDockerfileを記述してコンテナをビルドするという内容だったのですが,きちんとDockerfileを記述してもビルドできませんでした.
同じようにチュートリアルで躓く人が出ないように解決策のメモを残しておきます.
Part2: Sample applicationの概要
Part2ではNode.jsで書かれた簡単なTo doアプリを起動するという内容になっています.
やることとしては
- ソースコードをダウンロード
- Dockerfileを記述(コピペすれば良い)
- ビルド
- ラン
というシンプルな内容になっています.
ところが,チュートリアルに書いてあるDockerfileの通りだとビルドがうまくいきませんでした.
遭遇したエラー
チュートリアルに書いてあるDockerfileは以下のものです.
FROM node:12-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
これをコピペして以下のコマンドでビルドしました.
docker build -t getting-started .
すると以下のエラーが発生しました(一部抜粋).
#8 6.598 gyp ERR! find Python **********************************************************
#8 6.598 gyp ERR! find Python You need to install the latest version of Python.
#8 6.598 gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
#8 6.598 gyp ERR! find Python you can try one of the following options:
#8 6.598 gyp ERR! find Python - Use the switch --python="/path/to/pythonexecutable"
#8 6.598 gyp ERR! find Python (accepted by both node-gyp and npm)
#8 6.598 gyp ERR! find Python - Set the environment variable PYTHON
#8 6.598 gyp ERR! find Python - Set the npm configuration variable python:
#8 6.598 gyp ERR! find Python npm config set python "/path/to/pythonexecutable"
#8 6.598 gyp ERR! find Python For more information consult the documentation at:
#8 6.598 gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
#8 6.598 gyp ERR! find Python **********************************************************
pythonが見つからない.pythonをインストールする必要があると言われています.
Docker初心者の私はpythonのインストールをどのように行えばいいか知らなかったのですが,色々調べた結果,以下のようにDockerfileを書けば良いと分かりました.
FROM node:12-alpine
RUN apk add --no-cache python
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
二行目のRUN apk add --no-cache python
でpythonをインストールしています.
これでビルドしたところ,再度エラーが発生しました.
#9 7.443 gyp ERR! stack Error: not found: make
#9 7.443 gyp ERR! stack at getNotFoundError (/usr/local/lib/node_modules/npm/node_modules/which/which.js:13:12)
#9 7.443 gyp ERR! stack at F (/usr/local/lib/node_modules/npm/node_modules/which/which.js:68:19)
#9 7.443 gyp ERR! stack at E (/usr/local/lib/node_modules/npm/node_modules/which/which.js:80:29)
今度はmake
がないと怒られています.
そのためpythonと同じようにmakeもインストールを行います.
FROM node:12-alpine
RUN apk add --no-cache python make
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
これでいける!と思いましたが,またまたビルドに失敗しました.
今度は以下のエラーが出ました.
#10 7.250 make: cc: Command not found
#10 7.250 make: *** [deps/sqlite3.target.mk:141: Release/obj.target/sqlite3/gen/sqlite-autoconf-3280000/sqlite3.o] Error 127
#10 7.250 make: Leaving directory '/app/node_modules/sqlite3/build'
#10 7.250 gyp ERR! build error
#10 7.250 gyp ERR! stack Error: `make` failed with exit code: 2
どうやらcc
が見つからなくてmake
に失敗したようです.
cc
って何?と思いましたが,どうやらg++
をインストールすればいいようです.
g++
もpythonと同様にインストールします.
Dockerfile完成形
FROM node:12-alpine
RUN apk add --no-cache python make g++
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
このDockerfileでビルドしたところ,ようやくビルドに成功しました!
その後以下のコマンドでコンテナを起動し,
docker run -dp 3000:3000 getting-started
http://localhost:3000 にアクセスすることで無事にTo doアプリが起動しました.
めでたし.めでたし.
実行環境
M1 mac book air
Docker version 20.10.3, build 48d30b5 (preview版)
追記
Part6のUse bind mountsでも同様のエラーが発生しました.
以下が入力すべきコマンドでしたが,
docker run -dp 3000:3000 \
-w /app -v "$(pwd):/app" \
node:12-alpine \
sh -c "yarn install && yarn run dev"
これだとpython not found
が出ます.
同様にapk add --no-cache python make g++
を以下のように追加したら動きました.
docker run -dp 3000:3000 \
-w /app -v "$(pwd):/app" \
node:12-alpine \
sh -c "apk add --no-cache python make g++ && yarn install && yarn run dev"
疑問
チュートリアル自体に問題があってビルドできなかったのでしょうか?
それともM1バージョンのDockerに問題があるのでしょうか?...
そのあたりは現在他の環境がないため自分は良く分かっていません.