I'm creating web application (angular nest.js) and I'm trying to make multi stage build - I want to reduce size of docker image.
Angular build works good.
Nest.js one builds successfully on my pc using docker compose build
, but fails on github actions with following error: Status: COPY failed: stat usr/src/app/dist: file does not exist, Code: 1
What can i do to fix it?
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"]
previous single stage Dockerfile (builds successfully):
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 workflow file:
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
CodePudding user response:
Try with the following 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"]
CodePudding user response:
Caching multistage builds on CI Servers is not that easy as locally. You have to tell it where to get the layers from. As schema:
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
So for GH Actions you can use the internal caching of GH and do something like:
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
Remark: It's showing you the right direction and no copy&paste solution. Please adjust as you need..
CodePudding user response:
I managed to solved it by moving COPY . .
line before RUN npm run build