如何在 Docker 容器中提供自定义名称解析
How to provide custom name resolution in Docker containers
假设我有以下文件结构
.
├── 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 build
和 docker-compose up
设置和 运行 容器。当在主机或 web
容器上 运行ning 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.1
但 other
容器不是 运行 网络服务器。我希望将 other
上的名称旋转到 web
容器。
这显然是一个最小的例子。对于 Web 应用程序的开发设置,我需要这种行为,我想从容器中 运行 cronjobs 与 apache 主机 运行ning 所在的容器分开。 cronjobs 的脚本 运行 对容器中的不同主机执行 http 请求。
我怀疑解决方案以某种方式使用 docker 网络配置...
如果我没看错的话。
您可以在容器之间通过容器名称解析。
好的,在 Docker Slack 频道的帮助下,我找到了一个解决方案,准确地说,实际上是两个...
使用 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-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 build
和 docker-compose up
设置和 运行 容器。当在主机或 web
容器上 运行ning 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.1
但 other
容器不是 运行 网络服务器。我希望将 other
上的名称旋转到 web
容器。
这显然是一个最小的例子。对于 Web 应用程序的开发设置,我需要这种行为,我想从容器中 运行 cronjobs 与 apache 主机 运行ning 所在的容器分开。 cronjobs 的脚本 运行 对容器中的不同主机执行 http 请求。
我怀疑解决方案以某种方式使用 docker 网络配置...
如果我没看错的话。 您可以在容器之间通过容器名称解析。
好的,在 Docker Slack 频道的帮助下,我找到了一个解决方案,准确地说,实际上是两个...
使用 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"