Stephane Stephane - 3 months ago 8
Apache Configuration Question

Accessing my Dockerized website by its hostname

On my localhost I have a Docker container serving a PHP website which I can see just fine in my browser

http://localhost/learnintouch/www/learnintouch.com/
but I would prefer to access it this way
dev.learnintouch.com/
if that is possible.

I tried running my container with
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
but that was not enough.

The container Apache server uses the following virtual host:

<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>


My host's
/etc/hosts
contains:

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


I run a Docker with the version:

$ 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


The Docker info:

$ 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

Answer

The additional hosts you pass in with --add-host option will modify your container's /etc/host, not the file on your host itself. For testing, you need to modify the /etc/host on the host running your browser. And for production, you'd need to update DNS to point to the docker host (or a load balancer that includes the docker host as its target).