Help - How to run npm builds using docker?
Gaurav Saini

Gaurav Saini @sainig

About: Proud coder, gaming lover, traveller, cooking enthusiast, but sometimes I’m like (╯°□°)╯︵ ┻━┻

Location:
Jaipur, India
Joined:
Jan 27, 2020

Help - How to run npm builds using docker?

Publish Date: Aug 17 '23
1 2

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!!

Comments 2 total

  • Neel
    NeelAug 21, 2023

    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

    • Gaurav Saini
      Gaurav SainiAug 22, 2023

      Thanks Neel, this worked as expected

Add comment