1

我有一个应用程序,它在运行时会在其工作目录中创建一些 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
4

1 回答 1

1

WORKDIR的相对路径设置不正确,无法按预期工作。WORKDIR带有添加和更改的 Dockerfile 示例CMD。(还要检查您是否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"]
于 2020-02-07T18:25:09.697 回答