Multiple hosts on the same docker container

I am trying to run two different domains on the same container and Docker port.

The Docker container launches CentOS. docker-compose.ymllooks like that:

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 .dev domain names inside /etc/hostson the host computer (OS X.)

It's been a while since I set up virtual hosts. I realized that I just needed to declare them and that Apache would automatically serve the files depending on the requested HTTP HOST.

This is what I have added at the end 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>

dummy.dev tests.dev /var/www/html/default. , Apache , ( $_SERVER PHP HTTP_HOST, : 127.0.0.1, dummy.dev tests.dev, , URL, .)

?

, Apache Docker.

( , , . , / //.)

+7
3

, Apache.

virtualhosts, :

NameVirtualHost *:80

.

.

+3

fab/centos -, , .

- .

docker search apache eboraas/apache , .

:

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

- vhost .

mkdir -p logs; for i in default tests dummy; do mkdir -p $i; echo "hello $i" > $i/index.html; done

, 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

, , docker-compose.yml, , httpd.conf site-available.

, , test.dev dummy.dev /etc/hosts, IP- Docker :

$> curl dummy.dev:9090
hello dummy
$> curl tests.dev:9090
hello tests

, apache-, , /etc/hosts , docker

+2

Enable NameVirtualHost in the httpd configuration:

File: /etc/httpd/conf/httpd.conf

NameVirtualHost *:80 
NameVirtualHost *:443
0
source

Source: https://habr.com/ru/post/1676384/


All Articles