チュートリアル:Node.js Express アプリを Docker 化し、Redis データベースを利用する
WebStorm を使って、複数の Docker コンテナーで実行されている Node.js Express アプリケーションの実行やデバッグができます。
このチュートリアルでは、同じ仮想ネットワーク内のコンテナーで2つのDocker Composeサービス(シンプルなNode.js ExpressアプリケーションとRedisデータベース)を実行する方法について説明します。
このチュートリアルでは、以下を行います:
HTTP リクエストの回数をカウントし、その回数をデータベースでインクリメントする Node.js Express アプリケーションを作成します。
このアプリケーションのために Dockerfile を作成してください。
カウンターアプリケーションとRedisデータベース、2つのサービスを接続するための DockerCompose ファイルを作成します。
アプリケーションのイメージと Redis イメージをビルド、デプロイ、実行して、それぞれ Docker コンテナーで動かします。
ブラウザーでアプリケーションにアクセスし、ページを更新するたびにカウンターが実際に増加しているか確認します。
始める前に
Node.js アプリケーションを作成する
Welcome 画面で 新規プロジェクト をクリックするか、メインメニューから を選択してください。
開いたダイアログで、左側のペインで Node.js を選択します。
アプリケーションの場所と名前(例:
counter)を指定します。使用するローカル Node.js ランタイムを指定します。 推奨インストールを承認するか、リストから別のものを選択するか、まだマシンに Node.js がインストールされていない場合は ダウンロード をクリックしてください。 詳しくは 新しい Node.js アプリケーションを作成するを参照してください。

作成 をクリックしてください。
WebStorm はプロジェクトスタブを生成し、エディターで開きます。
アプリケーションを実装する
アプリケーションはJavaScriptファイル index.js で構成されます。
プロジェクト ツールウィンドウ Alt+1 を開き、プロジェクトフォルダーを右クリックし、コンテキストメニューから を選択して、 を選択します。

表示されるポップアップダイアログで、ファイル名 (例: index.js ) を指定します。

WebStorm はファイル index.js を作成し、エディターで開きます。
index.js ファイルに次のコードを入力してください:
const express = require('express'); const redis = require('redis'); const app = express(); // The host name 'redis-server' will be specified in docker-compose.yml const client = redis.createClient({ url: 'redis://redis-server:6379' }); client.on('error', (err) => console.log('Redis Client Error', err)); app.get('/', async (req, res) => { try { // The `incr` command atomically increments the value stored at the key by 1. // If the key does not exist, it is created with the value 0 before the increment. const visits = await client.incr('visits'); res.send(`The number of visits is ${visits}`); } catch (err) { res.status(500).send('Error while accessing Redis'); } }); // First we connect to Redis, and then we run the server app.listen(3000, async () => { await client.connect(); console.log('Server running at port 3000'); });WebStorm は、Express フレームワークと Redis 用クライアントが不足していることを検出し、それらへの参照を未解決としてハイライトします。
未解決の参照にカーソルを合わせ、 Alt+Enter を押し、 または を選択します。

アプリケーションをコンテナ化する
Redis には用意されたイメージを利用し、アプリの Web 部分については Dockerfile を作成します。
Dockerfile を作成する
プロジェクトフォルダーのコンテキストメニューから を選択し、続いて を選択します。

新しく作成された Dockerfile を開き、次のコードを入力します。
# Specify a light-weight Node.js image FROM node:22-alpine # Specify the working directory inside the container WORKDIR /usr/src/app COPY package*.json ./ RUN npm install --production COPY . . # Expose the container's port EXPOSE 3000 # Specify the command to launch application CMD ["node", "index.js"]
アプリケーションの2つの部分を docker-compose.yml ファイルで結び付ける必要があります。
docker-compose.yml ファイルを作成します。
プロジェクトフォルダーのコンテキストメニューから を選択し、次に を選択します。

表示されたポップアップで、デフォルトの Docker Compose ファイル名をそのまま使用するか、カスタム名を指定して Enter を押します。

新しく作成された docker-compose.yml を開き、次のコードを入力します。
services: redis-server: image: redis:alpine web: # The build directive instructs Docker Compose to build # an image of our application based on the Dockerfile # from the current folder build: . # Bind the local port with port inside the container ports: - "3000:3000" # The depends_on directive ensures that # the container starts after the redis-server depends_on: - redis-server
アプリケーションを実行する
docker-compose.yml で
を
servicesの横でクリックし、コンテキストメニューから を選択してdocker compose up -dコマンドを実行します。
WebStorm は最初に redis-server Docker Compose サービスを起動し、データベースが正常であることを確認します。 その後、 web サービスを起動します。 すべて正しく設定されていれば、 サービス ツールウィンドウに次の内容が表示されます:

web ノード下の counter-web-1 をクリックすると、 Server running at port 3000 出力が表示されます:

アプリケーションが期待通りに動作するかを確認する
ブラウザーで
http://localhost:3000を開きます。 ページが開き、The number of visits is 1が表示されます。ページを数回更新して、カウンターが増加していることを確認してください。