node.jsdockerdockerfileappendfile

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. The application runs fine outside of my docker container, but when I run it within, I get an error:

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. I have tried using absolute/relative path and setting flags explicitly in the third parameter of 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.

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.

My 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

Solution

  • Your WORKDIR is not set properly for relative path to work as you expect it to work. Example of your Dockerfile with added WORKDIR and changed CMD. (Also check whether or not you have folder Data already created, if not it can also result in error)

    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"]