I'm trying to run the npm init
command to spit out a package.json file on my local machines current working directory, by running a Node image on docker.
I have attempted to do it like so but haven't had the desired results described above.
docker run -d -v $pwd:~/tmp node:18-alpine3.14 "cd ~/tmp && npm init"
The command I'm passing above at the end gets passed to the Node application rather than the container it is held inside. I know this because the container exits with Error: Cannot find module '/cd ~/tmp && npm init'
.
How can i execute commands for the container to receive rather than Node inside it in this example?
CodePudding user response:
You cloud use sh -c "some command"
as command, but I think it's cleaner, like below.
Using the workdir flag and also using your local user and group id so that you don't have to fix the permissions later on.
docker run --rm \
--user "$(id -u):$(id -g)" \
--workdir /temp \
--volume "$PWD:/tmp" \
--tty
--interactive
node:18-alpine3.14 npm init
I am also using tty and interactive so that you can answer the questions from npm init. If you dont want questions, use npm init -y
.