简体   繁体   English

docker-compose → 为链接容器设置通配符域名

[英]docker-compose → set wildcard domain name for linked container

I have two containers: application and MINIO service我有两个容器:应用程序和MINIO服务

Let's say docker-compose.yml is:假设docker-compose.yml是:

version: "3"

services:
  s3:
    image: minio/minio:RELEASE.2021-11-09T03-21-45Z
    expose:
     - "9000"
     - "9001"
    container_name: s3
    command: server /data --console-address ":9001"
    environment:
     - MINIO_DOMAIN=s3
     - MINIO_ROOT_USER=some_user
     - MINIO_ROOT_PASSWORD=some_password
    healthcheck:
     test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
     interval: 30s
     timeout: 20s
     retries: 3
    volumes:
     - /tmp/data:/data

  app:
    image: my_image
    container_name: app
    build:
      context: .
      dockerfile: ./Dockerfile
    depends_on:
      - s3

app container connecting to MINIO via http://s3:9000 endpoint URL app容器通过http://s3:9000端点 URL 连接到MINIO

When I run当我跑

aws --profile myprof --endpoint-url="http://s3:9000" s3 ls  s3://bucketname/path/to/file

all works fine.一切正常。

But some of third-party libraries needed for my app using virtual-host styled path:但是我的app程序需要一些使用虚拟主机样式路径的第三方库:

http://bucketname.s3:9000/path/to/file

And of course bucketname.s3 can't be resolved.当然, bucketname.s3也无法解析。

I tried to set 172.19.0.4 bucketname.s3 (container local IP) inside /etc/hosts and all works fine.我尝试在/etc/hosts中设置172.19.0.4 bucketname.s3 (容器本地 IP),一切正常。 But I can't update /etc/hosts every time for every new bucket.但是我不能每次都为每个新存储桶更新/etc/hosts

So is here any way to set wildcard hostname *.s3 in docker?那么这里有什么方法可以在 docker 中设置通配符主机名*.s3吗?

My application use python:3.7.0-alpine image我的应用程序使用python:3.7.0-alpine image

Result of cat /etc/resolv.conf is cat /etc/resolv.conf的结果是

nameserver 127.0.0.11
options edns0 ndots:0

So looks like it is possible to set domain inside local host resolver service by using Dockerfile , is here some way to do that?所以看起来可以使用Dockerfile在本地主机解析器服务中设置域,这里有什么方法吗?

Memo added 2022/01/25备忘录已添加 2022/01/25

I added network alias to s3 container as temporary solution to be able add exact bucket names at docker-compose up -d timing, but that is not a solution because I still can't create new buckets on the fly without restart containers:我将网络别名添加到s3容器作为临时解决方案,以便能够在docker-compose up -d时间添加准确的存储桶名称,但这不是解决方案,因为我仍然无法在不重新启动容器的情况下即时创建新存储桶:

version: "3"

services:
  s3:
    image: minio/minio:RELEASE.2021-11-09T03-21-45Z
    expose:
     - "9000"
     - "9001"
    container_name: s3
    command: server /data --console-address ":9001"
    environment:
     - MINIO_DOMAIN=s3
     - MINIO_ROOT_USER=some_user
     - MINIO_ROOT_PASSWORD=some_password
    healthcheck:
     test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
     interval: 30s
     timeout: 20s
     retries: 3
    volumes:
     - /tmp/data:/data
    networks:
      default:
        aliases:
         - bucketone.s3
         - buckettwo.s3

Also I tried to set record inside external separated DNS server:我还尝试在外部分离的 DNS 服务器内设置记录:

*.s3  IN CNAME  s3.

But this way is not working too.但这种方式也行不通。 Looks like container trying to resolve s3.看起来像容器试图解析s3. by using same DNS server instead of use Docker internal DNS resolver (where s3 host exists).通过使用相同的 DNS 服务器而不是使用 Docker 内部 DNS 解析器(存在s3主机)。 So looks like only possible solution is to set DNS server integrated with Docker that can manage container internal dynamic IPs and records所以看起来唯一可能的解决方案是设置 DNS 服务器与 Docker 集成,可以管理容器内部动态 IP 和记录

(This might be not the best answer, but probably better than nothing). (这可能不是最好的答案,但可能总比没有好)。

I already had a custom DNS server on the host system (in my case, Acrylic).我已经在主机系统上有一个自定义的 DNS 服务器(在我的例子中是 Acrylic)。 What I did as a quick fix:我做了什么作为快速修复:

  1. from the depending app container: ping anotherservice.loc (to get the service container's IP, in my case it was 172.18.0.7)从依赖的应用程序容器: ping anotherservice.loc (获取服务容器的 IP,在我的例子中是 172.18.0.7)
  2. on the own DNS server, added an entry 172.18.0.7 *.anotherservice.loc and restarted the DNS在自己的 DNS 服务器上,添加条目172.18.0.7 *.anotherservice.loc并重新启动 DNS
  3. optionally, cleared host system DNS cache: ipconfig /flushdns (this command is for Windows)可选地,清除主机系统 DNS 缓存: ipconfig /flushdns (此命令适用于 Windows)

In theory:理论上:

  • maybe you could hardcode this IP in docker-compose.yml , to avoid the need to edit DNS settings, in case Docker changes it in future.也许你可以在 docker-compose.yml 中硬编码这个docker-compose.yml ,以避免需要编辑 DNS 设置,以防 ZC5FD214CDD0D2B3B4B4272 中更改它。 Or,或者,
  • maybe you could find and set up a custom DNS server inside the depending container instead.也许您可以在依赖容器内找到并设置自定义 DNS 服务器。 There is some space for creativity here, I just tried to give the idea.这里有一些创意空间,我只是试着给出这个想法。

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

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