通过主机名访问我的 Dockerized 网站

Accessing my Dockerized website by its hostname

在我的本地主机上,我有一个 Docker 容器服务于一个 PHP 网站,我可以在我的浏览器中看到它 http://localhost/learnintouch/www/learnintouch.com/ 但我更愿意以这种方式访问​​它 dev.learnintouch.com/ 如果可能的话。

我尝试 运行用 docker run -d --name learnintouch -p 80:80 -p 443:443 --add-host "dev.learnintouch.com":127.0.0.1 -v /home/stephane/dev/php/learnintouch:/usr/bin/apache/htdocs/learnintouch stephaneeybert/learnintouch 连接我的容器,但这还不够。

容器 Apache 服务器使用以下虚拟主机:

<VirtualHost *:80>
  ServerName dev.learnintouch.com
  ServerAlias dev.learnintouch.com
  DocumentRoot /usr/bin/apache/htdocs/learnintouch/www/learnintouch.com
  CustomLog logs/learnintouch-access_log combined
  <Directory "/usr/bin/apache/htdocs/learnintouch/www/learnintouch.com">
    Include /usr/bin/apache/htdocs/learnintouch/engine/setup/url_rewrite.conf
  </Directory>
  AddDefaultCharset UTF-8
</VirtualHost>

我的主机的 /etc/hosts 包含:

127.0.0.1   localhost
127.0.1.1   stephane-ThinkPad-X301

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

我运行一个Docker版本:

$ docker version
Client:
 Version:      1.12.1
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   23cf638
 Built:        Thu Aug 18 05:33:38 2016
 OS/Arch:      linux/amd64

Server:
 Version:      1.12.1
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   23cf638
 Built:        Thu Aug 18 05:33:38 2016
 OS/Arch:      linux/amd64

Docker 信息:

$ docker info
Containers: 3
 Running: 1
 Paused: 0
 Stopped: 2
Images: 150
Server Version: 1.12.1
Storage Driver: aufs
 Root Dir: /home/stephane/programs/install/docker/aufs
 Backing Filesystem: extfs
 Dirs: 118
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: overlay bridge null host
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options: apparmor seccomp
Kernel Version: 4.4.0-36-generic
Operating System: Ubuntu 16.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 7.693 GiB
Name: stephane-ThinkPad-X301
ID: YMZD:RANJ:QIQ5:3KX3:GNQ6:UIGW:6NG3:YYPU:IABK:XVPI:KPUV:NPSC
Docker Root Dir: /home/stephane/programs/install/docker
Debug Mode (client): false
Debug Mode (server): false
Username: stephaneeybert
Registry: https://index.docker.io/v1/
WARNING: No swap limit support
Insecure Registries:
 127.0.0.0/8

您使用 --add-host 选项传入的额外主机将修改容器的 /etc/host,而不是主机本身上的文件。为了进行测试,您需要修改主机 运行 上的 /etc/host 您的浏览器。对于生产,您需要更新 DNS 以指向 docker 主机(或包含 docker 主机作为其目标的负载均衡器)。