1

I have a Node express app that I'm trying to turn into a Docker image and run in a container. The problem I'm having is that it seems in the containerized version of my app, it can't find my local modules av-request and aml-request, but it has no issue finding the installed modules.

app.js

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');

var av = require('./modules/av-request')
var aml = require('./modules/aml-request')

var app = express();
...

dockerfile

FROM node:carbon
# Create container's working directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY ./app/package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY ./app/* /usr/src/app/
# Verify directory contents 
RUN ls /usr/src/app/
EXPOSE 8080
CMD ["npm", "start"]

Here is my output from the build and run process. Notice that in step 7 when we run ls /usr/src/app/ it shows all the files that were copied over but not any directory structure or anything. Based on how ls normally works I would expect it to list directories as well but none are shown. This gives me the impression that all my files from my application exist at the root directory level for some reason. Am I copying my application correctly? The correct file structure is below, but I don't see anything at all like this in the output from my build process.

- app/
    - package.json
    - app.js
    - modules/
    - node_modules/
    - private/
    - public/
    - views/
- dockerfile
- .dockerignore

And this is the output when I build the image.

docker build -t webapp .
Step 1/9 : FROM node:carbon
...
Step 7/9 : RUN ls /usr/src/app/
 ---> Running in 9951fa3289da
accepts
acorn
acorn-globals
ajv
align-text
amdefine
aml-request.js
app.js
array-flatten
...
Successfully built f3d877bc94da
Successfully tagged webapp:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have
'-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

I can verify the image exists when I run docker images

Then I run docker run -d -p 8080:80 webapp which spits out the container ID. Now when I run docker ps -l it shows the container status as Exited (1) so I go check out the logs.

docker logs b3fcc5a6f545

> app@0.0.0 start /usr/src/app
> node app.js

module.js:538
    throw err;
    ^

Error: Cannot find module './modules/av-request'
    at Function.Module._resolveFilename (module.js:536:15)
    at Function.Module._load (module.js:466:25)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/usr/src/app/app.js:5:10)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! app@0.0.0 start: `node app.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the app@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
James
  • 903
  • 7
  • 22
  • just a hunch: in your dockerfile, try and change `# Bundle app source COPY ./app/* /usr/src/app/` to `# Bundle app source COPY ./app /usr/src/app`. Also to check your filesystem inside the docker container you can also just run `docker exec -it webapp /bin/bash` to connect to the container and just use `ls -al` to see what's going on. – Pitt Dec 13 '17 at 02:13
  • This was the solution, found this: https://stackoverflow.com/questions/30215830/dockerfile-copy-keep-subdirectory-structure and realized. My intuition still feels like the * shouldn't ruin the directory structure but it was an easy fix regardless. – James Dec 13 '17 at 02:22

0 Answers0