【问题标题】:How to provide custom name resolution in Docker containers如何在 Docker 容器中提供自定义名称解析
【发布时间】:2018-12-26 08:34:06
【问题描述】:

假设我有以下文件结构

.                                  
├── docker-compose.yml             
└── webserver                      
    ├── Dockerfile                 
    ├── html                       
    │   ├── test1                  
    │   │   └── index.html         
    │   └── test2                  
    │       └── index.html         
    └── vhosts.conf                

4 directories, 5 files

docker-compose.yml:

version: "3.1"
services:

    webserver:
        build: webserver
        container_name: web
        working_dir: /application
        volumes:
            - ./webserver/html/:/application
        ports:
            - "80:80"

    other:
        image: php:7.2.7
        container_name: other
        tty: true

Dockerfile:

FROM php:7.2.7-apache

COPY vhosts.conf /etc/apache2/sites-enabled/000-default.conf

vhosts.conf:

<VirtualHost *:80>
    ServerName test1.localhost

    DocumentRoot /application/test1
    <Directory /application/test1>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName test2.localhost

    DocumentRoot /application/test2
    <Directory /application/test2>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

index.html 文件只是用一些内容来区分它们。

此外,我在 docker 主机的 /etc/hosts 文件中添加了以下几行:

127.0.0.1   test1.localhost
127.0.0.1   test2.localhost

现在我可以运行 docker-compose builddocker-compose up 来设置和运行容器。在主机或web 容器上运行curl test1.localhost 时,我得到index.html 的预期内容。但是,如果我从 other 容器运行相同的命令,我会得到:

curl: (7) Failed to connect to test1.localhost port 80: Connection refused

我在https://docs.docker.com/v17.09/engine/userguide/networking/configure-dns/ 找到以下引用:

在没有 --dns=IP_ADDRESS...、--dns-search=DOMAIN... 或 --dns-opt=OPTION... 选项的情况下,Docker 使用 /etc/resolv.conf主机(docker 守护进程运行的地方)。

据我了解,容器内的名称解析只是使用 docker 主机的 hosts 文件中的条目。但是,这在other 容器上失败,因为/etc/hosts 文件将名称解析为127.0.0.1,但other 容器未运行网络服务器。我希望将other 上的名称转至web 容器。

这显然是一个最小的例子。对于 Web 应用程序的开发设置,我需要这种行为,我想从与运行 apache 主机的容器不同的容器运行 cronjobs。 cronjobs 运行的脚本对容器中的不同主机执行 http 请求。

我怀疑以某种方式使用 docker 网络配置的解决方案...

【问题讨论】:

    标签: docker dns docker-compose virtualhost


    【解决方案1】:

    好的,在 Docker Slack 频道的帮助下,我找到了一个解决方案,实际上甚至是两个……

    使用 Docker 网络别名

    第一个解决方案使用docker network aliases。本质上,我明确说明了两个容器所属的默认网络,并为 webserver 容器创建了一些别名。

    docker-compose-yml:

    version: "3.1"
    
    services:
    
        webserver:
            build: webserver
            container_name: web
            working_dir: /application
            volumes:
                - ./webserver/html/:/application
            ports:
                - "80:80"
            networks:
                default:
                    aliases:
                        - test1.localhost
                        - test2.localhost
    
        other:
            image: php:7.2.7
            container_name: other
            tty: true
            networks:
                - default
    
    networks:
        default:
    

    使用 Docker 网络链接

    第二种解决方案使用docker network links。这反过来起作用。对于每个依赖容器(其他),您可以指定应转发到网络服务器的其他链接/名称。

    docker-compose-yml: 版本:“3.1”

    services:
    
        webserver:
            build: webserver
            container_name: web
            working_dir: /application
            volumes:
                - ./webserver/html/:/application
            ports:
                - "80:80"
    
        other:
            image: php:7.2.7
            container_name: other
            tty: true
            links:
                - "webserver:test1.localhost"
                - "webserver:test2.localhost"
    

    【讨论】:

      【解决方案2】:

      如果我理解正确的话。 您可以通过容器之间的容器名称来解析。

      【讨论】:

      • 感谢您的回复。问题是我想将多个名称解析为同一个容器。在示例中:test1.localhosttest2.localhost 到容器 web
      • 尝试将您的主机文件更改为:127.0.0.1 test1.localhost test2.localhost localhost
      • AFAIK 你不能以这种方式“转发”名称。即使我能知道test2.localhost localhost 是什么意思?
      猜你喜欢
      • 2017-02-22
      • 2016-05-12
      • 2018-05-14
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      相关资源
      最近更新 更多