簡體   English   中英

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

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

我有一個應用程序,它在運行時在其工作目錄中創建一些 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;
});

為了調試,我做了一個觸摸 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"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM