简体   繁体   English

NodeJS'appendFile'不在Docker容器中创建文件

[英]NodeJS 'appendFile' not creating file in Docker container

I have an application that creates a few CSV files in its working directory while it runs.我有一个应用程序,它在运行时在其工作目录中创建一些 CSV 文件。 The application runs fine outside of my docker container, but when I run it within, I get an error:该应用程序在我的 docker 容器之外运行良好,但是当我在其中运行它时,出现错误:

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

The file is written using the below function call in a myApp.js file.该文件是使用 myApp.js 文件中的以下函数调用编写的。 I have tried using absolute/relative path and setting flags explicitly in the third parameter of appendFile.我曾尝试使用绝对/相对路径并在 appendFile 的第三个参数中显式设置标志。 Same error every time.每次都一样的错误。

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

For debugging, I did a touch Output.csv and set the following line in my Dockerfile to see if the file is actually there, and below is the output of that.为了调试,我做了一个触摸 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

Yet when I docker run myApp/myApp I get the error that there is no such file or directory.然而,当我docker run myApp/myApp 时,我得到没有这样的文件或目录的错误。

My Dockerfile:我的 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

Your WORKDIR is not set properly for relative path to work as you expect it to work.您的WORKDIR未正确设置相对路径,以使其正常工作。 Example of your Dockerfile with added WORKDIR and changed CMD .添加了WORKDIR并更改了CMD Dockerfile 示例。 ( Also check whether or not you have folder Data already created, if not it can also result in error ) 还要检查您是否已经创建了文件夹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"]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM