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
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.
I recommend you using Docker Compose, but you also can use Volume in both
docker run
command anddocker-compose
.docker-compose
First of all let’s see
docker-compose
. Create a file calleddocker-compose.yml
with the following contents:The two keywords
hostname
andcontainer_name
are optional and you can change their values or even remove these two lines.Regarding
volumes
, copy yourapache2.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:For each site you can create another file and add it to
volumes
.Now after everything is done, run this command:
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):Update 1
Edit your
Dockerfile
:apache2.sh
: