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

查看:53
本文介绍了使用本地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.

可以做到吗?

推荐答案

是的,这是可能的,但是有点难看.您遇到的问题是,当涉及到 build上下文时,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.使用.作为其 build上下文,并且由于未另行指定,因此 ./Dockerfile作为Dockerfile.不能在Dockerfile中引用 build上下文之外的文件或路径(因此,没有 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.

因此,您需要确保 apps shared 都在 build上下文中.一种方法是像这样将所有 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_app my_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天全站免登陆