I have a dockerized NestJS application.
Dockerfile
#################### BUILD FOR LOCAL DEVELOPMENT###################FROM node:18-alpine As development# Create app directoryWORKDIR /usr/src/app# Set to dev environmentENV NODE_ENV dev# Copy application dependency manifests to the container image.# A wildcard is used to ensure copying both package.json AND package-lock.json (when available).# Copying this first prevents re-running npm install on every code change.COPY --chown=node:node package*.json ./# Install app dependencies using the `npm ci` command instead of `npm install`RUN npm ci# Bundle app sourceCOPY --chown=node:node . .# Use the node user from the image (instead of the root user)USER node#################### BUILD FOR PRODUCTION###################FROM node:18-alpine As buildWORKDIR /usr/src/appCOPY --chown=node:node package*.json ./# In order to run `npm run build` we need access to the Nest CLI which is a dev dependency. In the previous development stage we ran `npm ci` which installed all dependencies, so we can copy over the node_modules directory from the development imageCOPY --chown=node:node --from=development /usr/src/app/node_modules ./node_modulesCOPY --chown=node:node . .# Run the build command which creates the production bundleRUN npm run build# Set NODE_ENV environment variableENV NODE_ENV production# Running `npm ci` removes the existing node_modules directory and passing in --only=production ensures that only the production dependencies are installed. This ensures that the node_modules directory is as optimized as possibleRUN npm ci --only=production && npm cache clean --forceUSER node#################### PRODUCTION###################FROM node:18-alpine As production# Copy the bundled code from the build stage to the production imageCOPY --chown=node:node --from=build /usr/src/app/node_modules ./node_modulesCOPY --chown=node:node --from=build /usr/src/app/dist ./dist# Start the server using the production buildCMD [ "node", "dist/main.js" ]
docker-compose.yml
version: '3.5'services: backend: container_name: backend build: context: ./backend target: development dockerfile: Dockerfile ports: - '3001:3001' volumes: - ./backend:/app - /app/node_modules command: npm run dev
package.json
scripts: {"start:dev": "nest start --watch"}
After running docker-compose up -d
, I run docker logs -f backend
and then an error shows.
docker logs -f backend> backend@0.0.1 dev> nest start --watch -b swc> SWC Running...EACCES: permission denied, mkdir '/usr/src/app/dist'EACCES: permission denied, mkdir '/usr/src/app/dist'EACCES: permission denied, mkdir '/usr/src/app/dist'...Failed to compile 28 files with swc.Watching for file changes.Error: Cannot find module '/usr/src/app/dist/main' at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1134:15) at Function.Module._load (node:internal/modules/cjs/loader:975:27) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12) at node:internal/main/run_main_module:28:49
I guess it's related to user access. How to fix this issue?