'How to describe running npm script in Dockerfile

I described in Dockerfile the command to execute npm script:

WORKDIR /usr/src/app/server
CMD ["npm", "run", "build"]

It's just a webpack script which build my project

"scripts": {
  "build": "webpack --mode development --open",
  "watch": "webpack --mode development --open --watch",
  "prod": "webpack --mode production"
},

During build i can see that command was executed:

Step 23/28 : WORKDIR /usr/src/app/server
 ---> Running in e85934faf65a
Removing intermediate container e85934faf65a
 ---> 9f14cb58deca
Step 24/28 : CMD [ "npm", "run", "build" ]
 ---> Running in 6d3157d2e962
Removing intermediate container 6d3157d2e962
 ---> 9237c0a03fe9
Step 25/28 : WORKDIR /usr/src/app
 ---> Running in 3888c83ed88f

I expect to get generated dist folder. But I can see that folder is not generated inside container

root@82a01ee6d1fe:/usr/src/app/server# ls
package-lock.json  package.json  src  tsconfig.json  webpack.config.js

How to execute npm script in Docker file correctly?
What i did wrong?



Solution 1:[1]

I solve this problem using next command

RUN npm run build

That was really executed unlike CMD ["npm", "run", "build"]

And I had one problem with webpack. It didn't work from node_modules and i installed that globally:

RUN npm install -g webpack webpack-cli

Inside container i found dist folder and file bundle.js that generated by webpack:

root@49f66ba44ab9:/usr/src/app/server# ls
dist  node_modules  package-lock.json  package.json  src  tsconfig.json  webpack.config.js
root@49f66ba44ab9:/usr/src/app/server# ls ./dist
bundle.js

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dwlz