Docker Community Forums

Share and learn in the Docker community.

Best approach to run installation operations such as npm install

Hi Docker community,

I have a question about best approaching steps. I’ll try to keep this post straight to the point.

I am working on a legacy application which is migrating to Docker. It’s mostly a PHP/Apache application but has a few node.js scripts in some folders.

I am using docker-compose to start 2 services: web and db.

I want the web service to run some installation operations such as npm install and composer install and save the files to the host, but I’m not sure where to run these operations. I tried to do a multi-stage build where in the first stage i run these operations and on the second stage, I copy the files from the first stage into a mountpoint which is used by the service, but the files aren’t copied to the host system.

I believe this might be a common scenario, so I was wondering what are the best approaches to run these installation operations.

Do you really want/need to run these commands when running the containers? I’d run them during a build stage when creating the images. Did you see http://bitjudo.com/blog/2014/03/13/building-efficient-dockerfiles-node-dot-js/ and, say, https://vuejs.org/v2/cookbook/dockerize-vuejs-app.html?

1 Like

Thank you.
I took a quick look at the links you provided and noticed the project files are being copied to the container - is that the way to go?
The application I’m working on is a 10gb beast so I think copying is an overkill - I’m currently mounting the host folder into the container.

Key of the second link is: the project sources are copied into a build image, but do not end up in the final image. For example, a RUN npm install will put a lot into node_modules in that build image. But in the end only the bundles created by RUN npm build will be copied into the final image.

Of course, I don’t know if that suits your need, but running npm install when running the container feels like the above could really help. (It will also make the first run of the container be faster, as npm install has already been done while building the image.)

If you really need/want to run npm install when running the container, then you’ll need to add those commands to a custom ENTRYPOINT script.

1 Like

What operating systems are supported?
Node can be run on Windows, macOS, many “flavours” of Linux, Docker, etc. (there is a full list on the nodejs Downloads page). Almost any personal computer should have the necessary performance to run Node during development. Express is run in a Node environment, and hence can run on any platform that runs Node.

In this article we provide setup instructions for Windows, macOS, and Ubuntu Linux.

What version of Node/Express should you use?
There are many releases of Node — newer releases contain bug fixes, support for more recent versions of ECMAScript (JavaScript) standards, and improvements to the Node APIs.

Generally you should use the most recent LTS (long-term supported) release as this will be more stable than the “current” release while still having relatively recent features (and is still being actively maintained). You should use the Current release if you need a feature that is not present in the LTS version.