Docker 撰写失败 'COPY failed: stat usr/src/app/dist: file does not exist' - 仅在 github 操作上

Docker compose fails with 'COPY failed: stat usr/src/app/dist: file does not exist' - only on github actions

我正在创建 Web 应用程序 (angular + nest.js),我正在尝试进行多阶段构建 - 我想减小 docker 图像的大小。

Angular 构建效果很好。

Nest.js 一个在我的电脑上使用 docker compose build 成功构建,但在 github 操作上失败并出现以下错误:Status: COPY failed: stat usr/src/app/dist: file does not exist, Code: 1

我该如何解决?

Docker 文件:

FROM node:16.13.1-alpine AS build
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=development
RUN npm run build
COPY . .

FROM node:16.13.1-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
COPY . .
COPY --from=build /usr/src/app/dist ./dist
EXPOSE 7000
CMD ["node", "dist/main"]

之前的单阶段 Dockerfile(构建成功):

FROM node:16.13.1-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
RUN npm run build
COPY . .
EXPOSE 7000
CMD [ "npm", "start" ]

docker-compose.yml:

version: '3.8'

services:
  backend:
    container_name: nestjs
    image: ghcr.io/<gh-nickname/repo-name>/backend  
    build:
      context: backend
      dockerfile: Dockerfile
    ports:
    - 7000:7000
  frontend:
    container_name: angular
    image: ghcr.io/<gh-nickname/repo-name>/frontend
    build:
      context: frontend
      dockerfile: Dockerfile
    ports:
    - 8000:80

github 工作流程文件:

name: Build and publish to github packages

on:
  push:
    branches:
      - master

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build-and-push-image:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
    - name: Checkout 
      uses: actions/checkout@v2

    - name: Log in to the Container registry
      uses: docker/login-action@v1
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: Extract metadata (tags, labels) for Docker
      id: meta
      uses: docker/metadata-action@v3
      with:
        images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

    - name: Build image
      run: docker compose build

    - name: Publish image
      run: docker compose push

尝试使用以下 Dockerfile:

FROM node:16.13.1-alpine AS build
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=development
RUN npm run build
COPY . .

FROM node:16.13.1-alpine
COPY --from=build /usr/src/app/dist ./dist
EXPOSE 7000
CMD ["node", "dist/main"]

在 CI 服务器上缓存多阶段构建并不像在本地那么容易。您必须告诉它从哪里获取图层。作为模式:

docker build --target build-image --cache-from=you/repo/backend:build-stage --tag you/repo/backend:build-stage

docker build --target prod-image --cache-from=you/repo/backend:build-stage --cache-from=you/repo/backend:prod-stage --tag you/repo/backend:prod-stage

docker push 

因此对于 GH 操作,您可以使用 GH 的内部缓存并执行以下操作:

steps:
- name: Build base image
        id: docker_build_base
        uses: docker/build-push-action@v2
        with:
          context: .
          file: Dockerfile
          push: false
          cache-from: |
            type=gha,scope=base
          cache-to: |
            type=gha,scope=base,mode=max
          target: base
          tags: you/repo/backend:build-stage

      - name: Build prod image
        id: docker_build_prod
        uses: docker/build-push-action@v2
        with:
          context: .
          file: Dockerfile
          push: true
          cache-from: |
            type=gha,scope=prod
            type=gha,scope=base
          cache-to: |
            type=gha,scope=prod,mode=max
          target: prod
          tags: you/repo/backend:prod-stage

备注:它告诉你正确的方向,没有复制粘贴的解决方案。请根据需要调整..

我设法通过在 RUN npm run build

之前移动 COPY . . 行来解决它