如何在 Docker 容器中提供自定义名称解析

Ace*_*7k3 3 dns virtualhost docker docker-compose

假设我有以下文件结构

\n\n
.                                  \n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 docker-compose.yml             \n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 webserver                      \n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Dockerfile                 \n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 html                       \n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 test1                  \n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.html         \n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test2                  \n    \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.html         \n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 vhosts.conf                \n\n4 directories, 5 files\n
Run Code Online (Sandbox Code Playgroud)\n\n

docker-compose.yml

\n\n
version: "3.1"\nservices:\n\n    webserver:\n        build: webserver\n        container_name: web\n        working_dir: /application\n        volumes:\n            - ./webserver/html/:/application\n        ports:\n            - "80:80"\n\n    other:\n        image: php:7.2.7\n        container_name: other\n        tty: true\n
Run Code Online (Sandbox Code Playgroud)\n\n

Dockerfile

\n\n
FROM php:7.2.7-apache\n\nCOPY vhosts.conf /etc/apache2/sites-enabled/000-default.conf\n
Run Code Online (Sandbox Code Playgroud)\n\n

vhosts.conf

\n\n
<VirtualHost *:80>\n    ServerName test1.localhost\n\n    DocumentRoot /application/test1\n    <Directory /application/test1>\n        Options Indexes FollowSymLinks MultiViews\n        AllowOverride All\n        Require all granted\n    </Directory>\n</VirtualHost>\n\n<VirtualHost *:80>\n    ServerName test2.localhost\n\n    DocumentRoot /application/test2\n    <Directory /application/test2>\n        Options Indexes FollowSymLinks MultiViews\n        AllowOverride All\n        Require all granted\n    </Directory>\n</VirtualHost>\n
Run Code Online (Sandbox Code Playgroud)\n\n

并且index.html文件只是带有一些内容来区分它们。

\n\n

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

\n\n
127.0.0.1   test1.localhost\n127.0.0.1   test2.localhost\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
curl: (7) Failed to connect to test1.localhost port 80: Connection refused\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
\n

如果缺少 --dns=IP_ADDRESS...、--dns-search=DOMAIN... 或 --dns-opt=OPTION... 选项,Docker 将使用主机的 /etc/resolv.conf机器(docker 守护进程运行的地方)。

\n
\n\n

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

\n\n

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

\n\n

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

\n

Ace*_*7k3 7

好吧,在 Docker Slack 频道的帮助下,我找到了一个解决方案,准确地说甚至是两个......

使用 Docker 网络别名

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

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:
Run Code Online (Sandbox Code Playgroud)

使用 Docker 网络链接

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

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"
Run Code Online (Sandbox Code Playgroud)