在我的docker/my-project-node/檔案夾中的 Dockerfile 中,我有RUN npm i --legacy-peer-deps --only=production --no-optional.
我想在本地運行 docker build 看看是否一切正常。
當我運行時,docker build -t my-project-image .我得到這個 npm 錯誤:
#9 0.913 npm ERR! code ENOENT
#9 0.913 npm ERR! syscall open
#9 0.914 npm ERR! path /app/package.json
#9 0.914 npm ERR! errno -2
#9 0.916 npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
#9 0.917 npm ERR! enoent This is related to npm not being able to find a file.
#9 0.917 npm ERR! enoent
Dockerfile:
FROM node:16-alpine
RUN apk update && apk add --no-cache ca-certificates bash
WORKDIR '/app'
COPY . .
RUN npm i --legacy-peer-deps --only=production --no-optional
CMD ["npm","run", "start:prod"]
我的 docker 檔案有什么問題?
uj5u.com熱心網友回復:
您將錯誤傳遞build context給 docker。
這build context是 Docker 開始作業的目錄,所有COPY命令都從該路徑中查找檔案。
有兩種方法可以正確指定:
從專案的根目錄運行命令并明確指定 Dockerfile 路徑(如果它不在根目錄上)。
docker build -t my-web-app -f docker/my-project-node/Dockerfile在 docker 命令中,指定所需的根目錄。
docker build -t my-web-app ../..(因為你的根目錄是 2 個檔案夾)
最后,請看docker-compose。所有這些討厭的 docker 命令都可以在 docker-compose 檔案中精美地表示,如下所示:
# docker-compose.yaml placed at root of the project
version: '3'
services:
web:
container_name: "example1"
build:
context: .
dockerfile: docker/my-project-node/Dockerfile
ports:
- "8000:8000"
創建檔案后,只需運行docker-compose up -d以創建和運行容器,并docker-compose down關閉和洗掉容器:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/492397.html
