DEV Community

Gaurav Saini
Gaurav Saini

Posted on

Help - How to run npm builds using docker?

Hello šŸ‘‹

I want to run npm run build scripts using a Dockerfile/docker-compose.yml file and get the build output on my host file system. Is there any way I can achieve this?

Basically, what I'm looking for is to run a single command, like

docker-compose up

# OR

docker build -t my-builder . && docker run my-builder

# or maybe even a shell script
Enter fullscreen mode Exit fullscreen mode

which would spin up a container, run the npm build script inside the container, and with the help of either a volume or bind mount (or some other concept I'm not aware of šŸ˜…) put the build output on a specified directory on the host file system.

Any help would be greatly appreciated šŸ™.

Thanks!!

Top comments (2)

Collapse
 
neeldev96 profile image
Neel

I think, I get what you are trying to achieve. We used to do a similar thing for generating linux Golang binaries from our Macbook by running a single docker command

The following command will do the trick for you

# This will put the build folder in your current directory itself from where you are running the command

docker run --rm --workdir /app -v $(pwd):/app --entrypoint "npm" node:18.16.1-alpine3.18 run build
Enter fullscreen mode Exit fullscreen mode

I am assuming that your goal is to generate the build from a different platform. If that is not the case then ensure that whichever base image you are choosing generates the build with compatible outcomes

Collapse
 
sainig profile image
Gaurav Saini

Thanks Neel, this worked as expected