skip to Main Content

I am completely new to docker, php and apache, I am trying to run my sample website on apache server in a docker container. It works fine. And my dockerfile is following:

From php:7.2-apache
Copy Site/ /var/www/html/
Run echo "ServerName localhost" >> /etc/apache2/apache2.conf
RUN docker-php-ext-install pdo pdo_mysql
Expose 80

How can I have another website on the same apache sever in the docker container, for example "website1", in xampp I could change the httpd.conf file and add virtual host. but what should I do in docker container?

Update

============================================

I update the files as the following, This is docker-compose.yml

 version: '3.9'
services:
    apache:
        build: .
        container_name: php_cont
        volumes:
            - './apache2.conf:/etc/apache2/apache2.conf'
            - './Site1/site1.conf:/etc/apache2/sites-available/site1.conf'
            - './Site2/site2.conf:/etc/apache2/sites-available/site2.conf'
        ports:
            - '80:80' 

This is the site.conf

 <VirtualHost *:80>
 DocumentRoot "d:TempSite1"
 ServerName site1Name
 <Directory "d:TempSite1">
 </Directory>
 </VirtualHost>

This is the site2.conf

 <VirtualHost *:80>
 DocumentRoot "d:TempSite2"
 ServerName site2Name
 <Directory "d:TempSite2">
 </Directory>
 </VirtualHost>

and I addded these two line to hosts file in windows=>system32=>driver=>etc

  127.0.0.1 site1Name
  127.0.0.1 site2Name

but when I surf to site1Name or site2Name on the browser, there is no success!

2

Answers


  1. The idea of Docker is that every application (website in you case) has its own environment. You can then upgrade the PHP version for one application without affecting the others. So you should have a Dockerfile per website.

    To answer your question “how to direct visitors to the correct container?”: please take a look into Reverse Proxies. There are plenty of Docker images available like Nginx and Traefik. These can also take care of SSL certificates for instance.

    Login or Signup to reply.
  2. I recommend you using Docker Compose, but you also can use Volume in both docker run command and docker-compose.

    docker-compose

    First of all let’s see docker-compose. Create a file called docker-compose.yml with the following contents:

    version: '3.9'
    
    services:
        apache:
            build: .
            hostname: OPTIONAL
            container_name: OPTIONAL
            volumes:
                - './apache/apache2.conf:/etc/apache2/apache2.conf'
                - './apache/site1.conf:/etc/apache2/sites-available/site1.conf'
            ports:
                - '80:80'
    

    The two keywords hostname and container_name are optional and you can change their values or even remove these two lines.

    Regarding volumes, copy your apache2.conf to a path like ./apache and do your edits in this file.

    Then add another file called site1.conf. Here is my conf file as an example:

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

    For each site you can create another file and add it to volumes.

    Now after everything is done, run this command:

    docker-compose up -d
    

    docker run command

    If you want to use docker run command, you can do like this (the above paths in ./apache in the below are the same):

    docker run IMAGE_NAME -v './apache/apache2.conf:/etc/apache2/apache2.conf' -v './apache/site1.conf:/etc/apache2/sites-available/site1.conf' -dp80:80
    

    Update 1

    Edit your Dockerfile:

    From php:7.2-apache
    Copy Site/ /var/www/html/
    Run echo "ServerName localhost" >> /etc/apache2/apache2.conf
    COPY site1.conf /etc/apache2/apache2.conf
    COPY site2.conf /etc/apache2/apache2.conf
    COPY apache2.sh /root/
    RUN bash /root/apache2.sh
    RUN rm /root/apache2.sh
    RUN docker-php-ext-install pdo pdo_mysql
    Expose 80
    

    apache2.sh:

    service apache2 start
    a2ensite site1
    a2ensite site2
    service apache2 restart
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search