M1 Mac でDocker Tutorialにハマったときの備忘録

M1 Macで[Docker Tutorial](https://docs.docker.com/get-started/02_our_app/)をやってメチャクチャハマったので備忘録。

## 環境

```
$ pyenv versions
  system
  3.6.10
  3.7.0
* 3.9.6 (set by /Users/takuya/.pyenv/version)
$ python --version
Python 2.7.18
$ python3 --version
Python 3.9.6
```
## 現象

Part2の以下コマンドでエラーが出る。

```
$ docker build -t getting-started .
```

原因っぽいエラー文はこれ。

> #13 12.28 gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.

## 解決策

以下の一連のGitHub IssueにあるようにDockerfileの`apk add`をpython3からpython2に変えたら行けた。

```Dockerfile
# syntax=docker/dockerfile:1
FROM node:12-alpine
RUN apk add --no-cache python2 g++ make
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]

```

https://github.com/docker/getting-started/issues/227
https://github.com/docker/getting-started/issues/222

## 原因
正直わからないが、M1 Macに標準搭載？のPython3が駄目で、Python2（恐らくPython2.7系）がOKなのは`gyp`というライブラリがpython2でしか動かないから？Docker TutorialのページにもM1 Mac版のDocker Desktopを案内した上でDockerfileの記載例を書いているので、なぜ駄目なのか分からない。。

## エラー全文

```
 => ERROR [5/5] RUN yarn install --production                                                                                                                                                                                   12.4s
------                                                                                                                                                                                                                                
 > [5/5] RUN yarn install --production:                                                                                                                                                                                               
#13 0.222 yarn install v1.22.15                                                                                                                                                                                                       
#13 0.273 warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.                                                                                                                                                                                
#13 0.275 [1/4] Resolving packages...                                                                                                                                                                                                 
#13 0.487 warning Resolution field "ansi-regex@5.0.1" is incompatible with requested version "ansi-regex@^2.0.0"
#13 0.596 [2/4] Fetching packages...
#13 9.992 info fsevents@2.3.2: The platform "linux" is incompatible with this module.
#13 9.992 info "fsevents@2.3.2" is an optional dependency and failed compatibility check. Excluding it from installation.
#13 10.00 [3/4] Linking dependencies...
#13 11.11 [4/4] Building fresh packages...
#13 12.28 info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
#13 12.28 error /app/node_modules/sqlite3: Command failed.
#13 12.28 Exit code: 1
#13 12.28 Command: node-pre-gyp install --fallback-to-build
#13 12.28 Arguments: 
#13 12.28 Directory: /app/node_modules/sqlite3
#13 12.28 Output:
#13 12.28 node-pre-gyp info it worked if it ends with ok
#13 12.28 node-pre-gyp info using node-pre-gyp@0.11.0
#13 12.28 node-pre-gyp info using node@12.22.7 | linux | arm64
#13 12.28 node-pre-gyp WARN Using request for node-pre-gyp https download 
#13 12.28 node-pre-gyp info check checked for "/app/node_modules/sqlite3/lib/binding/napi-v3-linux-arm64/node_sqlite3.node" (not found)
#13 12.28 node-pre-gyp http GET https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v5.0.2/napi-v3-linux-arm64.tar.gz
#13 12.28 node-pre-gyp http 403 https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v5.0.2/napi-v3-linux-arm64.tar.gz
#13 12.28 node-pre-gyp WARN Tried to download(403): https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v5.0.2/napi-v3-linux-arm64.tar.gz 
#13 12.28 node-pre-gyp WARN Pre-built binaries not found for sqlite3@5.0.2 and node@12.22.7 (node-v72 ABI, musl) (falling back to source compile with node-gyp) 
#13 12.28 node-pre-gyp http 403 status code downloading tarball https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v5.0.2/napi-v3-linux-arm64.tar.gz 
#13 12.28 gyp info it worked if it ends with ok
#13 12.28 gyp info using node-gyp@3.8.0
#13 12.28 gyp info using node@12.22.7 | linux | arm64
#13 12.28 gyp info ok 
#13 12.28 gyp info it worked if it ends with ok
#13 12.28 gyp info using node-gyp@3.8.0
#13 12.28 gyp info using node@12.22.7 | linux | arm64
#13 12.28 gyp ERR! configure error 
#13 12.28 gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
#13 12.28 gyp ERR! stack     at PythonFinder.failNoPython (/app/node_modules/node-gyp/lib/configure.js:484:19)
#13 12.28 gyp ERR! stack     at PythonFinder.<anonymous> (/app/node_modules/node-gyp/lib/configure.js:406:16)
#13 12.28 gyp ERR! stack     at F (/app/node_modules/which/which.js:68:16)
#13 12.28 gyp ERR! stack     at E (/app/node_modules/which/which.js:80:29)
#13 12.28 gyp ERR! stack     at /app/node_modules/which/which.js:89:16
#13 12.28 gyp ERR! stack     at /app/node_modules/isexe/index.js:42:5
#13 12.28 gyp ERR! stack     at /app/node_modules/isexe/mode.js:8:5
#13 12.28 gyp ERR! stack     at FSReqCallback.oncomplete (fs.js:168:21)
#13 12.28 gyp ERR! System Linux 5.10.76-linuxkit
#13 12.28 gyp ERR! command "/usr/local/bin/node" "/app/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/app/node_modules/sqlite3/lib/binding/napi-v3-linux-arm64/node_sqlite3.node" "--module_name=node_sqlite3" "--module_path=/app/node_modules/sqlite3/lib/binding/napi-v3-linux-arm64" "--napi_version=8" "--node_abi_napi=napi" "--napi_build_version=3" "--node_napi_label=napi-v3"
#13 12.28 gyp ERR! cwd /app/node_modules/sqlite3
#13 12.28 gyp ERR! node -v v12.22.7
#13 12.28 gyp ERR! node-gyp -v v3.8.0
#13 12.28 gyp ERR! not ok 
#13 12.28 node-pre-gyp ERR! build error 
#13 12.28 node-pre-gyp ERR! stack Error: Failed to execute '/usr/local/bin/node /app/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/app/node_modules/sqlite3/lib/binding/napi-v3-linux-arm64/node_sqlite3.node --module_name=node_sqlite3 --module_path=/app/node_modules/sqlite3/lib/binding/napi-v3-linux-arm64 --napi_version=8 --node_abi_napi=napi --napi_build_version=3 --node_napi_label=napi-v3' (1)
#13 12.28 node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/app/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
#13 12.28 node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:314:20)
#13 12.28 node-pre-gyp ERR! stack     at maybeClose (internal/child_process.js:1022:16)
#13 12.28 node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:287:5)
#13 12.28 node-pre-gyp ERR! System Linux 5.10.76-linuxkit
#13 12.28 node-pre-gyp ERR! command "/usr/local/bin/node" "/app/node_modules/sqlite3/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
#13 12.28 node-pre-gyp ERR! cwd /app/node_modules/sqlite3
#13 12.28 node-pre-gyp ERR! node -v v12.22.7
#13 12.28 node-pre-gyp ERR! node-pre-gyp -v v0.11.0
#13 12.28 node-pre-gyp ERR! not ok 
#13 12.28 Failed to execute '/usr/local/bin/node /app/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/app/node_modules/sqlite3/lib/binding/napi-v3-linux-arm64/node_sqlite3.node --module_name=node_sqlite3 --module_path=/app/node_modules/sqlite3/lib/binding/napi-v3-linux-arm64 --napi_version=8 --node_abi_napi=napi --napi_build_version=3 --node_napi_label=napi-v3' (1)
------
```

## その他に試したこと

### Rosetta2でのターミナルの実行
Rosetta2をインストール

```terminal
$ softwareupdate --install-rosetta --agree-to-license
$ uname -m
x86_64
```

「Finder>ユーティリティ>ターミナル>情報を見る>Rosettaを使用して開く」にチェックを入れターミナルを起動。
![スクリーンショット 2021-12-04 22.05.48.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/242610/ba225857-1e97-deae-052c-a08c75d3b574.png)
しかし結果は変わらず。

## Dockerfileのpython3をpythonに

Dockerチュートリアルの日本語訳では、記事が古いのかDockerfileのRUN部分が`apk add --no-cache python3`ではなく`apk add --no-cache python`になっている。しかしこれで`docker build`してもかなり前の段階でエラーで終了して駄目だった。

### Python2.7系で試す
systemのpythonバージョンが2.7.18だったので`pyenv global system`してから`docker build`しても変わらず。
ちなみに`pyenv install 2.7.18`したらエラーが出て駄目だった。

### .bash_profileの中身

```bash
# pyenv
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"
```