skip to Main Content

I’ve been trying to build a multi site Apache docker but all in vain.
Here is my docker-compose.yml

version: '3.9'

services:
    app:
      container_name: Core
      build:
        context: ./conf/core
        args:
            UID: ${UID:-1000}
      env_file:
        - "./.env"
      ports:
        - "${HTTP_PORT:-80}:80"
        - "${HTTPS_PORT:-443}:443"
      volumes:
        - ./../application:/var/www/html
        - ./conf/core/php.ini:/usr/local/etc/php/conf.d/99-overrides.ini
        - ./conf/core/openssl.cnf:/etc/ssl/openssl.cnf
        - ./conf/ssl:/etc/apache2/ssl
        - ./logs/apache:/var/log/apache2
      networks:
        - abmmhasan-net

networks:
  abmmhasan-net:
    driver: bridge

The docker file

FROM php:8.2-apache

# Configureing Apache
#ENV APACHE_RUN_USER=#$UID
#ENV APACHE_RUN_GROUP=#$UID
ENV APACHE_LOG_DIR=/var/log/apache2
RUN rm -rf /etc/apache2/sites-enabled/000-default.conf /etc/apache2/sites-enabled/default-ssl.conf
RUN rm -rf /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/default-ssl.conf
ADD ./sites/*.conf /etc/apache2/sites-available
RUN echo "ServerName site1.local" >> /etc/apache2/apache2.conf
RUN echo "ServerName site2.local" >> /etc/apache2/apache2.conf
RUN a2ensite *
RUN a2enmod rewrite ssl socache_shmcb vhost_alias

# Required installations/updates
RUN apt update && apt upgrade -y

RUN apt install sudo -y && apt clean && 
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /var/cache/apt/archives/*

# Add synced system user
ARG UID
RUN useradd -G www-data,root -u $UID -d /home/devuser devuser && 
    mkdir -p /home/devuser/.composer && 
    chown -R devuser:devuser /home/devuser
USER devuser

WORKDIR /var/www/html

And the ./sites/site1.local.conf (same way the site2 with site2 directory) file

<VirtualHost site1.local:80>
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/site1
        DirectoryIndex index.php
        ServerName site1.local

        <Directory /var/www/html/site1>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/site1-error.log
        CustomLog ${APACHE_LOG_DIR}/site1-access.log common
</VirtualHost>

I’ve also added following in my hosts file

192.168.50.4 site1.local
192.168.50.4 site2.local

Tried to have single docker container with multiple site hosted in single place but instead of loading the sites from defined directory it loads from /var/www/html/ only!!

2

Answers


  1. Chosen as BEST ANSWER

    After several try I was able to resolve it. What I did was,

    • Updated to <VirtualHost *:80>. ServerName directive used instead to separate the sites.
    • Deleted the 000-default.conf & default-ssl.conf
    • No need to add ServerName in apache2.conf

    Updated code now available in https://github.com/abmmhasan/localhost


  2. in your hosts file it wont be 127.0.0.1 site1.local it would be something else, check your docker containers local ip address, and put that there docker_ip_here site1.local

    use this command to get its ip address, replace container_name_or_id with your docket name/id

    docker inspect 
      -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
    

    if on windows use this command

    docker inspect 
      -f "{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}" container_name_or_id
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search