link or install local typescript library inside docker container

Viewed 146

I'm still dockerizing our microfrontend application. Currently we use React and Angular and a shared library for the communication. The library is developed by ourself and installed locally via npm.

'- angular-project
'- react-projects
'- shared-libs
   '- communication-lib

We are installing the communication-lib locally with

npm install ..\shared-libs\communication-lib

Running everything locally works fine. But as we move to docker, there are several issues and I can't figure out a good solution. Below are the first few lines of the angular projects Dockerfile

FROM node:16-alpine As development
WORKDIR /usr/src/app/frontend
COPY ./angular/package.json ./angular/package-lock.json ./
RUN npm i
...
# already crashed here

This is the point where docker fails because the lib is not known to the docker image. I also added a volume to share the code of the shared lib with this image, but then i still have the issue of not having a already build node_module.

I'm really thankful if someone could provide me some useful hints how to solve this issue.

My thougths:

Is it somehow possible to create a standalone docker for the libs and use these container in my angular projects container?

Can I somehow copy/build and link the lib inside the container ?

1 Answers

TL;DR

You can't do exactly what you want, but there are alternatives...

Explanation

The Local Package Link Story

When you add a local package via npm install ../<folder> it creates a symbolic link (symlink) inside of node_modules to that folder instead of actually copying the content of ../<folder> over to your node_modules. It does that so in case you make an update in your linked package, it seamlessly reflects in your project in real-time.

The Docker Build Story

When you run docker build it sends the build context to the deamon. The root of the build context it where the Dockerfile is located. That means that the deamon has no access to any folder on the filesystem outside of the build context; i.e. a Dockerfile located inside of angular-project will cause the deamon not to be able to access the shared-libs folder, so you will not be able to copy anything from there.

This is also, why a COPY or an ADD command inside a Dockerfile cannot follow symlinks outside of the build context. Meaning it would not be able to copy that content into the image.

Solutions

Moving Up Dockerfile

One thing you can do is move the Dockerfile to a folder that contains all your expected packages, i.e. in your case one folder up where both the angular-project is as well as the shared-libs folder is. This of course would complicate other things like selecting the Dockerfile to build, as well as the commands inside the Dockerfile so they copy the correct items and only them into the image.

Packing Local Package

Another option, is to pack the communication-lib (npm pack ..\shared-libs\communication-lib) and then reference the tarball (npm install communication-lib.tgz) in angular-project instead. See here. You can add the tarball to your .gitignore if you don't want to check it in, but you will have to run the npm pack command on first build and every time there is an update to the shared library.

Related