I'm trying to run two different domains on one and the same Docker container and port.
The Docker container runs CentOS. docker-compose.yml
looks like so:
web:
image: fab/centos
ports:
- "80:80"
volumes:
- ./src/httpd.conf:/etc/httpd/conf/httpd.conf
- ./src:/var/www/html
- ./src/hosts:/etc/hosts
environment:
- VIRTUAL_HOST=dummy.dev,tests.dev
I also declared both .dev domain names inside of /etc/hosts
on the host computer (OS X.)
It's been a while since I configured virtual hosts. My understanding was that I just needed to declare them and that Apache would automatically serve the proper files depending on the HTTP HOST being requested.
This is what I have, added at the end of httpd.conf
:
<VirtualHost *:80> # first host = default host
DocumentRoot /var/www/html/default
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/dummy
ServerName dummy.dev
ServerAdmin webmaster@dummy.dev
ErrorLog logs/dummy.dev-error_log
CustomLog logs/dummy.dev-access_log common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/tests
ServerName tests.dev
ServerAdmin webmaster@tests.dev
ErrorLog logs/tests.dev-error_log
CustomLog logs/tests.dev-access_log common
</VirtualHost>
However, in practice, visiting either dummy.dev or tests.dev actually serves /var/www/html/default
. This is as if Apache didn't realize which host is being called (though a dump of $_SERVER
in PHP does show the expected HTTP_HOST
value, i.e.: either 127.0.0.1, dummy.dev or tests.dev depending on which URL I visit.)
What did I miss?
It's unclear to me whether this is an Apache issue or a Docker one.
(Please note this is a different question from how to host multiple apps on the same domain with different port. In my case, I do want the virtual hosts to be all inside/on the same app/port/container.)
Turns out this was an Apache configuration issue.
I needed to explicitly enable domain-named virtualhosts, like so:
This answer helped.
Docker had nothing to do with the matter.
The
fab/centos
does not exist in public docker hub, so not sure why you are experiencing the issue.My recommendation would be to take a step back and try to make it work with a simple example.
docker search apache
yieldseboraas/apache
as most starred image, so I'll use that one for the example.In a test directory, use your sample:
File: httpd.conf
Then create the vhost websites & the logs directory.
Finally, run docker.
Note that I use basically the same volumes as you did in your docker-compose.yml, except that I use
site-available
instead of changing the httpd.conf.To test, make sure you have tests.dev and dummy.dev in your /etc/hosts pointing at the right Docker IP and test:
From this point, build up on top of this by first trying the docker apache image that you are using, then try with your custom /etc/hosts file, then put it back in a docker-compose file