简体   繁体   中英

Docker cannot find package.json

I have the following project I'm trying to build an image from and docker is unable to find package.json:

root
  client
  server
    build/
    package.json
  Dockerfile

Dockerfile:

FROM node:latest
WORKDIR /server
COPY ./package.json .
RUN npm install                  
COPY . .
EXPOSE 5000
CMD ["npm". "start"]

I have tried every directory pointer for WORKDIR and COPY that I can come up with to no success.

The left-hand side of COPY is always relative to the build context directory , usually the directory that contains the Dockerfile. You can never copy anything outside of this directory tree. If the right-hand side of COPY is a relative path, it is relative to the current WORKDIR .

In the setup you show, you are trying to copy root/package.json from the host into /server in the container.

You probably need to change the left-hand side to be relative to the root directory. The container filesystem is isolated from the host so it doesn't especially matter what the WORKDIR path actually is, just so long as it's the current directory when you run npm install or npm ci .

WORKDIR /server                 # or /app or anything else you'd like
COPY ./server/package*.json ./  # make sure to get package-lock.json too
RUN npm ci

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM