angelok angelok - 4 months ago 32
Node.js Question

Nodejs private module and Docker containers

I've got a nodejs project that references a module I wrote and hosted by private github repo. Dependencies in package.json look something like this:

"dependencies": {
... other stuff ...
"my_module": "git+https://github.com/me/mymodule.git",
}


That's fine, but I'd like to create a Docker container for the application, but I don't want git inside the container. I know I can host via private npm repos, but I'd love to find a way to have the build process pull the source (including that module) and then copy it to the container.

I'm fine with doing an npm install in the container, but it will not like the git dependency. Alternatively, I don't want to do an npm install on the build machine because I want the freedom to choose any container I want... I don't want the build machine to snag windows binaries to a mongo module, for example, and copy those to my debian container.

One option I considered was putting the dependency to "my_module" in devDependencies, then within the Docker container do "npm install --production", then copy the one module over. That's just inconsistent with the intent of devDependencies.

Any better/recommended solutions? I'm open to not hosting the module in github if there's a better way (but I use it on a few projects that only make sense for this client).

Answer

Theres a pretty easy solution to this. Build the node application

npm install etc

Then in your dockerfile include the COPY command, telling it where the node projects install directory is, and where you want it to copy to.

Edit:

To address the issue brought up by @angelok you should use npm rebuild once it's copied into the docker image so that it builds with the correct dependencies relative to the OS of the Docker image instead of the OS in which the node packages were initially installed. See docs for rebuild here.