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.
CodePudding user response:
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