MeshCentral: [Request] Don't install modules at runtime

Background

I just got some micro desktops with vPro support, and I wanted to install MeshCentral using the Docker image in uldiseihenbergs/meshcentral-builds. A best practice for Docker containers is to run services as a unique user (particularly, non-root), which can be accomplished with --user or user: in a docker compose file. Unfortunately, doing that didn’t work because of a permissions issue with “module installation”.

I dug into the code and discovered that dependencies are being gathered and npm install is being executed by the main script at runtime: https://github.com/Ylianst/MeshCentral/blob/990da39fdeb484d243b83c0e4a626a4e6a96a834/meshcentral.js#L4019-L4107

Docker Issues

Permissions

The above means that the permissions issue in Docker can never really be resolved. The image must be created with a particular user selected at build time, and that generally should be root. The container should then ideally be able to run as any user, but in order to be able do an npm install at run time, the build user would have to be chosen in advance to be the same as the run user. (Or you could grant everyone write permissions, but that’s not a great choice for security).

Non-ephemeral container

There’s also a separate but related issue when it comes to Docker, and that is that you should not write directly to the container’s writable layer because 1) the container is ephemeral and should contain everything needed to run the application, and 2) for performance reasons. You can get around this with a volume, but then you run into issues with volume persistence in addition to violating Docker image design principles.

Dangers With Runtime Module Installation

I did a lot of searching, and I couldn’t find any other npm packages, services, etc, that install modules at runtime in this way. From my own experience, and according to the nodejs docs, this is a bad practice. It’s fragile in that you have to maintain the main dependency list in two places (package.json and meshcentral.js), and from looking through the issues it seems that module installation issues come up frequently. The whole point of npm is to resolve dependencies and install packages from a package.json (really: package-lock.json). It’s made to take care of this for you, and if you properly define all of your dependencies I would guess you’ll get far fewer reports of issues with installation and update because all of the versioning is frozen and reproducible.

Disk Space

I’m curious why it was done this way. I’m guessing it’s to save the disk space of modules that aren’t actually used by the config, and I am seeing 21MB on a “default” install, and 357MB with all modules installed, and 100MB of that is googleapis. Nonetheless, there’s a danger in modifying your “executable” based on user configuration, and I don’t think even the full 357MB is so concerning that it’s worth trying to save.

Request

I think the project (and Docker users!) would benefit from moving all of the dependencies into package.json and removing the runtime install code. I’d also be happy to contribute the change, including documentation updates, etc. Another benefit of doing this is it makes installing and running meshcentral trivial with npx

Bonus: I’d be happy to contribute an official docker container too, including github actions to automatically publish it.

The exception to the rule

npm doesn’t support os-specific dependencies, so you can’t add windows-only packages to dependencies or Unix systems will fail installation. You would add it to optionalDependencies, where the failure won’t prevent Unix/Mac installs, but windows users should get the packages. There is some risk that those packages fail to install on windows for some other reason, so a runtime check might be warranted.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 3
  • Comments: 20 (5 by maintainers)

Most upvoted comments

@Jamesits i decided that hopefully before the next release this week, im going to add in another docker image, something like latest-full and this will include EVERY SINGLE NPM PACKAGE we use on the project then if people want to download the full package they can do 👍 hope this is ok?

its also already on my to-do to add in the multi database tags too so it would be something like latest-mysql, latest-postgres, latest-mongodb and each will include the npm package that needs for that database!

i then HAD to install build-essential and make OS packages because of the syslog module requires them to build

For something like this, you can (and should) run a separate build step in its own container, then copy the results into the final image, so you don’t have the packages in it.

so add that on top of the already 1GB, so the image is now 1.5GB in size!

is this acceptable? as personally thats a ridiculous size!

Yes it’s totally fine. There are plenty of docker images that are even larger. In any case, the users will have to download all the packages anyways. It’s best practice to just include them to the image. If you want to reduce the image size, you should reduce the number of dependencies.

Possibly! But then i will try to fix it there 😁

This is still an issue for both container image users. To elaborate:

Plus additional trouble for the project maintainers:

The solution: just add all the packages into package.json and generate a lock file with the package manager of your choice! Then remove the package checks completely from meshcentral.js.

you can’t add windows-only packages to dependencies or Unix systems will fail installation.

All the Windows-only packages correctly installs under Linux and macOS! I’ve tested that. If you want to try for yourself, you can clone https://github.com/Jamesits/docker-meshcentral2.git on your OS and do a pnpm install (outside Docker).

for example someone ran the docker image with syslogtcp which required python in the docker image… I would like to try creating separate images at some point mc/mongodb mc/mysql mc/python etc if that makes sense

No, this is not the way to do it. Your container image should only contain everything your program need to run. For example, if you need to call mongodb client tools or other database client for daily backup (a function of your program), then include them. syslogtcp or even python is not of one of these cases.

If an user want to collect syslog of a container, they are better off doing it in a way that is non-intrusive. There are a lot of ways to achieve this. One common way is to configure an alternative logging driver on their container runtime; another common choice is to use a sidecar container.

Containers are designed to be inherited. If an user really need a certain tool they use inside the container, they can simply add layers to your container with the following Dockerfile:

FROM your-image:tag
RUN ...

This is cleaner and more managable than tell the upstream packager to include the universe in their container image. Plus, large container images is not a problem. OCI container images are designed to share layers with each other to reduce disk usage, and ~1GiB is not an extreme size for such a container.


Besides that, MeshCentral tries to find its config files relative to its code which does not work if the node_modules is not a flatten one. Not all npm package manager create a flattened node_modules, and an NPM package should not change anything inside the node_modules. Doing that is very dangerous and potentially disruptive. (e.g. https://github.com/Ylianst/MeshCentral/issues/4318 )


The way that the official container image being constructed has brought much trouble to me, so much that I had to maintain my own container image for MeshCentral. Here’s my approach, for reference:

  • Toolchain: corepack + pnpm (just what I’m most familiar with)
  • All packages locked and hash-checked during a CI container build
  • A script to extract the dependency packages automatically (albeit not the best way to do it…)
  • Upstream updates trigger auto PRs by Renovate bot

More than happy to accept PR requests…

I’d like to contribute, but the changes required exceed what I define as a small progressive patch (small enough so that it has a high chance of being reviewed even if submitted by a random person). And it’s the engineering choices I mentioned above that is proactively preventing people from contributing by making any change unneedingly expensive. The same engineering choices lead to a lot trouble and confusion to other users over the years, no matter if they use MeshCentral inside a container or not. And it cost you guys time to deal with these user confusion and issues.

Maybe it’s time to fix that.

(Don’t know how to write an ending for this comment. Sorry.)

PS: Thanks for reading my essay 😃