This docker utility container will help us execute npm commands without installing node in our machine. I've used node 20.10-alpine version in this project.
docker run -it --rm --name node-util node:20.10-alpine
docker run -it -d --rm --name node-util node:20.10-alpine
- Executing npm command (say
npm init
)
docker exec -it node-util npm init
The docker exec
command helps us to execute(run) a new command inside the running container.
- Build the image
docker build . -t actionanand/node-util
- You can run any command begining with
npm
(leavenpm
and add remaining at the end of following command)
docker run -it --rm --name node-util -v "D:\AR_extra\rnd\docker\node-util:/app" actionanand/node-util
For wsl2, mac and linux
docker run -it --rm --name node-util -v $(pwd):/app actionanand/node-util
To run npm init
command
docker run -it --rm --name node-util -v $(pwd):/app actionanand/node-util init
To run npm install express
docker run -it --rm --name node-util -v $(pwd):/app actionanand/node-util install express
docker-compose run
is used to run single service from multiple services present indidedocker-compose.yaml
Usage
docker-compose run --rm <service_name>
Consider npm-util
is the registered service name in docker-compose.yaml
and If you want to execute npm init
, the following will be the command to be executed!
docker-compose run --rm npm-util init
So if npm
is the registered service name,
docker-compose run --rm npm init