Docker - 在主机和容器上为不同的网站运行Apache [英] Docker - Run Apache on host and container for different websites

查看:121
本文介绍了Docker - 在主机和容器上为不同的网站运行Apache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Docker,以便能够运行需要PHP 5.3的旧应用程序,同时在主机服务器上仍然有其他网站,在主机Apache上运行。

I want to use Docker in order to be able to run an old application that requires PHP 5.3, while still having my other websites on my host server, running on the host Apache.

所以我有一个在主机上运行的siteA.com,siteB.com,siteC.com,使用主机Apache / PHP / MySQL服务器,我有一个安装在Docker容器中的siteZ.com使用容器的Apache / PHP,但是使用主机MySQL服务器。

So I have siteA.com, siteB.com, siteC.com running on the host, using the host Apache / PHP / MySQL server, and I have siteZ.com that is installed in a Docker container, which should be using the container's Apache / PHP but the host MySQL server.

以下是我想获取的体系结构:

Here's a representation of the architecture I'd like to obtain :

我的问题是,我似乎无法在容器中运行Apache,因为80号端口已经在主机上使用。

My issue is that it seems like I can't run Apache in the container, since the port 80 is already in use on the host.

我的目标是人们可以访问siteA.com,siteB.com,siteC.com和siteZ。 com,而不必指定不同的p ort for any of these sites。

My goal would be that the people could access siteA.com, siteB.com, siteC.com and siteZ.com, without having to specify a different port for any of those websites.

我设法通过使用端口8080来运行siteZ.com,但显然不是一个选项。

I managed to get siteZ.com running by using port 8080, but it's obviously not an option.

谢谢

PS:请注意,我完全是Docker的新人。

PS : Please note that I'm completly new to Docker.

编辑:您可以在这里找到我的工作解决方案。感谢VonC的向我显示方式:)

Edit : You can find my working solution here. Thanks to VonC for showing me the way to go :)

推荐答案

感谢 VonC的答案,我设法使其工作,但我稍微改变了我的架构,导致3个容器,而不是只有1个。

Thanks to VonC's answer I managed to get it working but I slightly changed my architecture, resulting in 3 containers instead of only 1.

我有一个容器用于每个Apache / PHP版本,一个容器使用Nginx作为反向代理。我想你可以很容易地适应这个安装Nginx在主机上,并更改它的配置,以匹配我在我的问题中描述的架构。

I have one container for each Apache / PHP version, and one container with Nginx as reverse proxy. I think you can easily adapt this to install Nginx on the host and change it's configuration to match the architecture I described in my question.

请注意,因为我是新来的Docker和一个关于Linux系统管理的noob,他们可能有一些错误和事情在以下脚本中没有任何意义,但它对我来说是有效的。可以自由地改进它:)

Note that as I'm new to Docker, and a noob regarding Linux system administration, their's probably some mistakes and things that doesn't make any sense in the following scripts, but it's working for me. Feel free to improve it :)

Dockerfile:

The Dockerfile :

FROM debian:jessie

MAINTAINER AntoineB version: 0.1

RUN apt-get update && \
    apt-get install -y --force-yes \
            nginx \
        nano

EXPOSE 80
EXPOSE 443

ADD ./proxy.conf /etc/nginx/conf.d/proxy.conf

CMD ["nginx"]

这里是引用的 proxy.conf 文件:

proxy_redirect          off;
proxy_set_header        Host            $host;
proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
client_header_buffer_size 64k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffer_size   16k;
proxy_buffers       32   16k;
proxy_busy_buffers_size 64k;

我使用以下bash脚本运行它:

And I run it using the following bash script :

docker run -ti -d -p 80:80 -v /home/antoineb/Docker/images/nginxproxy/virtualhosts:/etc/nginx/sites-enabled --name nginxproxy nginxproxy /bin/bash

我有一个 / home / antoineb / Docker / images / nginxproxy / virtualhosts 文件夹在我的主机上包含以下默认文件:

I have a /home/antoineb/Docker/images/nginxproxy/virtualhosts folder on my host that contains the following default file :

server {
       listen 80;

       server_name  siteZ.com;
       location / {
            proxy_pass http://apache22php53:80;
       }
}

server {
       listen 80;

       server_name  siteA.com;
       location / {
            proxy_pass http://apache24php56:80;
       }
}
server {
       listen 80;

       server_name  siteB.com;
       location / {
            proxy_pass http://apache24php56:80;
       }
}






Apache 2.2 + PHP 5.3映像



Dockerfile:


Apache 2.2 + PHP 5.3 image

Dockerfile :

FROM debian:wheezy

MAINTAINER AntoineB version: 0.1

RUN apt-get update

RUN echo "deb http://packages.dotdeb.org squeeze all" > /etc/apt/sources.list.d/dotdeb_squeeze.list
RUN echo "deb-src http://packages.dotdeb.org squeeze all" >> /etc/apt/sources.list.d/dotdeb_squeeze.list
RUN echo "deb http://ftp.debian.org/debian/ squeeze main contrib non-free" >> /etc/apt/sources.list.d/dotdeb_squeeze.list

RUN echo "Package: *php*" > /etc/apt/preferences.d/php53.pref
RUN echo "Pin: release o=packages.dotdeb.org,n=squeeze" >> /etc/apt/preferences.d/php53.pref
RUN echo "Pin-Priority: 989" >> /etc/apt/preferences.d/php53.pref

RUN apt-get update && \
    apt-get install -y --force-yes \
            apache2 \
        php5 \
        php5-curl \
        php5-gd \
        php5-mysql \
        nano

RUN a2enmod \
            php5 \
        rewrite

ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP    www-data
ENV APACHE_LOG_DIR  /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid

EXPOSE 80
EXPOSE 443

CMD /usr/sbin/apache2ctl -D FOREGROUND

我使用以下脚本启动它:

I'm launching it using the following script :

docker run -ti -d -p 2253:80 -v /home:/home -v /home/antoineb/Docker/images/apache22php53/virtualhosts:/etc/apache2/sites-enabled --name apache22php53 apache22php53 /bin/bash

我的网站存储在/home/website.com/www中,我的apache虚拟主机被存储o在 / home / antoineb / Docker / images / apache22php53 / virtualhosts 中的主机。

My websites are stored in /home/website.com/www, and my apache virtualhosts are stored on the host in /home/antoineb/Docker/images/apache22php53/virtualhosts.

Dockerfile:

Dockerfile :

FROM debian:jessie

MAINTAINER AntoineB version: 0.1

RUN apt-get update && \
    apt-get install -y --force-yes \
            apache2 \
        php5 \
        php5-curl \
        php5-gd \
        php5-mysql \
        nano

RUN a2enmod \
            php5 \
        rewrite

ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP    www-data
ENV APACHE_LOG_DIR  /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid

EXPOSE 80
EXPOSE 443

CMD /usr/sbin/apache2ctl -D FOREGROUND

我的运行脚本:

docker run -ti -d -p 2456:80 -v /home:/home -v /home/antoineb/Docker/images/apache24php56/virtualhosts:/etc/apache2/sites-enabled --name apache24php56 apache24php56 /bin/bash

再次,我的网站存储在/home/website.com/www中,我的apache虚拟主机存储在主机上的 / home / antoineb / Docker / images / apache24php56 / virtualhosts

Again, my websites are stored in /home/website.com/www, and my apache virtualhosts are stored on the host in /home/antoineb/Docker/images/apache24php56/virtualhosts.

这篇关于Docker - 在主机和容器上为不同的网站运行Apache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆