Docker 容器工作,但从 docker-compose 构建时失败

Docker container works, but fails when build from docker-compose

我有一个包含 3 个容器的应用程序: 客户端 - angular 应用程序, 网关 - .NET Core 应用程序, api - .NET Core 应用程序

我在使用托管 angular 应用程序的容器时遇到问题。 这是我的 Docker 文件:

#stage 1
FROM node:alpine as node
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build

#stage 2
FROM nginx:alpine
COPY --from=node /app/dist/caliber_client /usr/share/nginx/html
EXPOSE 80

这里是 docker 撰写文件:

# Please refer https://aka.ms/HTTPSinContainer on how to setup an https developer certificate for your ASP .NET Core service.

version: '3.4'

services:
  calibergateway:
    image: calibergateway
    container_name: caliber-gateway
    build:
      context: .
      dockerfile: caliber_gateway/Dockerfile
    ports:
      - 7000:7000
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
    networks:
      - caliber-local

  caliberapi:
    image: caliberapi
    container_name: caliber-api
    build:
      context: .
      dockerfile: caliber_api/Dockerfile
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
    networks:
      - caliber-local

  caliberclient:
    image: caliber-client-image
    container_name: caliber-client
    build:
      context: .
      dockerfile: caliber_client/Dockerfile
    ports:
      - 7005:7005
    networks:
      - caliber-local

networks:
  caliber-local:
    external: true

当我独立构建和 运行 angular 容器时,我可以连接并 运行 站点,但是如果我尝试使用 docker-compose 构建它,我收到以下错误:

enoent ENOENT: no such file or directory, open '/app/package.json'

我可以看到 npm 找不到 package.json,但我正在将整个站点复制到 docker 文件中的 /app 目录,所以我不确定断开连接的位置。

谢谢。

在 Dockerfile 中,COPY 语句的左侧始终相对于 docker-compose.yml 文件中的 build: { context: } 目录(或 build: 目录)进行解释如果没有嵌套参数或 docker build 目录参数;但无论如何都不会超出此目录树)。

在评论中,你说

The package.json is one level deeper than the docker-compose.yml file. It is at the same level of the Dockerfile in the caliber_client folder.

假设客户端应用程序是独立的,您可以更改构建定义以使用客户端子目录作为构建上下文

build:
  context: caliber_client
  dockerfile: Dockerfile

或者,由于 dockerfile: Dockerfile 是默认值,较短的

build: caliber_client

如果使用父目录作为构建上下文对您很重要(也许您包含了一些未在问题中显示的共享文件),那么您还可以更改 Dockerfile 以引用子目录.

# when the build: { context: } is the parent directory of this one
COPY caliber_client .