1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Dockerfile】Next.js のコンテナを作成する

Posted at

はじめに

この記事では、Dockerfile からビルドしたイメージを元に Next.js のコンテナを作成する手順について記載します。

開発環境

開発環境は以下の通りです。

  • Windows11
  • Docker Engine 26.1.1
  • Node.js 20.17.0
  • npm 10.8.2
  • Next.js 14.2.8
  • React 18

Next.js アプリケーションの作成

まずは Next.js アプリケーションを作成します。

npx create-next-app@latest

インストール中の質問は、以下のように回答します。

image.png

ローカルサーバーを起動します。

cd next
npm run dev

初期画面が表示されます。

image.png

Dockerfile の作成

Dockerfile を作成します。

FROM node:20-alpine

WORKDIR /app

COPY . .

RUN npm install
RUN npm run build

CMD ["npm", "start"]

イメージのビルド

作成した Dockerfile からイメージをビルドします。

docker image build --tag next-image:0.0.1 .

image.png

イメージがビルドできたか確認します。

docker image ls next-image

image.png

コンテナの起動

ビルドしたイメージからコンテナを起動します。

docker container run `
--name app           `
--rm                 `
--publish 3000:3000  `
next-image:0.0.1

image.png

http://localhost:3000 にアクセスすると、先ほどと同じ初期画面が表示されます。

image.png

関連

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?