问题
I am trying to wire up my Angular 7 client app into docker compose locally.
I get the following error when I docker-compose up:
client-app | npm ERR! errno -2
client-app | npm ERR! syscall open
client-app | npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
Dockerfile:
FROM node:9.6.1
RUN mkdir -p /app
WORKDIR /app
EXPOSE 4200
ENV PATH /app/node_modules/.bin:$PATH
COPY . /app
RUN npm install --silent
RUN npm rebuild node-sass
CMD ["npm", "run", "docker-start"]
The compose part for the client is:
client-app:
image: ${DOCKER_REGISTRY-}client
container_name: client-app
ports:
- "4200:81"
build:
context: .
dockerfile: ClientApp/Dockerfile
package.json is in the ClientApp folder along-side Dockerfile, I would assume COPY . /app should copy the package.json to the container. I don't have any excludes in dockerignore. I am using Docker for Windows with Unix containers. I tried npm init before (but that will create an empty package.json anyway) and looked through the SO posts but most of the dockerfile definitions look exactly the same. I also tried: COPY package*.json ./ additionally and building the image with --no-cache.
回答1:
Your Dockerfile is fine, and COPY package*.json ./ is not necessary - its being copied with your entire app.
The problem is in your docker-compose file.
you defined:
build:
context: .
dockerfile: ClientApp/Dockerfile
that means, your Dockerfile will accept the docker-compose context, which is 1 directory above:
├── docker-compose.yml -- this is the actual context
└── ClientApp
├── Dockerfile -- this is the expected context
so when the CMD is running, it is 1 directory above the package.json, therefore it cannot run the command and the container exits.
to fix it, give your docker-compose file the correct context:
build:
context: ClientApp
dockerfile: Dockerfile
and it should work.
回答2:
This command COPY package*.json ./ will try copy package.json from root dir. However looks like you need to copy package.json from ClientApp dir
Two way to fix this:
1) Change compose file context: ./ClientApp/
2) Change docker file COPY ./ClientApp/package.json /app/package.json
I think better to change docker file because in feature you may want to copy some file from root dir
来源:https://stackoverflow.com/questions/55589014/npm-cant-find-package-json-when-running-docker-container-with-compose