vy218
vy218

Reputation: 1017

Docker isn't executing RUN commands correctly, and doesn't read dockerignore file

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

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

What's going on?

Why does RUN command not install node modules in container?

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

Upvotes: 0

Views: 368

Answers (1)

Tobias Geiselmann
Tobias Geiselmann

Reputation: 2476

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.

Upvotes: 1

Related Questions