LoginSignup
22
15

More than 5 years have passed since last update.

Alpine Linux に node-gyp のビルドが必要な npm モジュールを入れる

Last updated at Posted at 2017-12-06

node-gyp に依存しているモジュール (例: mdns ) を Alpine Linux 上でインストールすると、ビルドに必要なパッケージが足りななくてインストールに失敗する。

Dockerfile
FROM node:alpine

RUN npm install mdns
$ docker build .
(略)
mdns@2.3.4 install /node_modules/mdns
node-gyp rebuild
(略)
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
(略)

ビルドに必要な python / make / g++ を入れれば良いのだが、せっかく軽量な Alpine を使ってるのに Docker イメージが重くなってしまう。

そこで apk の --virtual NAME オプションを指定しておくことで、ビルドのためだけに入れたパッケージをビルド後にまとめて削除する。

Dockerfile
FROM node:alpine

RUN apk add --no-cache --virtual .gyp python make g++ \
    && apk --no-cache add avahi-dev \
    && npm install mdns \
    && apk del .gyp

avahi-devmdns が依存しているパッケージ

削除することでイメージサイズを 310MB → 165MB まで減らすことができた。

参考: Alpine Linux で軽量な Docker イメージを作る - Qiita

22
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
15