5

I'm working on a NestJS project and copying my assets works fine on my Mac. However, once I dockerize it it won't work.

nest-cli.json

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "assets": [
      "**/*.hbs",
      "**/*.css",
      "**/*.jpg",
      "**/*.png",
      "**/*.jpeg"
    ],
    "watchAssets": true
  }
}

Dockerfile:

FROM mhart/alpine-node:latest

RUN npm install pm2 -g

COPY backend /var/www/backend
COPY process.json /var/www
WORKDIR /var/www

#RUN npm i -g @nestjs/cli (tried with and w/out -> no difference)
RUN cd ./backend && npm i --legacy-peer-deps && npm run build

# Expose ports
EXPOSE 80

CMD ["pm2-runtime", "./backend/main"]

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  }
}

tsconfig.build.json

npm{
  "extends": "./tsconfig.json",
  "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}

I'm not getting any build errors and I've reproduced these steps manually on my mac and everything's being copied. When I go into the docker image and also run the buld step manually it works with no errors but again, no assets are being copied.

I have my assets in different module & services folders and will want to keep it this way - so I'm not looking for a simply post build copy dir solution :)

I've tried different linux dists. and Node version - I'm not sure what else to check

Matt Brandt
  • 581
  • 1
  • 4
  • 20
  • 4
    Were you able to find the solution. I happen to face the same issue. In my case, the assets are copied to dist folder sometimes but not every time. If I get a docker container which does not have assets copied and execute an interactive terminal inside it and run npm run build, I see that the assets are copied as usual. In my mac, it never happens but I could reproduce this on Ubuntu 16.04.7 LTS (Xenial Xerus) – Eranga Heshan Feb 22 '21 at 09:31
  • 2
    @ErangaHeshan Unfortunately, no. It "might" have something to do with the typescript version installed but I was never able to get it working regardless of what image and/or typescript version I'd use in my container. So I'm using npm:copyfiles as a buildstep now, as ugly as i is.... Example: copyfiles --up 1 src/**/*.hbs dist – Matt Brandt Feb 23 '21 at 18:11
  • I have the same issue. Random copying in dist on Ubuntu but on my mac everything is ok. – mr_squall Apr 23 '22 at 06:12

1 Answers1

1

I had a similar problem trying to dockerize my project on a Linux host (yarn build on local worked just fine, but assets were not copied in the docker image).

Original Setup

Dockerfile:

FROM node:16-bullseye AS builder
WORKDIR /usr/app
COPY package.json ./
COPY yarn.lock ./
COPY src ./
COPY nest-cli.json ./
COPY tsconfig.build.json ./
COPY tsconfig.json ./
RUN yarn install
RUN yarn build
...

nest-cli.json:

{
  ...,
  "compilerOptions": {
    "assets: [ "modules/mails/templates/**/*.hbs" ],
    ...
  }
}

Fix

The paths specified within 'compilerOptions.assets' are always relative to the 'src' folder within the project. Inspecting my docker image I realized that the content of the 'src' folder was being copied in the root of the build folder inside docker.

When the content of 'src' is moved to the root, NestJS is able to build the project successfully, but since assets are looked for within the 'src' folder, no files are copied. Changing this in my Dockerfile fixed the problem:

COPY src ./src # Instead of 'COPY src ./'
  • Unfortunately, I have no way of verifying this on my setup as I've long moved on but your explanation and fix makes sense to me - will accept this answer - thx! – Matt Brandt Mar 31 '23 at 13:31