简体   繁体   English

在构建 docker-compose v2 期间访问 docker 网络?

[英]Access a docker network during build of docker-compose v2?

Summary概括

Having a docker-compose.yml file that builds an image like this:有一个docker-compose.yml文件来构建这样的图像:

services:
  my-service:
    build: 
      context: My.VS.AppFolder
    networks:
      - my-docker

networks:
  my-docker:
    external: true

the defined network is only avaliable in the ENTRYPOINT .定义的网络仅在ENTRYPOINT中可用。 But not during build.但不是在构建期间。 How can I access another container on the same network my-docker during build of the Dockerfile ?如何在构建Dockerfile期间访问同一网络my-docker docker 上的另一个容器?

Use case: Fetching NuGet packages from private repo用例:从私有仓库获取 NuGet 包

Description of the use case用例描述

I have an ASP.NET Core MVC 2.1 web application.我有一个 ASP.NET 核心 MVC 2.1 web 应用程序。 For a better seperation of concerns, I want to implement certain features in another seperated web app (eg admin interface).为了更好地分离关注点,我想在另一个单独的 web 应用程序(例如管理界面)中实现某些功能。 To avoid copying shared things like app layout (Razor) or some helper utility classes, I created a shared project for those things.为了避免复制应用程序布局 (Razor) 或一些辅助实用程序类等共享内容,我为这些内容创建了一个共享项目。

So there are three projects now:所以现在有三个项目:

  • MyApp (The original application) MyApp (原始应用程序)
  • MyApp.Core (For shared things) MyApp.Core (用于共享的东西)
  • MyApp.Admin我的应用程序管理员

Since MyApp.Core needs to be referenced from the other projects, I installed BaGet as simple NuGet hosting repo for my docker build environment.由于MyApp.Core需要从其他项目中引用,因此我将BaGet 安装为简单的 NuGet 托管存储库,用于我的 docker 构建环境。 This container is internally referenced with it's DNS name in nuget.config , created at solution level of MyApp (same on new MyApp.Admin but let's focus on MyApp for simplicity).此容器在nuget.config中使用其 DNS 名称进行内部引用,在MyApp的解决方案级别创建(在新的MyApp.Admin上相同,但为了简单起见,让我们专注于MyApp )。

The problems问题

In the Dockerfile of MyApp I'm doing now this:MyAppDockerfile中,我现在正在这样做:

RUN dotnet restore --configfile nuget.config

and need to access the dns name called baget on the my-docker network.并且需要访问my-docker docker 网络上名为baget的 dns 名称。 Researchs show that this is only possible with at least version 3.4 of docker-compose , and seems still not officially documentated.研究表明,这只有在docker-compose的至少 3.4 版才有可能,而且似乎还没有正式记录。 But Docker removed several options from v2 in v3 , for example ressource limits like mem_limits I'm using.但是Docker 从 v3 的 v2 中删除了几个选项,例如我正在使用的mem_limits等资源限制。 From v3, they're only avaliable using swarm, not on single nodes any more.从 v3 开始,它们仅可使用 swarm,不再适用于单个节点。

So I currently don't see any solution than migrating to v3 and swarm, which would cause extra work and complexity without benefits other than this networking issue.所以我目前没有看到除了迁移到 v3 和 swarm 之外的任何解决方案,这会导致额外的工作和复杂性,除了这个网络问题之外没有其他好处。 My project isn't that big that swarm is required.我的项目并不需要swarm那么大。

I found two ways of working around this problem:我发现了两种解决此问题的方法:

Build with docker cli tool instead of docker-compose使用docker cli 工具而不是docker-compose

Instead of docker-compose up -d --build , I manually build the image using dockers CLI tool because it has a --network switch that allows specifying the network during build:我没有使用docker-compose up -d --build ,而是使用 dockers CLI 工具手动构建映像,因为它有一个--network开关,允许在构建期间指定网络:

docker build --network my-docker -t my-service:latest --build-arg ASPNETCORE_ENVIRONMENT=Development My.VS.AppFolder

Now reference this image in docker-compose.yml instead of building it there:现在在docker-compose.yml中引用这个图像,而不是在那里构建它:

services:
  my-service:
    image: my-service:latest

After the image was build, run docker-compose up -d without the --build flag.构建映像后,在不带--build标志的情况下运行docker-compose up -d This causes a bit of overhead since you have to CLI calls and for real tagging like alpine-3.2.1 , this tag need to be specified with an env variable and passed to both docker/docker-compose.这会导致一些开销,因为您必须进行 CLI 调用,并且对于像alpine-3.2.1这样的真正标记,需要使用 env 变量指定此标记并传递给 docker/docker-compose。 But it seems the best working alternative for productive usage.但它似乎是生产使用的最佳替代方案。

Compatibility mode兼容模式

There is a --compatibility switch in docker-compose since 1.20.0 that allows using the new v3 file version, but map swarm options like ressource limits to the locally v2 form. docker-compose起,docker-compose 中有一个--compatibility开关,允许使用新的 v3 文件版本,但 map 群选项,如资源限制到本地 v2 形式。 In other words: You can use specifiy ressources in v3 and they apply on docker-compose when this switch is used.换句话说:您可以在 v3 中使用指定的ressources ,并且在使用此开关时它们适用于docker-compose Otherwise, they would be ignored on docker-compose and only have effect with docker stack deploy .否则,它们将在docker-compose上被忽略,并且仅对docker stack deploy有效。

So with this switch, you can profit from the ability of defining a network during build, without loosing ressource limits.因此,使用此开关,您可以从构建期间定义网络的能力中受益,而不会失去资源限制。 But the documentation warns that this is not stable enough for productive usage:但是文档警告说,这对于生产性使用来说不够稳定:

We recommend against using --compatibility mode in production.我们建议不要在生产中使用 --compatibility 模式。 Because the resulting configuration is only an approximate using non-Swarm mode properties, it may produce unexpected results.由于生成的配置只是使用非 Swarm 模式属性的近似值,因此可能会产生意想不到的结果。

For this reason, I don't consider it as a real solution and use the first approach of building the image using dockers CLI where specifiying a network is possible.出于这个原因,我不认为它是一个真正的解决方案,而是使用第一种使用 dockers CLI 构建图像的方法,其中可以指定网络。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM