• Uncategorized

About node.js : Docker-isnt-executing-RUN-commands-correctly-and-doesnt-read-dockerignore-file

Question Detail

Here the RUN command is not executed in the way I expected:

/tmp $ express -c --view=ejs test-app

  warning: the default view engine will not be jade in future releases
  warning: use `--view=jade' or `--help' for additional options


   create : test-app/
   create : test-app/public/
   create : test-app/public/javascripts/
   create : test-app/public/images/
   create : test-app/public/stylesheets/
   create : test-app/public/stylesheets/style.css
   create : test-app/routes/
   create : test-app/routes/index.js
   create : test-app/routes/users.js
   create : test-app/views/
   create : test-app/views/error.jade
   create : test-app/views/index.jade
   create : test-app/views/layout.jade
   create : test-app/app.js
   create : test-app/package.json
   create : test-app/bin/
   create : test-app/bin/www

   change directory:
     $ cd test-app

   install dependencies:
     $ npm install

   run the app:
     $ DEBUG=test-app:* npm start

/tmp $ cd test-app/
/tmp/test-app $ ll | grep node_modules
/tmp/test-app $ vim Dockerfile
/tmp/test-app $ cat Dockerfile
FROM node:12.15-alpine AS development
WORKDIR /usr/src/app
ENV NODE_ENV development
COPY . .
RUN npm install
/tmp/test-app $ docker build -t test-app:development --target development .
Sending build context to Docker daemon  17.41kB
Step 1/5 : FROM node:12.15-alpine AS development
 ---> afd897e3184b
Step 2/5 : WORKDIR /usr/src/app
 ---> Using cache
 ---> b635d27109d9
Step 3/5 : ENV NODE_ENV development
 ---> Using cache
 ---> d8358f2dc7b3
Step 4/5 : COPY . .
 ---> a02fff431f86
Step 5/5 : RUN npm install
 ---> Running in 8cb422535183
npm WARN deprecated [email protected]: Jade has been renamed to pug, please install the latest version of pug instead of jade
npm WARN deprecated [email protected]: Please update to at least constantinople 3.1.1
npm WARN deprecated [email protected]: Deprecated, use jstransformer
npm notice created a lockfile as package-lock.json. You should commit this file.
added 99 packages from 139 contributors and audited 194 packages in 23.436s
found 4 vulnerabilities (3 low, 1 critical)
  run `npm audit fix` to fix them, or `npm audit` for details
Removing intermediate container 8cb422535183
 ---> 727228b0222f
Successfully built 727228b0222f
Successfully tagged test-app:development
/tmp/test-app $ docker run --rm -it --init -v "${PWD}:/usr/src/app" -p 3000:3000 test-app:development ls -l | grep node_modules
/tmp/test-app $ docker run --rm -it --init -v "${PWD}:/usr/src/app" -p 3000:3000 test-app:development pwd
/usr/src/app
  • No node_modules folder in source yet, so copy won’t copy it to container
  • It looks like npm install ran from dockerfile
  • But node_modules folder is not in the container

Continuing from above, here file in dockerignore are not ignored:

/tmp/test-app $ vim .dockerignore
/tmp/test-app $ cat .dockerignore
node_modules
/tmp/test-app $ npm install
npm WARN deprecated [email protected]: Jade has been renamed to pug, please install the latest version of pug instead of jade
npm WARN deprecated [email protected]: Deprecated, use jstransformer
npm WARN deprecated [email protected]: Please update to at least constantinople 3.1.1
npm notice created a lockfile as package-lock.json. You should commit this file.
added 99 packages from 139 contributors and audited 194 packages in 6.48s
found 4 vulnerabilities (3 low, 1 critical)
  run `npm audit fix` to fix them, or `npm audit` for details
/tmp/test-app $ ll | grep node_modules
drwxr-xr-x  94 user  wheel   3196 12 Feb 01:42 node_modules
/tmp/test-app $ docker build -t test-app:development --target development .
Sending build context to Docker daemon  45.57kB
Step 1/5 : FROM node:12.15-alpine AS development
 ---> afd897e3184b
Step 2/5 : WORKDIR /usr/src/app
 ---> Using cache
 ---> b635d27109d9
Step 3/5 : ENV NODE_ENV development
 ---> Using cache
 ---> d8358f2dc7b3
Step 4/5 : COPY . .
 ---> 90a3b228a863
Step 5/5 : RUN npm install
 ---> Running in e65e1c18dc6a
added 99 packages from 139 contributors and audited 194 packages in 4.355s
found 4 vulnerabilities (3 low, 1 critical)
  run `npm audit fix` to fix them, or `npm audit` for details
Removing intermediate container e65e1c18dc6a
 ---> a5fd730535ce
Successfully built a5fd730535ce
Successfully tagged test-app:development
/tmp/test-app $ docker run --rm -it --init -v "${PWD}:/usr/src/app" -p 3000:3000 test-app:development ls -l | grep node_modules
drwxr-xr-x   94 root     root          3196 Feb 11 18:42 node_modules
  • Now the node_modules folder is in the source but it’s in the .dockerignore file, so shouldn’t get copied to container
  • Again it looks like npm install happens, but no warnings this time
  • But the node_modules folder this time is there, so it looks like it got copied even though it was in the .dockerignore file

What’s going on?

Why does RUN command not install node modules in container?

Why do the folders in .dockerignore get copied to container?

Question Answer

You are mounting your local host directory into your container and thus hiding all contents in your container’s /usr/src/app folder. In the first case, you did not install the dependencies, therefore the node_modules folder is nowhere to be found, because it is not in your host directory. In the second case, you have the node_modules folder locally and it gets mounted into the container. The .dockerignore is working fine.

Remove the -v option from your docker run command and everything will work as expected.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.