【发布时间】: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 build 和 docker-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