使用主机名的码头工人容器访问Apache服务器 [英] Accessing an Apache server in a Docker container using a hostname
问题描述
我在使用Apache 2.4.6和CentOS泊坞窗容器中运行的Web应用程序;这个应用程序反过来会谈其他几个Docker容器如通过泊坞窗链接后端服务和MySQL。我可以连接到应用程序容器在本地使用boot2docker VM,即 http://192.168.59.103:80/
的IP地址。不过,我想是一个子域,即 http://test.mydomain.com
能够访问在本地通过一个域名。原因是该应用程序使用 $ _ SERVER ['HTTP_HOST']
变量进行身份验证请求,并反过来,后端服务使用 HTTP_ORIGIN
头验证从Web应用程序请求从一个有效的子域的到来。因此,在浏览器中测试应用程序时,我需要它来正确地发送这些HTTP标头。我只需要为当地的环境,不部署(专门用于运行针对应用程序的持续集成测试的目的)进行配置。我真的不知道该怎么做来配置我的本地系统(OS X小牛)和CI环境(Ubuntu的12.04),以使域名进行访问。
这是我目前的配置:
的httpd.conf
<虚拟主机*:80>
DocumentRoot的/无功/网络/网站
ServerAlias 172 *
服务器名mydomain.com
UseCanonicalName在 <目录/无功/网络/网站>
有FollowSymLinks
所有的AllowOverride
为了允许,拒绝
所有允许
< /目录>
< /虚拟主机>
这是命令我运行启动Web应用程序容器:
泊坞窗运行-d -p 80:80 -h mydomain.com --name网络--link的mysql:mysql的--link的memcached:--link的memcached服务:服务--link API:基API MYORG / mywebapp:制定
在此设置,我可以成功地在 http://192.168.59.103
导航到应用程序,但因为 HTTP_HOST
是IP,应用程序不解析子域名并不会验证登录。有没有一种办法可以让这么本地请求something.mydomain.com会重定向到泊坞窗容器?请问服务器名称mydomain.com
行有什么目的在Apache的配置,并在 -h mydomain.com
参数在泊坞窗运行
命令?
您可以定义一个容器的主机名和域名,并使用shell脚本来更新/ etc / hosts中为您服务。
在这里您可以找到如何某些内脏: HTTP:// WWW。 intrapesite.ro/access-docker-application-by-hostname 。
有可能创建一个泊坞窗的应用程序,将听取泊坞窗API(启动和停止)和更新共享/ etc / hosts中的主机和客户之间的文件(如一个优雅的,自动化的解决方案)。在红宝石API看一看,例如: https://github.com/swipely/docker-api #events 的,也许在与bjeanes /鬼主机管理器(github上)。结合
更新:
另一种方法是增加一个桥接网络主机上,给人一种静态的IP地址,如 192.168.33.100
。
在使用其接受的IP地址以及泊坞窗端口转发。在泊坞窗撰写YAML语法是:
端口:
     - 192.168.33.100:80:80
块引用>文件
/ etc / hosts中
需要与指向DOMAIN_URL的IP进行更新。这种方式可以添加为具有不同的静态IP,只有唯一的在/ etc /每泊坞窗项目hosts文件修改许多桥梁。最后的解决方案看起来最优雅的所有。
I have a web application running in a Docker container using Apache 2.4.6 and CentOS; this application in turn talks to several other Docker containers such as backend services and mysql through Docker links. I can connect to the app container locally using the ip of the boot2docker vm, i.e.
http://192.168.59.103:80/
. However, I would like to be able access it locally through a domain name with a subdomain, i.e.http://test.mydomain.com
. The reason is that the app uses the$_SERVER[ 'HTTP_HOST' ]
variable to authenticate requests, and in turn, the backend services use theHTTP_ORIGIN
header to authenticate that requests from the web app are coming from a valid subdomain. As such, when testing the app in a browser, I need it to send those HTTP headers correctly. I only need to configure this for a local environment, not deployment (specifically for the purposes of running continuous integration tests against the app). I don't really know what to do to configure my local system (OS X Mavericks) and CI environment (Ubuntu 12.04) to make the domain name accessible.This is my current configuration:
httpd.conf
<VirtualHost *:80> DocumentRoot /var/www/site ServerAlias 172.* ServerName mydomain.com UseCanonicalName On <Directory "/var/www/site"> Options FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>
This is the command I run to start the web app container:
docker run -d -p 80:80 -h mydomain.com --name web --link mysql:mysql --link memcached:memcached --link service:service --link api:base-api myorg/mywebapp:develop
Under this setup, I can navigate to the app successfully at
http://192.168.59.103
but because theHTTP_HOST
is the IP, the app does not parse a subdomain and will not authenticate logins. Is there a way I can make it so local requests to something.mydomain.com will redirect to the docker container? What purpose does theServerName mydomain.com
line have in the apache config, and the-h mydomain.com
parameter in thedocker run
command?解决方案You can define the hostname and domainname of a container and use a shell script to update the /etc/hosts for you.
Here you can find some insides on how to: http://www.intrapesite.ro/access-docker-application-by-hostname.
It is possible to create a docker application that will listen to docker api (start and stop) and update a shared /etc/hosts file between host and guest (as an elegant, automated solution). Take a look, for example at ruby api: https://github.com/swipely/docker-api#events, maybe in conjunction with bjeanes/ghost hosts manager (github).
UPDATED:
Another way is to add a bridge network on the host, giving a static ip address such as
192.168.33.100
.After that use the docker port forwarding which accept IP address as well. In docker compose yaml the syntax is:
ports:
- "192.168.33.100:80:80"The file
/etc/hosts
needs to be updated with the IP pointing to domain_url. This way can be added as many bridges having different static IP with only only modification of the /etc/hosts file per docker project.The last solution looks the most elegant from all.
这篇关于使用主机名的码头工人容器访问Apache服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!