Docker - EACCES:权限被拒绝,mkdir '/usr/src/app/dist'

问题描述 投票:0回答:1

我有一个 dockerized NestJS 应用程序。

Dockerfile

###################
# BUILD FOR LOCAL DEVELOPMENT
###################

FROM node:18-alpine As development

# Create app directory
WORKDIR /usr/src/app

# Set to dev environment
ENV 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 source
COPY --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 build

WORKDIR /usr/src/app

COPY --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 image
COPY --chown=node:node --from=development /usr/src/app/node_modules ./node_modules

COPY --chown=node:node . .

# Run the build command which creates the production bundle
RUN npm run build

# Set NODE_ENV environment variable
ENV 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 possible
RUN npm ci --only=production && npm cache clean --force

USER node

###################
# PRODUCTION
###################

FROM node:18-alpine As production

# Copy the bundled code from the build stage to the production image
COPY --chown=node:node --from=build /usr/src/app/node_modules ./node_modules
COPY --chown=node:node --from=build /usr/src/app/dist ./dist

# Start the server using the production build
CMD [ "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: {
    "dev": "nest start --watch -b swc",
}

运行

docker-compose up -d
后,我运行
docker logs -f backend
,然后显示错误。

docker logs -f backend

> [email protected] 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

我猜这与用户访问有关。 尝试在 Dockerfile 中添加

RUN chown -R node.node /app
,但不起作用。 如何解决这个问题?

javascript node.js typescript docker nestjs
1个回答
0
投票

通过添加解决了问题

RUN chown -R node.node /usr/src/app

Dockerfile

###################
# BUILD FOR LOCAL DEVELOPMENT
###################

FROM node:18-alpine As development

# Create app directory
WORKDIR /usr/src/app

# Set to dev environment
ENV 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

RUN chown -R node.node /usr/src/app <-----------------HERE
...
© www.soinside.com 2019 - 2024. All rights reserved.