gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:21:42 25 4
gpt4 key购买 nike

假设我有以下文件结构

.                                  
├── 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/ 找到了以下引述:

In the absence of the --dns=IP_ADDRESS..., --dns-search=DOMAIN..., or --dns-opt=OPTION... options, Docker uses the /etc/resolv.conf of the host machine (where the docker daemon runs).

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

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

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

最佳答案

好的,在 Docker Slack channel 的帮助下,我找到了一个解决方案,准确地说,实际上是两个...

使用 Docker 网络别名

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

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"

关于docker - 如何在 Docker 容器中提供自定义名称解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51401733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com