Running virtual hosts in apache docker container

Here is a solution -- Step by Step:

Step 1:

Add/Update extra_hosts, hostname, domainname and in docker-compose.yml and in my case I'm running it over PORT 80.

This is how my php service looks like in docker-compose.yml file.

php:
   build: .
   image: php:7.2-apache
   working_dir: /var/www/html
   volumes:
     - ./:/var/www/html
     - ./php.ini:/usr/local/etc/php/php.ini
   extra_hosts:
     - "lara.local:127.0.0.1"
   hostname: lara.local
   domainname: local 
   ports:
     - 80:80  
   environment:
     - "DB_PORT=3306"
     - "DB_HOST=database"

Step 2:
Update your hosts file with following line:

127.0.0.1 lara.local

Step 3: Test our hostname by running this command

docker exec -it <your-php-container-name> hostname

If you see output lara.local then you are good to go!

Step 4: Rebuild app

docker-compose build

Step 5: Start the services and check the app is running at http://lara.local

docker-compose up -d

Note: If you are using a different port for example 8080 then it would be http://lara.local:8080

PS. If you want to change the DocumentRoot then ssh to your container and cd /etc/apache2/sites-available/000-default.conf and change DocumentRoot path as per your needs.


Thanks to @David Maze I found the problem I added the listen directives to the top of my apache configuration and changed the port numbers.

docker-compose.yml

version: '3.2'
services:
  php-apache:
    build:
      context: ./apache-php
    ports:
      - 80:80
      - 8060:8060

    volumes:
      - ./DocumentRoot:/var/www/html:z

Apache config

Listen 80
Listen 8060

<VirtualHost *:8060>
    DocumentRoot /var/www/html/api
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Directory structure

apache-php
   ¬ sqlite3_ext
   ¬ 000-default.conf (Apache config)
   ¬ Dockerfile
   ¬ php.ini
docker-compose.yml

Dockerfile

FROM php:7.2.1-apache
RUN docker-php-ext-install pdo pdo_mysql mysqli

# Enable apache rewrite
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf

#Install spatialite and create symlink for libproj.so.0
COPY sqlite3_ext /etc/sqlite3_ext
RUN apt-get update && apt-get -y install gdal-bin
RUN ln -s /usr/lib/x86_64-linux-gnu/libproj.so.12 /usr/lib/x86_64-linux-gnu/libproj.so.0

#Install gd library for images
RUN apt-get install libpng-dev libfreetype6-dev libjpeg62-turbo-dev -qy \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install gd

#Copy php ini
COPY php.ini /usr/local/etc/php/php.ini
RUN a2enmod rewrite