问题
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.)
回答1:
Turns out this was an Apache configuration issue.
I needed to explicitly enable domain-named virtualhosts, like so:
NameVirtualHost *:80
This answer helped.
Docker had nothing to do with the matter.
回答2:
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
yields eboraas/apache
as most starred image, so I'll use that one for the example.
In a test directory, use your sample:
File: 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>
Then create the vhost websites & the logs directory.
mkdir -p logs; for i in default tests dummy; do mkdir -p $i; echo "hello $i" > $i/index.html; done
Finally, run docker.
docker run -it -v $(pwd):/var/www/html -v $(pwd)/httpd.conf:/etc/apache2/sites-available/000-default.conf -v $(pwd)/logs:/etc/apache2/logs -p 9090:80 --rm --name apache_c eboraas/apache
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:
$> curl dummy.dev:9090
hello dummy
$> curl tests.dev:9090
hello tests
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
回答3:
Turn on NameVirtualHost in your httpd config:
File : /etc/httpd/conf/httpd.conf
NameVirtualHost *:80
NameVirtualHost *:443
来源:https://stackoverflow.com/questions/43786208/multiple-vhosts-on-one-and-the-same-docker-container