[開発] WSLとDockerを活用したReactサーバー構築ガイド

Docker

最近Reactの構築が出来るようになってきたので、Webアプリを作るついでに備忘録的に手順を残します。

まずはGithubにてリポジトリを作成します。
今回はサーバーのテンプレートを作るため server_template というリポジトリ名にしました。
https://github.com/okzaq/server_template

今後の開発を進めやすくするため、VisualStudioCodeのリモートエクスプローラーという拡張機能でWSLにアタッチします。

  1. VisualStudioCodeでWSLにアタッチする
  2. ホームディレクトリを開く
  3. ホームディレクトリ内に server_template ディレクトリを作成
  4. 作成した server_template ディレクトリを開く
  5. ターミナルを開いたときに okzaq@PC名:~/server_template$ とディレクトリ名が表示されていれば準備完了

コマンドを実行する準備ができたので、リポジトリの初期化を進めます。

git init
# 最初のコミットはrebaseなどで変更しづらいため --allow-empty オプションを付けて空のコミットにする
git commit --allow-empty -m "first commit"
git branch -M main
git remote add origin https://github.com/okzaq/server_template.git
git push -u origin main

次はReactの環境を構築します。
今回デプロイ予定のサーバーはXserverで調べてみるとnodeのバージョンは16.4.2だったのですが、調べてみるとXserverに20系のnodeを入れる手順があるようなので、20系のイメージを使ってDocker上にReact環境を構築します。

# frontend用のディレクトリを作成
mkdir frontend
# docker-compose.yml
version: '3.8'
services:
  frontend:
    build:
      context: ./
      dockerfile: ./Dockerfile_frontend  # 今後バックエンドも追加予定のためサフィックスを付与
    container_name: sample_frontend
    volumes:
      - "./frontend:/workspace"
    ports:
      - "3000:3000"
    tty: true
# Dockerfile_frontend
FROM node:20-buster  # VSCodeでアタッチできないとデバッグがしづらいためbusterを指定
WORKDIR /workspace

# -------------------------------------------------
# [1] 初期構築
# -------------------------------------------------

RUN apt-get update
RUN apt-get -y install locales && \
    localedef -f UTF-8 -i ja_JP ja_JP.UTF-8
ENV LANG ja_JP.UTF-8 \
    LANGUAGE ja_JP:ja \
    LC_ALL ja_JP.UTF-8 \
    TZ JST-9
RUN apt-get install -y vim

次にDockerを起動します。

docker-compose up --build

無事に起動できた場合、以下のようなログが出ます。

Recreating sample_frontend ... done
Attaching to sample_frontend
sample_frontend | Welcome to Node.js v20.12.1.
sample_frontend | Type ".help" for more information.

Dockerが起動できたらコンテナにアタッチしてReactを構築するコマンドを実行します。

cd /workspace/  # 作業ディレクトリに移動
npx create-next-app@latest  # Reactのプロジェクトを新規作成
# 実行結果と設定メモ
root@8b93b597330d:/workspace# npx create-next-app@latest
Need to install the following packages:
create-next-app@14.1.4
Ok to proceed? (y)  ### デフォルトのままEnter
✔ What is your project named? … .  ### workspace直下に展開したいのでドットを指定
✔ Would you like to use TypeScript? … No / Yes  ### デフォルト(Yes)のままEnter
✔ Would you like to use ESLint? … No / Yes  ### デフォルト(Yes)のままEnter
✔ Would you like to use Tailwind CSS? … No / Yes  ### Noを指定してEnter
✔ Would you like to use `src/` directory? … No / Yes  ### デフォルト(No)のままEnter
✔ Would you like to use App Router? (recommended) … No / Yes  ### デフォルト(Yes)のままEnter
✔ Would you like to customize the default import alias (@/*)? … No / Yes  ### デフォルト(No)のままEnter
Creating a new Next.js app in /workspace.

・・・省略・・・

added 316 packages, and audited 317 packages in 1m

125 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
Success! Created workspace at /workspace

# 最後にバージョンをあげるようnoticeが表示されることがありますが無視します

Reactを起動してみて画面が表示されることを確認します。

root@8b93b597330d:/workspace# npm run dev

> workspace@0.1.0 dev
> next dev

   ▲ Next.js 14.1.4
   - Local:        http://localhost:3000

Attention: Next.js now collects completely anonymous telemetry regarding usage.
This information is used to shape Next.js' roadmap and prioritize features.
You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:

Telemetry | Next.js by Vercel - The React Framework
Next.js by Vercel is the full-stack React framework for the web.
✓ Ready in 1521ms

NextJSの画面が表示されていれば起動成功です。

次にDocker起動時にReactが自動で起動されるようにします。
(npm run devをCtrl+cで停止、docker-composeもCtrl+cで停止)

# docker-compose.yml
・・・省略・・・
    ports:
      - "3000:3000"
    tty: true
    command: sh -c "npm run dev"  # 追記、コンテナ起動時にReactが起動されるようにします
# Dockerfile_frontend
・・・省略・・・
RUN apt-get install -y vim

# ここより下を追記

# -------------------------------------------------
# [2] パッケージのインストール
# -------------------------------------------------

COPY frontend/package*.json /workspace
COPY frontend/tsconfig.json /workspace
RUN npm install -g next && npm install

再度Dockerを起動し、NextJSの画面が表示されていればReactのセットアップは完了です。

最後に、Docker内でReactを作ったことでfrontendディレクトリ内がroot権限になってしまっているため、権限を書き換えてGithubにコードをあげます。

# 権限変更(okzaqの部分はユーザー名を入れてください)
sudo chown okzaq:okzaq -R frontend/

# コードをGithubにあげる
git add ./
git commit -m "React環境を構築"
git push

今後はこの server_template にLaravelやMySQLを追加してサーバーのベースを作っていく予定です。
ベースが出来上がればアプリを新規で立ち上げる際に立ち上げのコストが無視できるようになるので、開発スピードが上がることを期待しています。

コメント

タイトルとURLをコピーしました