使用本地 npm 依赖项设置 docker nodejs 应用程序 [英] Setting up docker nodejs application with local npm dependencies

查看:16
本文介绍了使用本地 npm 依赖项设置 docker nodejs 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想开始容器化我们的应用程序,但我们偶然发现了本地依赖项的一些问题.

We want to start containerizing our applications, but we have stumbled upon some issues with local dependencies.

我们有一个 git 存储库,其中有许多节点包,位于共享"文件夹下,以及需要这些包的应用程序.

We have a single git repository, in which we have numerous node packages, under "shared" folder, and applications that require these packages.

假设我们的文件夹结构如下:

So let's say our folder structure is as follows:

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

在 my_app package.json 我们有以下依赖:

in my_app package.json we have the following dependency:

{
  "dependencies": {
    "shared-module": "file:../../shared/shared_module"
  }
}

这里的问题是,因为我们想将my_app"移动到容器中运行,我们需要 npm 安装我们的本地依赖项.

The issue here is that because we want to move "my_app" to run in a container, we need to npm install our local dependency.

这个可以吗?

推荐答案

是的,有可能,但有点难看.您的问题是 Docker 在其构建上下文方面非常严格.我不确定你对这个概念有多熟悉,所以这里是 文档的介绍:

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:

docker build 命令从 Dockerfile 和 context 构建映像.

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

例如,docker build . 使用 . 作为其 构建上下文,由于没有另外指定,./Dockerfile 作为 Dockerfile.构建上下文之外的文件或路径不能在 Dockerfile 中被引用(所以没有 COPY ..).

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 ..).

您的问题是,在 Docker 构建期间,构建上下文 不能保留.如果您有多个要构建的应用程序,您通常会为每个应用程序添加一个 Dockerfile.

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

当然,您可以将 cd 放入 my_app 并使用 docker build . 来构建应用程序的 Docker 映像.这样做的问题是您无法从构建中访问 ../../shared,因为它在上下文之外.

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.

所以你需要确保 appsshared 都在 build context 中.一种方法是将所有 Dockerfile 放在 src 中,如下所示:

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

然后您可以通过显式指定上下文和 Dockerfile 来构建应用程序:

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

src$ docker build -f Dockerfile.my_app .

或者,您可以将 Dockerfile 保存在 my_appmy_other_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 .

这也应该有效.在这两种情况下,构建都是从 src 中执行的,这意味着您需要注意 Dockerfile 中的路径.工作目录仍然是src:

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 /src
COPY ./shared /src/shared
COPY ./apps/my_app /src/apps/my_app
RUN cd /src/apps/my_app && npm install

希望能帮助您入门.

这篇关于使用本地 npm 依赖项设置 docker nodejs 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆