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

Dockerfileでのビルド時エラー Could not read package.json: Error: ENOENT: no such file or directory の解決法

Posted at

エラーの内容

Dockerfile内でRUN npm installをした際にちらほら見る下記のエラーについて1つ試してほしい解決策があります。

スクリーンショット 2024-09-10 1.21.45.png

ディレクトリ構造

その他必要なファイルもありますが、今回はエラーに関わる必要最低限のディレクトリファイルの構造のみ記述しました。

|---- project/
         |
         |---- docker/
         |        |---- node/
         |                |---- Dockerfile
         |
         |---- src/
         |
         |---- docker-compose.yml
         |---- package.json
         |---- package-lock.json

Docker関連のファイル内容

# node/Dockerfile

FROM node:22.9

RUN apt-get update -y && \
    apt-get clean all

WORKDIR /var/www/html

# ホスト側の`project/`ディレクトリをコンテナ側の`/var/www/html`ディレクトリにコピー
COPY ../../package*.json .

# ここでエラーが出る
RUN npm install

docker-compose.yml
services:
    node:
        build:
            context: ./docker/node
            dockerfile: Dockerfile
    stdin_open: true
    tty: true
    volumes:
        - .:/var/www/html

docker-compose.ymlファイルでcontext: ./docker/nodeなので、DockerfileのCOPY ../../package*.json .は間違っていないように見えますがイメージをビルドしてみると、

Could not read package.json: Error: ENOENT: no such file or directory

package.jsonが見つからないというエラーが出てしまう。

エラーの解決法

  • docker-compose.ymlのcontext: ./docker/node -> context: .に変更
  • docker-compose.ymlのdockerfile: Dockerfile -> ./docker/node/Dockerfileに変更
  • node/DockerfileのCOPY ../../package*.json . -> COPY ./package*.json .に変更

上記を試してみてください。

色々調べ、WORKDIRを作成したりバインドマウントのマウント部分を変えてみたりとしてみましたが、私はこれで解決することができました。

(処理の内容的には変更前・変更後同じことだと思うのですがなぜかエラーが出る(or 解決する)。。。原因分かる方がいればコメントください)

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?