[小ネタ] コンテナにしたチャットアプリのDockerイメージ軽量化

Dockerイメージの簡単な軽量化を試してみました!
2022.02.28

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは!コンサル部のinomaso(@inomasosan)です。

今回は前回コンテナにしたチャットアプリのDockerイメージを軽量化していきます。

軽量化前のチャットアプリのイメージサイズ

node:16.14を元にDockerイメージを作成したのですが、イメージのサイズが914MBもありました。

% docker images
REPOSITORY            TAG       IMAGE ID       CREATED       SIZE
socket-chat-example   0.0.1     75bf138a922a   12 days ago   914MB

原因はなんなのか?

Dockerfile

前回作成したDockerfileから、もう一度確認していきます。

FROM node:16.14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "node", "index.js" ]

RUNコマンドは一つで、npm installpackage.jsonの依存関係にあるパッケージしかインストールしていないので、こちらが原因ではなさそうです。

ベースのイメージサイズ

そもそもベースのイメージサイズがどれくらいだっけと確認したところ、なんと906MBありました。。。

% docker pull node:16.14
16.14: Pulling from library/node
a024302f8a01: Already exists
289773030fdc: Already exists
81bb8b3399fe: Already exists
9c63da771697: Already exists
bcf1b23b1e4b: Already exists
53a5e10666cb: Already exists
c9872a0dc1d7: Already exists
e8b67599ff06: Already exists
dd019e527248: Already exists
Digest: sha256:fd86131ddf8e0faa8ba7a3e49b6bf571745946e663e4065f3bff0a07204c1dde
Status: Downloaded newer image for node:16.14
docker.io/library/node:16.14
% docker images node
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
node         16.14     1e151315aa91   2 weeks ago   906MB

というわけで、もっと軽量なベースイメージを探す必要があるとわかりました。

別のベースイメージを探す

DockerHubのnodeを確認していくと、デフォルトイメージ以外だとnode:-alpineかnode:-slimがありました。

ただ、alpineに以下の説明があり、libcの要件や前提条件によっては、ソフトウェアで問題が発生するとのことでした。

The main caveat to note is that it does use musl libc instead of glibc and friends, so software will often run into issues depending on the depth of their libc requirements/assumptions.

一方、slimに以下の説明がありましたが、動作の不具合が発生するような記述がなかったのでこちらを使用してみます。

This image does not contain the common packages contained in the default tag and only contains the minimal packages needed to run node. Unless you are working in an environment where only the node image will be deployed and you have space constraints, we highly recommend using the default image of this repository.

slimでDockerイメージ作成

まずはnode:16.14-slimのサイズを確認していくと175MBと大分小さくなりました!

% docker pull node:16.14-slim
16.14-slim: Pulling from library/node
6552179c3509: Pull complete
d4debff14640: Pull complete
3602b62b4939: Pull complete
2e9deffe3445: Pull complete
3dcfed3bb34d: Pull complete
Digest: sha256:64dc41bcf5e9048aa1b9a6efe3af68631720e6b76e98f281a77d305e898d3610
Status: Downloaded newer image for node:16.14-slim
docker.io/library/node:16.14-slim
% docker images node:16.14-slim
REPOSITORY   TAG          IMAGE ID       CREATED       SIZE
node         16.14-slim   cecb3f1a7417   2 weeks ago   175MB

DockerfileのFROMのベースイメージを修正します。

FROM node:16.14-slim
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "node", "index.js" ]

さっそくイメージを作成していきます。 イメージ名のタグは0.0.2に修正しました。

% docker build -t socket-chat-example:0.0.2 .

最終的なイメージのサイズを確認したところ183MBと大分小さくなりました!!

% docker images socket-chat-example:0.0.2
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
socket-chat-example   0.0.2     caf3208f422b   2 minutes ago   183MB

動作確認

試しにローカル環境で、作成したDockerイメージからコンテナ起動していきます。

% docker run -p 3000:3000 -d socket-chat-example:0.0.2

前回と同様に、ブラウザのURLにhttp://localhost:3000入力し、チャットアプリが正常に動作することを確認できました!

参考URL

まとめ

DockerfileのFROMを修正するだけで、Dockerイメージを軽量化することができました。

この記事が、どなたかのお役に立てば幸いです。それでは!