多个docker容器作为单个IP上的Web服务器 [英] Multiple docker containers as web server on a single IP

查看:174
本文介绍了多个docker容器作为单个IP上的Web服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一台机器上有多个码头容器。在每个容器上运行一个进程和一个Web服务器,为进程提供API。



我的问题是,如何在默认情况下从我的浏览器访问API港口是80?要能够访问docker容器内的Web服务器,请执行以下操作:

  sudo docker run -p 80:80 -t -i< yourname> /< imagename> 

这样我可以从我的电脑终端:

  curl http:// hostIP:80 / foobar 

但是如何处理这个多个容器和多个Web服务器?

解决方案

您可以暴露多个端口,例如

  docker run -p 8080:80 -t -i< yourname> /< imagename> 
docker run -p 8081:80 -t -i< yourname1> /< imagename1>

或放置代理( nginx ,apache,varnish等)在您的API容器前面。



更新



执行代理的最简单方法是将其链接到API容器,例如具有apache配置

  RewriteRule ^ api1 /(.*)$ http:// api1 / $ 1 [proxy] 
RewriteRule ^ api2 /(.*)$ http:// api2 / $ 1 [proxy]

你可以运行您的容器如下:

  docker run  -  name api1< yourname> /< imagename> 
docker run --name api2< yourname1> /< imagename1>
docker run --link api1:api1 --link api2:api2 -p 80:80< my_proxy_container>

这可能有点麻烦,但是如果您需要重新启动api容器,因为代理容器将必须也可以重新启动(链接在泊坞台是相当静态的)。如果这成为问题,您可以查看 fig 或自动更新代理配置的方法: http://jasonwilder.com/blog/2014/03/25 / automated-nginx-reverse-proxy-for-docker / 。后面的链接也显示了用nginx代理。



更新II:



在更现代化的Docker版本中,使用用户定义的网络,而不是显示的链接以克服不赞成的链接机制的一些不便。


I have multiple docker containers on a single machine. On each container is running a process and a web server that provides an API for the process.

My question is, how can I access the API from my browser when the default port is 80? To be able to access the web server inside docker container I do the following:

sudo docker run -p 80:80 -t -i <yourname>/<imagename>

This way I can do from my computers terminal:

curl http://hostIP:80/foobar

But how to handle this with multiple containers and multiple web servers?

解决方案

You can either expose multiple ports, e.g.

docker run -p 8080:80 -t -i <yourname>/<imagename>
docker run -p 8081:80 -t -i <yourname1>/<imagename1>

or put a proxy ( nginx, apache, varnish, etc.) in front of your API containers.

Update:

The easiest way to do a proxy would be to link it to the API containers, e.g. having apache config

RewriteRule ^api1/(.*)$ http://api1/$1 [proxy]
RewriteRule ^api2/(.*)$ http://api2/$1 [proxy]

you may run your containers like this:

docker run --name api1 <yourname>/<imagename>
docker run --name api2 <yourname1>/<imagename1>
docker run --link api1:api1 --link api2:api2 -p 80:80 <my_proxy_container>

This might be somewhat cumbersome though if you need to restart the api containers as the proxy container would have to be restarted either (links are fairly static at docker yet). If this becomes a problem, you might look at approaches like fig or autoupdated proxy configuration : http://jasonwilder.com/blog/2014/03/25/automated-nginx-reverse-proxy-for-docker/ . The later link also shows proxying with nginx.

Update II:

In a more modern versions of docker it is possible to use user defined network instead of the links shown above to overcome some of the inconveniences of the deprecated link mechanism.

这篇关于多个docker容器作为单个IP上的Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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