【问题标题】:Docker compose fails with 'COPY failed: stat usr/src/app/dist: file does not exist' - only on github actionsDocker compose 因“复制失败:stat usr/src/app/dist:文件不存在”而失败 - 仅在 github 操作上
【发布时间】:2022-02-07 23:50:03
【问题描述】:

我正在创建 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

我能做些什么来解决它?

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
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 的同一目录下有 dist 文件夹吗?该目录是否包含在.gitignore.dockerignore 中?
  • @BMitch distDockerfile 在同一目录中。目录不包含在 .gitignore.dockerignore
  • github repo 是公开的吗?如果是这样,请发布链接。

标签: docker docker-compose nestjs github-actions


【解决方案1】:

尝试使用以下 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"]

【讨论】:

  • 产生与以前相同的错误
【解决方案2】:

在 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

备注:它向您展示了正确的方向,没有复制和粘贴解决方案。请根据需要进行调整..

【讨论】:

    【解决方案3】:

    我设法通过在RUN npm run build 之前移动COPY . . 行来解决它

    【讨论】:

      猜你喜欢
      • 2022-10-15
      • 1970-01-01
      • 2022-08-11
      • 2012-01-26
      • 2022-01-03
      • 2022-01-10
      • 2021-10-01
      • 2021-03-31
      • 1970-01-01
      相关资源
      最近更新 更多