5

I have a NodeJS project that uses MongoDB. I would like to run this service in a Docker container, but despite the many examples I have based my learning from, this will not work.

Here is my /heimdall_jwt/Dockerfile:

FROM node:9-alpine
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm install
RUN npm install pm2 -g
COPY . /usr/src/app
EXPOSE 3000
CMD ["pm2-docker", "start", "process.json"]

And here is my /heimdall_jwt/docker-compose.yml

version: '2'
    # Define the services/containers to be run
services:
    myapp: #name of your service
        build: ./ # specify the directory of the Dockerfile
        ports:
          - "3000:3000" #specify ports forwarding
        links:
          - database # link this service to the database service
        volumes:
          - .:/usr/src/app
        depends_on:
          - database    
    database: # name of the service
       image: mongo # specify image to build container from

I tried running via: $docker-compose up --build which results in the mongo being build and starting. Here is the condensed output:

Building myapp
Step 1/8 : FROM node:9-alpine
...
Step 4/8 : RUN npm install
 ---> Using cache
 ---> befb91b1324c
...
Removing intermediate container 945eb0ad40d5
Successfully built b500f7ec9b89
Successfully tagged heimdalljwt_myapp:latest
Creating heimdalljwt_database_1 ...
Creating heimdalljwt_database_1 ... done
Creating heimdalljwt_myapp_1 ...
Creating heimdalljwt_myapp_1 ... done
Attaching to heimdalljwt_database_1, heimdalljwt_myapp_1
database_1  | 2017-11-25T21:15:39.001+0000 I INDEX    [initandlisten]    building index using bulk method; build may temporarily use up to 500 megabytes of RAM
database_1  | 2017-11-25T21:15:39.002+0000 I INDEX    [initandlisten] build index done.  scanned 0 total records. 0 secs
database_1  | 2017-11-25T21:15:39.003+0000 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.4
database_1  | 2017-11-25T21:15:39.005+0000 I NETWORK  [thread1] waiting for connections on port 27017
myapp_1     | 0|heimdall | Error: Cannot find module 'bcryptjs'
myapp_1     | 0|heimdall |     at Function.Module._resolveFilename (module.js:542:15)
myapp_1     | 0|heimdall |     at Function.Module._load (module.js:472:25)
myapp_1     | 0|heimdall |     at Module.require (module.js:585:17)
myapp_1     | 0|heimdall |     at require (internal/module.js:11:18)
myapp_1     | 0|heimdall |     at Object.<anonymous> (/usr/src/app/user/User.js:2:14)
myapp_1     | 0|heimdall |     at Module._compile (module.js:641:30)
myapp_1     | 0|heimdall |     at Object.Module._extensions..js (module.js:652:10)
myapp_1     | 0|heimdall |     at Module.load (module.js:560:32)
myapp_1     | 0|heimdall |     at tryModuleLoad (module.js:503:12)
myapp_1     | 0|heimdall |     at Function.Module._load (module.js:495:3)
myapp_1     | 0|heimdall |     at Module.require (module.js:585:17)
myapp_1     | 0|heimdall |     at require (internal/module.js:11:18)
myapp_1     | 0|heimdall |     at Object.<anonymous> (/usr/src/app/user/UserController.js:12:12)
myapp_1     | 0|heimdall |     at Module._compile (module.js:641:30)
myapp_1     | 0|heimdall |     at Object.Module._extensions..js (module.js:652:10)
myapp_1     | 0|heimdall |     at Module.load (module.js:560:32)
myapp_1     | PM2        | App name:heimdall_app id:0 disconnected

I am uncertain what is going on here, but I am guessing that the dependencies are either not being installed, or not being copied into the working directory. How

UPDATED Modified from original posting as I still am struggling to get this to work.

4
  • 2
    you can find the similar issue in stackoverflow.com/questions/37454548/… Commented Nov 23, 2017 at 14:51
  • Thank you, that helped move it a long a bit, but I still am unable to actually get this thing running. I updated the post with my observation. Commented Nov 24, 2017 at 14:57
  • that's because of missing packages, you have to make changes in heimdall_jwt dockerfile, install all the necessary packages before starting npm install Commented Nov 25, 2017 at 6:58
  • I don't see where my Dockerfile is incorrect, I create directories, copy the package.json to them and then run npm install. does that not install the necessary packages? Commented Nov 25, 2017 at 15:26

1 Answer 1

3
+50

You should copy files after npm install. Right now you are installing dependencies and then copying over everything, so you are effectively canceling that install and you don't have dependencies.

In your Dockerfile you have:

...
RUN npm install
RUN npm install pm2 -g
COPY . /usr/src/app
...

It should be:

...
COPY . /usr/src/app
RUN npm install
RUN npm install pm2 -g
...
Sign up to request clarification or add additional context in comments.

9 Comments

I get this: Step 5/8 : RUN npm install ---> Running in 39ee34e1ce11 npm ERR! Unexpected token } in JSON at position 124674 npm ERR! A complete log of this run can be found in: npm ERR! /root/.npm/_logs/2017-11-28T01_28_32_360Z-debug.log ERROR: Service 'myapp' failed to build: The command '/bin/sh -c npm install' returned a non-zero code: 1 but I cannot find that .log file anywhere to troubleshoot
Troubleshoot your package.json. Remove something that you think might be causing this and try again...
You got that error after changing the order of COPY and RUN npm install?
I got that after changing the COPY and RUN order. I found the log file and am looking into it now
You should make sure that package.json is there. Maybe copy package.json after copying everything. Maybe that's causing the error...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.