Tuesday, 27 October 2020

Substituting the npm links for Docker container (case with Node.js "node:12.4" and "docker-compose" file)

Project

enter image description here

There are dependencies provided via Lerna (I suppose, it using the npm link) inside node_modules (assume dependency_a and dependency_b). The project building works without Docker, but Docker don't see the links to dependency_a and dependency_b. With below docker-compose.yaml:

version: "3"

services:

  logic:
    image: node:12.4
    command: npm run 'SPA incremental building' && npm run 'Run server'
    volumes:
      - .:/Application
    working_dir: /Application

I have error:

Error: Cannot find module '/packages/dependency_a/bin/dependency_a'.

Conceptual solution

For Docker container only (not local environment) replace npm links inside node_modules to such links as Docker can understand. It's important that Docker must see the changes of these dependencies because the are being frequently updated.

It could be something like:

version: "3"

services:

  logic:
    image: node:12.4
    command: npm run 'SPA incremental building' && npm run 'Run server'
    volumes:
      - .:/Application
      - ../../packages/dependency_a:/Application/node_modules/dependency_a
      - ../../packages/dependency_b:/Application/node_modules/dependency_a
    working_dir: /Application
#    ports:
#      - "3000:3000"

But

  1. I am not sure what I actually doing.
  2. I don't feeling good about intersection of volumes (second and third volumes overrides first).
  3. Above settings does not works.

Off course, I telling about local development mode, not about production.

Related article analysis

The similar situation considering in the article Developing a new Node module in a Docker container without using NPM link. The solution for the case in this article:

RUN mkdir -p /usr/src/node_modules
 
ENV PATH /usr/src/node_modules/.bin:$PATH

and the in docker-compose.yaml:

volumes:
- .:/usr/src/app
- ../redux-beacon-slack:/usr/src/node_modules/redux-beacon-slack

Why this solution does not suite with my case because I am using node:12.4 image. I suppose, the paths like usr/src are actual for images of operation systems, but I don't understand if I need to create the file system (like bin, boot, cdrom, home, root, etc.) in my node:12.4 image.

Inappropriate solutions

Use only published npm libraries without npm links

The well-organized development environment is the basic requirement of modern IT industry.. In relation to locally developing npm dependencies:

  1. No excess realizes (to npm).
  2. If we changed something in locally developing dependency, the changes must immediately reflect on application (what npm link providing). E. g. if we added console.log() somewhere in dependency_a, we instantly see this output in our application.

Changing of project structure

IMHO good software must adapt to custom project structure, not force own project structure.



from Substituting the npm links for Docker container (case with Node.js "node:12.4" and "docker-compose" file)

No comments:

Post a Comment