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?

docker composeのbuild時COPY package*.json ./のエラー

Posted at

はじめに

独学でプログラミングを勉強しながら学んだことについて記録したと考えています。もし内容に誤りや改善点があれば、ご指摘いただけると幸いです。:pray:

COPY package*.json ./のエラー

docker-compose.dev.ymlをビルドしてみようと思いましたが、以下のようなエラーになりました。

=> [backend 3/5] COPY package*.json ./
=> ERROR [backend 4/5] RUN npm install

[backend 4/5] RUN npm install:
1.269 npm error code ENOENT
1.269 npm error syscall open
1.269 npm error path /app/package.json
1.269 npm error errno -2
1.269 npm error enoent Could not read package.json: Error: ENOENT: no such file or directory, open '/app/package.json'
1.269 npm error enoent This is related to npm not being able to find a file.
1.269 npm error enoent
1.271 npm error A complete log of this run can be found in: /root/.npm/_logs/2025-02-28T00_55_00_093Z-debug-0.log


failed to solve: process "/bin/sh -c npm install" did not complete successfully: exit code: 254

RUN npm installを実行しようとしたが、package.json見つかれなくて発生したエラーでした。

修正前docker-compose.dev.yml

services:
  backend:
    build:
      context: .
      dockerfile: backend/Dockerfile.dev
    container_name: oauth_backend_dev
    ports:

修正前はこのように作成していました。

  • これが問題になる理由は以下になります。
  • context: . だと Oauth ディレクトリ全体 が Docker ビルドの対象になるため、Dockerfile 内の COPY package*.json ./ はcontext のルート (Oauth/) からコピーしようとするからです。
  • つまり、COPY package*.json ./ は Oauth/package.json を探してしまい、backend/package.json を見つけないことになります。

修正後docker-compose.dev.yml

services:
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile.dev
    container_name: oauth_backend_dev
    ports:
修正のポイント
  • context: ./backend にすることで、backend/ ディレクトリの中身だけが Docker のビルドコンテキストになります。
  • これにより、COPY package*.json ./ は backend/package.json を正しくコピーできるようになります。
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?