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

The question remains the same whether this is done in the build stage or run stage.

I don’t understand what you’re trying to say, or may even be asking.

In general, things such as npm install should be done when building an image, and only the results of some npm run build should be copied into the final image.