使用compose运行docker容器时,npm找不到package.json [英] npm can't find package.json when running docker container with compose
问题描述
我正在尝试将Angular 7客户端应用程序连接到本地的docker compose中。
I am trying to wire up my Angular 7 client app into docker compose locally.
当我进行docker组合时,出现以下错误:
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:
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"]
客户的组成部分是:
client-app:
image: ${DOCKER_REGISTRY-}client
container_name: client-app
ports:
- "4200:81"
build:
context: .
dockerfile: ClientApp/Dockerfile
package.json与Dockerfile一起位于ClientApp文件夹中,我假设为COPY。 / app应该将package.json复制到容器中。我在dockerignore中没有任何排除项。我正在将Docker用于Windows和Unix容器。我之前尝试过npm init(但是无论如何都会创建一个空的package.json),并仔细查看SO帖子,但大多数dockerfile定义看起来完全一样。我还尝试了:COPY package * .json ./并另外使用--no-cache构建映像。
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.
推荐答案
您的 Dockerfile 很好,不需要 COPY package * .json ./
-它将与您的整个应用程序一起复制。
Your Dockerfile is fine, and COPY package*.json ./
is not necessary - its being copied with your entire app.
问题出在您的 docker-compose 文件中。
您定义:
build:
context: .
dockerfile: ClientApp/Dockerfile
这意味着您的 Dockerfile 将接受docker-compose上下文,该上下文位于上面的1个目录中:
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
因此,当CMD运行时,它位于 package.json 上方的1个目录中,因此它无法运行该命令并且容器退出。
so when the CMD is running, it is 1 directory above the package.json, therefore it cannot run the command and the container exits.
要对其进行修复,请为您的 docker-compose 文件提供正确的上下文:
to fix it, give your docker-compose file the correct context:
build:
context: ClientApp
dockerfile: Dockerfile
,它应该可以工作。
这篇关于使用compose运行docker容器时,npm找不到package.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!