gpt4 book ai didi

node.js - NodeJS 'appendFile' 未在 Docker 容器中创建文件

转载 作者:太空宇宙 更新时间:2023-11-03 23:48:44 25 4
gpt4 key购买 nike

我有一个应用程序,它在运行时在其工作目录中创建一些 CSV 文件。该应用程序在我的 docker 容器之外运行良好,但是当我在其中运行它时,出现错误:

Error: ENOENT: no such file or directory, open 'Data/Output.csv'

该文件是使用 myApp.js 文件中的以下函数调用编写的。我尝试使用绝对/相对路径并在appendFile 的第三个参数中显式设置标志。每次都出现同样的错误。

fs.appendFile('Data/Output.csv', Text, (err) => {
if (err) throw err;
});

为了进行调试,我执行了 touch Output.csv 操作,并在 Dockerfile 中设置了以下行,以查看该文件是否确实存在,下面是该文件的输出。

Step 7/8 : RUN ls -al ~/myApp/src/Data
---> Running in 2dfabf2dd92b
total 8
drwxr-xr-x 2 root root 4096 Feb 7 16:17 .
drwxr-xr-x 5 root root 4096 Feb 7 16:17 ..
-rw-r--r-- 1 root root 0 Feb 7 16:17 .gitkeep
-rwxrwxrwx 1 root root 0 Feb 7 16:17 Output.csv

但是,当我 docker run myApp/myApp 时,我收到错误消息,表示不存在这样的文件或目录。

我的 Dockerfile:

FROM ubuntu

#Install dependencies
RUN apt update -y && apt install -y git ssh nodejs npm gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget

#install go and libraries
RUN wget https://dl.google.com/go/go1.13.7.linux-amd64.tar.gz && tar -C /usr/local -xzf go1.13.7.linux-amd64.tar.gz && <some git clones of private repos>

#clone from local repo
ARG SSH_PRIVATE_KEY
ARG SSH_CONFIG
RUN mkdir ~/.ssh && chmod 0700 ~/.ssh && ssh-keyscan <private repo> ~/.ssh/known_hosts && echo "${SSH_PRIVATE_KEY}" > ~/.ssh/id_ed25519 && echo "${SSH_CONFIG}" > ~/.ssh/config && chmod 0600 ~/.ssh/id_ed25519 && cd ~/ && git clone <private repo> && rm -rf ~/.ssh && touch ~/myApp/src/Data/Output.csv && chmod 777 ~/myApp/src/Data/Output.csv && cd ~/myApp/src && npm install

#CMD ["node", "root/myApp/src/myApp.js"]
ENTRYPOINT /bin/bash #for debugging

最佳答案

您的 WORKDIR 未正确设置相对路径,无法按您期望的方式工作。添加了 WORKDIR 并更改了 CMD 的 Dockerfile 示例。(还要检查您是否已经创建了Data文件夹,如果没有,也可能导致错误)

FROM ubuntu
RUN apt update -y && apt install -y git ssh nodejs npm gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
RUN wget https://dl.google.com/go/go1.13.7.linux-amd64.tar.gz && tar -C /usr/local -xzf go1.13.7.linux-amd64.tar.gz && <some git clones of private repos>
ARG SSH_PRIVATE_KEY
ARG SSH_CONFIG
RUN mkdir ~/.ssh && chmod 0700 ~/.ssh && ssh-keyscan <private repo> ~/.ssh/known_hosts && echo "${SSH_PRIVATE_KEY}" > ~/.ssh/id_ed25519 && echo "${SSH_CONFIG}" > ~/.ssh/config && chmod 0600 ~/.ssh/id_ed25519 && cd ~/ && git clone <private repo> && rm -rf ~/.ssh && touch ~/myApp/src/Data/Output.csv && chmod 777 ~/myApp/src/Data/Output.csv && cd ~/myApp/src && npm install
WORKDIR /root/src
CMD ["node", "app.js"]

关于node.js - NodeJS 'appendFile' 未在 Docker 容器中创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60117644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com