Node.js NodeJS';附录文件';未在Docker容器中创建文件

Node.js NodeJS';附录文件';未在Docker容器中创建文件,node.js,docker,dockerfile,appendfile,Node.js,Docker,Dockerfile,Appendfile,我有一个应用程序,在运行时在其工作目录中创建一些CSV文件。应用程序在docker容器外部运行正常,但在内部运行时,出现错误: Error: ENOENT: no such file or directory, open 'Data/Output.csv' 该文件是在myApp.js文件中使用下面的函数调用编写的。我尝试过使用绝对/相对路径,并在appendFile的第三个参数中显式设置标志。每次都是同样的错误 fs.appendFile('Data/Output.csv', Text, (e

我有一个应用程序,在运行时在其工作目录中创建一些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;
});
为了调试,我做了一个触摸输出.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运行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
来自ubuntu的

#安装依赖项
运行apt更新-y&&apt安装-y git ssh nodejs npm gconf服务libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0-0 libpangocairo-1.0-0 libstdc++-6 libx11-6 libx11-xb1-xcb1-libxcb1 libxcb1 libxcombxcomb1 libxcomb1 libxcompos1 libx6libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca证书字体解放libappindicator1 libnss3 lsb发行版xdg utils wget
#安装go和库
运行wgethttps://dl.google.com/go/go1.13.7.linux-amd64.tar.gz &&tar-C/usr/local-xzf-go1.13.7.linux-amd64.tar.gz&&
#从本地回购中克隆
ARG SSH_私钥
ARG SSH_配置
运行mkdir~/.ssh&&chmod 0700~/.ssh&&ssh keyscan~/.ssh/known_hosts&&echo“${ssh_PRIVATE_KEY}”>~/.ssh&&echo“${ssh_CONFIG}”>~/.ssh/CONFIG&&chmod 0600~/.ssh/id_ed25519&&cd~/&git clone&&rm-rf/.ssh&&touch&&myApp/src/Data/Output.csv&&chmod 777~/myApp
#CMD[“node”,“root/myApp/src/myApp.js”]
用于调试的入口点/bin/bash

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

来自ubuntu的

运行apt更新-y&&apt安装-y git ssh nodejs npm gconf服务libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0-0 libpangocairo-1.0-0 libstdc++-6 libx11-6 libx11-xb1-xcb1-libxcb1 libxcb1 libxcombxcomb1 libxcomb1 libxcompos1 libx6libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca证书字体解放libappindicator1 libnss3 lsb发行版xdg utils wget
运行wgethttps://dl.google.com/go/go1.13.7.linux-amd64.tar.gz &&tar-C/usr/local-xzf-go1.13.7.linux-amd64.tar.gz&&
ARG SSH_私钥
ARG SSH_配置
运行mkdir~/.ssh&&chmod 0700~/.ssh&&ssh keyscan~/.ssh/known_hosts&&echo“${ssh_PRIVATE_KEY}”>~/.ssh&&echo“${ssh_CONFIG}”>~/.ssh/CONFIG&&chmod 0600~/.ssh/id_ed25519&&cd~/&git clone&&rm-rf/.ssh&&touch&&myApp/src/Data/Output.csv&&chmod 777~/myApp
WORKDIR/root/src
CMD[“node”,“app.js”]

Dockerfile声明了制作容器图像的步骤。似乎您的代码正在容器中创建文件(而不是图像)。所以当然,在构建步骤中执行
ls-al
不会显示任何您想要的内容。运行docker容器后,只需进入docker容器:1)
docker ps
find container id,2)
docker exec-it container sh
进入容器。@num8er我确实这样做了,文件就在那里。也验证了绝对路径和相对路径,但即使从容器内部以交互方式运行文件,也找不到该文件。请将Dockerfile放入您的问题。谢谢!这是问题的一部分。我一直遇到的另一件事是试图在同一个RUN命令中创建一个目录和cd。当我把他们分开的时候,我很高兴我能帮上忙
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"]