3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

これはなに?

WSL内に、Nodeが動作するDocker環境を作成してみました

環境

  • Windows11
    • WSL2
      • Ubuntu 20.04
    • Docker Desktop
  • VS Code
    • Remote WSL

準備

必要なファイルを作成します

folder.png

package.json

ファイルがないので、以下のコマンドで作成しました

$ yarn init

作成されたファイルを少し修正して以下のようにしました

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "start": "node main.js"
  }
}

yarn.lock

一旦空ファイルで用意しました

Dockerfile

以下に、部分ごとに説明します

FROM node:lts-bullseye-slim
WORKDIR /app

COPY package.json yarn.lock ./
RUN yarn install

COPY . .
CMD [ "yarn", "start" ]

ベースイメージ

FROM node:lts-bullseye-slim

lts

LTS(Long-Term Support)
ソフトウェアの特定の版(バージョン)を長期間安定的にサポートする
開発元によってそのような長期サポートが約束されたバージョン

bullseye

Debianのコードネームになります

  • bookworm: Debian12
  • bullseye: Debian11

slim

公式に説明がありますが、node を実行するのに必要な最小限のパッケージのみ含まれている

node:-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.

パッケージのインストール

COPY package.json yarn.lock ./
RUN yarn install

アプリケーションに必要なファイルをコピーして、実行

package.jsonに記載したscriptsのコマンドを実行します

COPY . .
CMD [ "yarn", "start" ]

main.js

Hello worldを表示します

console.log("Hello World!");

イメージの作成

VS CodeのTerminalで以下のコマンドを実行します

$ docker build . -t node

コンテナ起動

作成したイメージからコンテナを作成して起動します
※作成したコンテナは終了したら削除するようにしています

実行されて Hello World!が表示されました

$ docker run --rm node

yarn run v1.22.22
warning package.json: No license field
$ node main.js
Hello World!
Done in 0.06s.

最後に

WSL内でDockerコンテナが起動して、Node.jsが動作する環境を用意することができました
誰かの参考になれば幸いです:relaxed:

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?