Different output while testing a docker image

  docker, node.js

I found out that somehow, when I’m testing a docker image something aren’t working properly but when I test it on localhost everything is fine. Here’s what really happening:

CronTasksService.js -> Object {run()}

    run: (task, callback) => {
        var file = requireAppRoot('cron_tasks/' + task.file);
        console.log('cron file --> ', 'cron_tasks/' + task.file)
        console.log('File:', file)
        /*
        if (file != undefined && typeof file.run == "function") {
            return file.run(task, callback);
        }*/
        callback('Invalid File');
    }

The line below, retrieves the file path according to the following format "./cron_tasks/{task.file}", the path is correct because two files are actually working in the docker image and their function run() is being retrieved inside an object. As I previously said when I’m testing in localhost everything goes well with every file having his own run() retrieved inside an object.

var file = requireAppRoot('cron_tasks/' + task.file);

And now, heres how it goes in localhost

// localhost - Output
cron file -->  cron_tasks/ReprocessLevelsTask
File: { run: [Function: run] }
cron task error -->  Invalid File
cron file -->  cron_tasks/AlarmsTask
File: { run: [Function: run] }
cron task error -->  Invalid File
cron file -->  cron_tasks/EmailQueueTask
File: { run: [Function: run] }
cron task error -->  Invalid File
cron file -->  cron_tasks/DailyReportsTasks
File: { run: [Function: run] }
cron task error -->  Invalid File

Fine as planned, here’s what happens when i’m testing the docker image
OBS: Ignore those logs with "Invalid File", they’re there on purpose a default callback

// docker image/logs -f - Output
cron file -->  cron_tasks/ReprocessLevelsTask
File: { run: [Function: run] }
cron task error -->  Invalid File
cron file -->  cron_tasks/AlarmsTask
File: undefined
cron task error -->  Invalid File
cron file -->  cron_tasks/EmailQueueTask
File: undefined
cron task error -->  Invalid File
cron file -->  cron_tasks/DailyReportsTasks
File: undefined
cron task error -->  Invalid File

As you can "File: undefined", which doesn’t make sense since the file is there and has a run() function.
To build and run the image I used the following:

docker build -t [image:tag] .
docker run --env TCP_PORT=34000 -p 127.0.0.1:34000:34000/tcp [image:tag]

// Dockerfile

FROM node:14.16.1-stretch
WORKDIR /usr/src/app
COPY package*.json ./
COPY .npmrc ./
RUN npm install nodemon -g
RUN npm install pm2 -g
RUN npm install
COPY ./ ./
RUN ls -la ./
EXPOSE 34000
EXPOSE 25
ENV NODE_ENV=staging
CMD [ "pm2-runtime", "server.js" ]

I’ve tested the image in many different ways, different syntax (in js files), a different nodejs image, different ports, tried to see if there were any differences in the run function in the ReprocessLevelsTask.js file (the structure is the same), etc

Source: Docker Questions

LEAVE A COMMENT