Setting up docker nodejs application with local npm dependencies Setting up docker nodejs application with local npm dependencies node.js node.js

Setting up docker nodejs application with local npm dependencies


Yes, it's possible but a little bit ugly. The problem for you is that Docker is very restrictive when it comes to its build context. I'm not sure how familiar you are already with that concept, so here is the introduction from the documentation:

The docker build command builds an image from a Dockerfile and a context.

For example, docker build . uses . as its build context, and since it's not specified otherwise, ./Dockerfile as the Dockerfile. Files or paths outside the build context cannot be referenced in the Dockerfile (so no COPY ..).

The issue for you is that during a Docker build, the build context cannot be left. If you have multiple applications that you want to build, you would normally add a Dockerfile for each app.

src/├── apps   │   ├── my_app│   │   └── Dockerfile│   └── my_other_app│       └── Dockerfile└── shared    └── shared_module

Naturally, you would cd into my_app and use docker build . to build the application's Docker image. The issue with this is that you can't access ../../shared from the build, since it's outside of the context.

So you need to make sure both apps and shared is in the build context. One way would be to place all Dockerfile in src like so:

src/├── Dockerfile.my_app├── Dockerfile.my_other├── apps│   ├── my_app│   └── my_other_app└── shared    └── shared_module

You can then build the applications by explicitly specifying the context and the Dockerfile:

src$ docker build -f Dockerfile.my_app .

Alternatively, you can keep the Dockerfiles inside my_app and my_other_app, and point to them:

src$ docker build -f apps/my_app/Dockerfile .

That should also work. In both cases, the build is executed from within src, which means you need to pay a little attention to the paths in the Dockerfile. The working directory is still src:

COPY ./apps/my_app /src/apps/my_app

By mirroring the folder structure you have locally, you should be able to make your dependencies work without any changes:

RUN mkdir -p /srcCOPY ./shared /src/sharedCOPY ./apps/my_app /src/apps/my_appRUN cd /src/apps/my_app && npm install

Hope that helps you get started.