Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm package.json and docker (mounting it...)

I am using Docker, so this case might look weird. But I want my whole /data directory to be mounted inside my docker container when developing.

My /data folder container my package.json file, an app directory and a bunch of other stuff. The problem is that I want my node_modules folder to NOT be persistent, only the package.json file.

I have tried a couple of things, but package.json and npm is giving me a hard time here...

  • Mounting the package.json file directly will break npm. npm tries to rename the file on save, which is not possible when its a mounted file.
  • Mounting the parent folder (/data) will mount the node_modules folder.
  • I cant find any configuration option to put node_modules in another folder outside /data, example /dist
  • Putting package.json in /data/conf mounting the /data/conf as a volume instead wont work. I cant find any way to specify the package.json path in npmrc.
  • Putting package.json in /data/conf and symlinking it to /data/package.json wont work. npm breaks the symlink and replaces it with a file.

Copying data back and forth to/from inside the docker container is how I am doing it now.. A little tedious.. I also want a clean solution..

like image 360
xeor Avatar asked Nov 05 '14 12:11

xeor


People also ask

How do I stop npm installing every time docker?

To avoid the npm install phase on every docker build just copy those lines and change the ^/opt/app^ to the location your app lives inside the container. That works.

What is npm install package json?

Description. This command installs a package and any packages that it depends on. If the package has a package-lock, or an npm shrinkwrap file, or a yarn lock file, the installation of dependencies will be driven by that, respecting the following order of precedence: npm-shrinkwrap. json.

Is npm a docker?

npm is the command-line interface to the npm ecosystem. It is battle-tested, surprisingly flexible, and used by hundreds of thousands of JavaScript developers every day. Docker can be classified as a tool in the "Virtual Machine Platforms & Containers" category, while npm is grouped under "Front End Package Manager".


2 Answers

As you have already answered, I think that might be the only solution right now.

When you are building your Docker image, do something like:

COPY data/package.json /data/
RUN mkdir /dist/node_modules && ln -s /dist/node_modules /data/node_modules && cd /data && npm install

And for other stuff (like bower, do the same thing)

COPY data/.bowerrc /data/
COPY data/bower.json /data/
RUN mkdir /dist/vendor && ln -s /dist/vendor /data/vendor && cd /data && bower install --allow-root

And COPY data/ /data at the end (so you are able to use Dockers caching and not having to do npm/docker installation when there is a change to data.

You will also need to create the symlinks you need and store them in your git-repo. They will be invalid on the outside, but will happely work on the inside of your container.

Using this solution, you are able to mount your $PWD/data:/data without getting the npm/bower "junk" outside your container. And you will still be able to build your image as a standalone deployment of your service..

like image 83
edos Avatar answered Oct 21 '22 03:10

edos


A similar and alternative way is to use NODE_ENV variable instead of creating a symlink.

RUN mkdir -p /dist/node_modules RUN cp -r node_modules/* /dist/node_modules/ ENV NODE_PATH /dist/node_modules

Here you first create a new directory for node_modules, copy all modules there, and have Node read the modules from there.

like image 5
Deniz Ozger Avatar answered Oct 21 '22 03:10

Deniz Ozger