简体   繁体   English

docker 容器上的多个站点

[英]multiple sites on docker container

I am completely to docker and php and apache, I am trying to run my sample website on apache on a docker contain. I am completely to docker and php and apache, I am trying to run my sample website on apache on a docker contain. it works fine.它工作正常。 my dockerfile is that like this我的 dockerfile 是这样的

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

so what if I want to have another website on that apache,for example "website1".那么,如果我想在 apache 上拥有另一个网站,例如“website1”,该怎么办。 in xampp I could change the httpd.conf file and add virtual host.在 xampp 中,我可以更改 httpd.conf 文件并添加虚拟主机。 but what should I do in docker container?但是我应该在 docker 容器中做什么?

Update更新

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

I update the files as the following, This is docker-compose.yml我将文件更新如下,这是 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这是site.conf

 <VirtualHost *:80>
 DocumentRoot "d:\Temp\Site1"
 ServerName site1Name
 <Directory "d:\Temp\Site1">
 </Directory>
 </VirtualHost>

This is the site2.conf这是site2.conf

 <VirtualHost *:80>
 DocumentRoot "d:\Temp\Site2"
 ServerName site2Name
 <Directory "d:\Temp\Site2">
 </Directory>
 </VirtualHost>

and I addded these two line to hosts file in windows=>system32=>driver=>etc我将这两行添加到 windows=>system32=>driver=>etc 的 hosts 文件中

  127.0.0.1 site1Name
  127.0.0.1 site2Name

but when I surf to site1Name or site2Name on browser, there is no success但是当我在浏览器上浏览到 site1Name 或 site2Name 时,没有成功

The idea of Docker is that every application (website in you case) has its own environment. Docker 的想法是每个应用程序(在您的情况下为网站)都有自己的环境。 You can then upgrade the PHP version for one application without affecting the others.然后,您可以为一个应用程序升级 PHP 版本,而不会影响其他应用程序。 So you should have a Dockerfile per website.所以你应该有一个 Dockerfile 每个网站。

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.有很多 Docker 图像可用,例如 Nginx 和 Traefik。 These can also take care of SSL certificates for instance.例如,这些还可以处理 SSL 证书。

I recommend you using Docker Compose , but you also can use Volume in both docker run command and docker-compose .我建议您使用Docker Compose ,但您也可以在docker run命令和docker-compose中使用Volume

docker-compose docker-compose

First of all let's see docker-compose .首先让我们看看docker-compose Create a file called docker-compose.yml with the following contents:创建一个名为docker-compose.yml的文件,其内容如下:

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.两个关键字hostnamecontainer_name是可选的,您可以更改它们的值,甚至删除这两行。

Regarding volumes , copy your apache2.conf to a path like ./apache and do your edits in this file.关于volumes ,将您的apache2.conf复制到./apache之类的路径并在此文件中进行编辑。

Then add another file called site1.conf .然后添加另一个名为site1.conf的文件。 Here is my conf file as an example:这里以我的 conf 文件为例:

<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 .对于每个站点,您可以创建另一个文件并将其添加到volumes中。

Now after everything is done, run this command:现在一切都完成后,运行这个命令:

docker-compose up -d

docker run command docker 运行指令

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命令,你可以这样做(下面./apache中的上述路径相同):

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更新 1

Edit your Dockerfile :编辑您的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 : apache2.sh

service apache2 start
a2ensite site1
a2ensite site2
service apache2 restart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM