Yii2 advanced app Docker - adding virtual hosts to apache

I am struggling to get virtual hosts for my advanced app working in Docker. It currently works perfectly in xampp.

I have copied over the following files from GitHub - yiisoft/yii2: Yii 2: The Fast, Secure and Professional PHP Framework. into the corresponding directories in my repo: -

/docker-compose.yml
/backend/Dockerfile
/frontend/Dockerfile

I altered /docker-compose.yml to add in the virtual hosts volume: -

version: "3.2"
services:
	frontend:
		build: frontend
		ports:
		 - 3000:80
		volumes:
		 # Re-use local composer cache via host-volume
		 - ~/.composer-docker/cache:/root/.composer/cache:delegated
		 # Mount source-code for development
		 - ./:/app
		 # virtual hosts
		 - ./apache_conf:/etc/apache2/sites-enabled/
	backend:
		build: backend
		ports:
		 - 4000:80
		volumes:
		 # Re-use local composer cache via host-volume
		 - ~/.composer-docker/cache:/root/.composer/cache:delegated
		 # Mount source-code for development
		 - ./:/app
		 # virtual hosts
		 - ./apache_conf:/etc/apache2/sites-enabled/

In my ./apache_conf/000-default.conf: -

<VirtualHost \*:80>
	ServerName frontend.myapp
	DocumentRoot /app/frontend/web
</VirtualHost>
<VirtualHost \*:80>
	ServerName backend.myapp
	DocumentRoot /app/backend/web
</VirtualHost>

In my Windows hosts file: -

127.0.0.1 frontend.myapp
127.0.0.1 backend.myapp

However, when I run the container, I get “site can’t be reached” when I try both the frontend and backend domains.

Can anyone help me with this? Thanks.

Hi,
please start docker compose in non detach mode and show the logs.
Then share both dockerfile.
M

1 Like

Thanks.

console output: -

Creating network "myapp_php_default" with the default driver
Building frontend
[+] Building 22.1s (6/6) FINISHED
 => [internal] load build definition from Dockerfile                                                                           0.1s 
 => => transferring dockerfile: 204B                                                                                           0.0s 
 => [internal] load .dockerignore                                                                                              0.1s 
 => => transferring context: 2B                                                                                                0.0s 
 => [internal] load metadata for docker.io/yiisoftware/yii2-php:7.4-apache                                                    12.8s 
 => [auth] yiisoftware/yii2-php:pull token for registry-1.docker.io                                                            0.0s 
 => [1/1] FROM docker.io/yiisoftware/yii2-php:7.4-apache@sha256:03f9223a0cbbad1e93c0d2d78b4f80835d89dcd12c6846b655c9998ac8aa1  8.8s 
 => => resolve docker.io/yiisoftware/yii2-php:7.4-apache@sha256:03f9223a0cbbad1e93c0d2d78b4f80835d89dcd12c6846b655c9998ac8aa1  0.0s 
 => => sha256:03f9223a0cbbad1e93c0d2d78b4f80835d89dcd12c6846b655c9998ac8aa192d 4.91kB / 4.91kB                                 0.0s 
 => => sha256:9db597b82154368587cd0cb64ac15a3c549a826898c5ee4b877f5dc15f2e6181 15.32kB / 15.32kB                               0.0s 
 => => sha256:06b26edf3d79db6343a1b1afc59198718851445e95858bd19689c153b040e102 232B / 232B                                     0.9s 
 => => sha256:5854427c3daea3eaf347a03638cdb684c9e42a2327bf936228e816fe09e0f4c3 24.10MB / 24.10MB                               5.1s 
 => => sha256:603c53e5a88983462c2d9f50a7afa108da7c716a20bf83bf3070cd9e8663c243 22.42kB / 22.42kB                               1.4s 
 => => sha256:77f25fd5b7900131a9b86b363a903ca50c6894bd17ac4180d7561f5b3985b34d 2.43kB / 2.43kB                                 1.3s 
 => => sha256:5ba8806d19a8a8052e761bc3ca139627311cc4efc4de80ae12b98f6ae2364235 524B / 524B                                     1.7s 
 => => extracting sha256:603c53e5a88983462c2d9f50a7afa108da7c716a20bf83bf3070cd9e8663c243                                      0.1s 
 => => sha256:dab793cd97e74f618279195d87d13860b9b35a088251c4a6e24cd0246cb2abb8 565.56kB / 565.56kB                             2.6s 
 => => sha256:d2914ec6496296d1a21e6345be3f58ef65e7b44666649fdfcdf9074d8aa6a83f 345B / 345B                                     2.1s 
 => => sha256:bc278c71330c2e2432b803daf5a0d43f98e2028347094c2e84ae402f973d164f 1.04kB / 1.04kB                                 2.4s 
 => => sha256:eb195ee88f32c743b0b1976045774fa652e5a1d51eac266ba60733d5ac56054f 92B / 92B                                       3.1s 
 => => extracting sha256:5854427c3daea3eaf347a03638cdb684c9e42a2327bf936228e816fe09e0f4c3                                      1.7s 
 => => extracting sha256:06b26edf3d79db6343a1b1afc59198718851445e95858bd19689c153b040e102                                      0.0s 
 => => extracting sha256:77f25fd5b7900131a9b86b363a903ca50c6894bd17ac4180d7561f5b3985b34d                                      0.0s 
 => => extracting sha256:5ba8806d19a8a8052e761bc3ca139627311cc4efc4de80ae12b98f6ae2364235                                      0.0s 
 => => extracting sha256:dab793cd97e74f618279195d87d13860b9b35a088251c4a6e24cd0246cb2abb8                                      0.1s 
 => => extracting sha256:d2914ec6496296d1a21e6345be3f58ef65e7b44666649fdfcdf9074d8aa6a83f                                      0.0s 
 => => extracting sha256:bc278c71330c2e2432b803daf5a0d43f98e2028347094c2e84ae402f973d164f                                      0.0s 
 => => extracting sha256:eb195ee88f32c743b0b1976045774fa652e5a1d51eac266ba60733d5ac56054f                                      0.0s 
 => exporting to image                                                                                                         0.2s 
 => => exporting layers                                                                                                        0.0s 
 => => writing image sha256:2fd34947d01aa417c9b5c43c370e3f86db0d8924d7ef47093f04f3b57a4e8aeb                                   0.0s 
 => => naming to docker.io/library/myapp_php_frontend                                                                    0.0s 

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
WARNING: Image for service frontend was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating myapp_php_frontend_1 ... done
Attaching to myapp_php_frontend_1
frontend_1  | usermod: no changes
frontend_1  | [Fri Nov 12 16:35:03.297568 2021] [core:error] [pid 1] (EAI 2)Name or service not known: AH00547: Could not resolve host name \\* -- ignoring!
frontend_1  | [Fri Nov 12 16:35:03.304444 2021] [core:error] [pid 1] (EAI 2)Name or service not known: AH00547: Could not resolve host name \\* -- ignoring!
frontend_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.2. Set the 'ServerName' directive globally to suppress this message
frontend_1  | [Fri Nov 12 16:35:03.339845 2021] [core:error] [pid 1] (EAI 2)Name or service not known: AH00547: Could not resolve host name \\* -- ignoring!
frontend_1  | [Fri Nov 12 16:35:03.344236 2021] [core:error] [pid 1] (EAI 2)Name or service not known: AH00547: Could not resolve host name \\* -- ignoring!
frontend_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.2. Set the 'ServerName' directive globally to suppress this message
frontend_1  | [Fri Nov 12 16:35:03.449274 2021] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.51 (Debian) configured -- resuming 
normal operations
frontend_1  | [Fri Nov 12 16:35:03.449314 2021] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

docker-compose.yml: -

version: "3.2"

services:
  frontend:
    build: frontend
    ports:
      - 3000:80
    volumes:
      # Re-use local composer cache via host-volume
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      # Mount source-code for development
      - ./:/app
      # virtual hosts
      - ./apache_conf:/etc/apache2/sites-enabled/

frontend Dockerfile: -

FROM yiisoftware/yii2-php:7.4-apache

# Change document root for Apache
RUN sed -i -e 's|/app/web|/app/frontend/web|g' /etc/apache2/sites-available/000-default.conf

VirtualHost directives were wrong…

try to replace

<VirtualHost \*:80>

with

<VirtualHost *:80>
1 Like

Tried that, no luck.

Please,
publish your sites-enabled conf.

m

1 Like

Thanks for your help.

In my container /etc/apache2/sites-enabled/000-default.conf

<VirtualHost *:80>
        ServerName frontend.myapp
        DocumentRoot /app/frontend/web
</VirtualHost>
<VirtualHost *:80>
        ServerName backend.myapp
        DocumentRoot /app/backend/web
</VirtualHost>