0
0

More than 1 year has passed since last update.

Node.js で小さな Docker イメージを求めて

Last updated at Posted at 2022-12-11

小さなイメージを求めた時の記録

Node.js のイメージを小さくするという記事を読んで、以前、使っていた Dockerfile ではどの程度のサイズだったかを記録しておきたく思った。
読んだ記事と同じアプリケーション(Node.js Web アプリケーションを Docker 化する | Node.js)で。

ポイントは 2 つ。

環境

  • Docker 20.10.21
mkdir app
cd app

touch package.json
touch app.js
touch Dockerfile
package.json
{
  "scripts": {
    "build": "ncc build app.js --minify --out ncc-minify"
  },
  "dependencies": {
    "express": "^4.18.2"
  },
  "devDependencies": {
    "@vercel/ncc": "^0.36.0"
  }
}
app.js
"use strict";
const express = require("express");

const PORT = 8080;
const HOST = "0.0.0.0";

const app = express();
app.get("/", (req, res) => {
  res.send("Hello World");
});

app.listen(PORT, HOST, () => {
  console.log(`Running on http://${HOST}:${PORT}`);
});
Dockerfile
FROM node:18 AS build

WORKDIR /app
COPY . .

RUN npm i
RUN npm run build

FROM alpine:3.17 AS production

RUN apk add --no-cache nodejs~=18

COPY --from=build /app/ncc-minify/ /app/

EXPOSE 8080
CMD ["node", "/app/index.js"]

ビルド

docker build -t nodejs18-min .

イメージ確認

docker images
REPOSITORY             TAG           IMAGE ID       CREATED             SIZE
nodejs18-min           latest        618d0cf78ed4   53 seconds ago      55.2MB

実行

docker run --init -it -p 8080:8080 nodejs18-min

動作確認

curl localhost:8080

最後に

次やるときは、見た記事でもやっていた distroless だな。

以上

0
0
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
0
0