简体   繁体   English

docker 容器节点应用程序中的访问被拒绝问题

[英]Access denied issue in docker container node application

I have a MERN stack application.我有一个 MERN 堆栈应用程序。 I deployed the app on render cloud.我在渲染云上部署了应用程序。 I noticed that my image upload functionality is not working but works on local machine.我注意到我的图片上传功能不起作用,但可以在本地机器上使用。 I 'dockerized' the node application.我“码头化”了节点应用程序。 The error message I got when I tried to upload image is this:我尝试上传图片时收到的错误消息是这样的:

Error: EACCES: permission denied, open 'images/1666890887146download.png'

First, I am using multer npm package to handle image upload.首先,我使用multer npm 包来处理图片上传。 The image is saved inside a folder called image in my api project folder.图像保存在我的api项目文件夹中名为image的文件夹中。 I think because of the user access in dockerfile, that is why the access is denied.我认为由于 dockerfile 中的用户访问,这就是访问被拒绝的原因。 Here is my dockerfile:这是我的码头文件:

FROM  node:lts-alpine

 WORKDIR /app

 COPY package*.json ./

  COPY client/package*.json client/
  RUN npm run install-client --only=production


  COPY api/package*.json api/
  RUN npm run install-api --only=production



   COPY client/ client/
   RUN npm run client-build --prefix client


 COPY api/ api/



 USER node

 CMD [ "npm", "start", "--prefix", "api" ]



 EXPOSE 5000

Here is my multer image upload function:这是我的multer图上传功能:

 const ALLOWED_FORMATS = ['image/jpeg', 'image/png', 'image/jpg'];

    //login to upload images using libery called multer
    const storage = multer.diskStorage({//choosing destination of the file
    destination:(req, file, cb) =>{
    if(ALLOWED_FORMATS.includes(file.mimetype)){
         cb(null, "./images")
    }else{
        cb(new Error('Not supported file type!'), false)
    }
   
}, filename:(req, file,cb)=> {//choosing file name
   cb(null, req.body.name) 
  }
 });

 const upload = multer({storage:storage})

I am sensing that the node User I assigned in dockerfile does not have access to read and write file in the image folder.我感觉到我在dockerfile中分配的node用户无权读取和写入image文件夹中的文件。 My logics rely on that because after uploading the image from the image folder to cloudinary, I also deleted it immediately.我的逻辑依赖于此,因为在将图像从image文件夹上传到 cloudinary 后,我也立即将其删除。 Is there a way I can assign node User the permission to read and write files in image folder?有没有办法可以为node用户分配在图像文件夹中读取和写入文件的权限? I am not too familiar with docker.我对docker不太熟悉。

Yes, you can add the node user to the sudo group...是的,您可以将节点用户添加到 sudo 组...

sudo usermod -aG sudo $USER

or you can give permissions to the user using something like或者您可以使用类似的方式向用户授予权限

chown -R $USER:$USER images/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Docker 容器中访问 Angular 应用程序 - Access Angular application in a Docker container 能够使用 docker-compose.yml 在容器和 redis 容器中启动 node js 应用程序,但无法访问应用程序 - Able to bring up a node js application in a container and a redis container using docker-compose.yml but unable to access application Docker 容器内的 node-oracledb 问题 - Issue with node-oracledb inside a Docker container 如何在Docker容器中访问Node API - How to Access Node API within Docker Container 在Docker容器中配置应用程序以访问其他容器 - Configuring application in docker container to access other containers 使用 web 应用程序访问另一个容器中的 docker 容器中托管的 nodejs - access nodejs hosted in docker container in another container using web application 在Docker容器中从外部访问Node.JS应用 - External access to Node.JS app, within Docker container 从 docker 容器内的节点服务器访问 ubuntu localhost - Access ubuntu localhost from node server inside docker container 访问docker容器外部的节点模块的数据量 - access data volume of node modules outside docker container 应用服务中的 Docker 容器无法使用节点 SDK 访问 KeyVault - Docker Container in App Service not able to access KeyVault with Node SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM