如何托管两个Docker容器在同一台服务器上暴露端口80 [英] How to host two Docker containers exposing port 80 on the same server

查看:174
本文介绍了如何托管两个Docker容器在同一台服务器上暴露端口80的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个客户端需要连接到80端口的网站。每个网站都运行在自己的容器中。我想在同一个Docker主机上运行两个容器。

I have 2 websites that clients need to connect to on port 80. Each website runs in its own container. I want to run both containers on the same Docker host.

我知道端口80只能在主机上公开一次。存在哪些最小开销/管理的方案,这将允许我简单地在同一主机上运行两个容器(同时仍然允许客户端到达端口80上的每个容器)?

I understand that port 80 can only be exposed on the Host one time. What solutions exist that have minimal overhead/administration that will allow me to simply run both containers on the same host (while still allowing clients to reach each container on port 80) ?

网站1和网站2应该出现在客户端80端口的Web浏览器,并具有友好的URL(即:www.web1.com,www.web2.com)

Both website 1 and website 2 should appear to client the web browser on port 80 and have friendly URL's (ie: www.web1.com, www.web2.com)

推荐答案

使用 nginx反向代理


  1. 安装 Nginx 在主机上。在Debian / Ubuntu上: apt-get install nginx 。注意:我假设你没有apache或主机上的其他一些Web服务器已经...

  2. 对于每个站点,在目录<$ c $中写一个nginx站点文件c> / etc / nginx / sites-available
    将该站点重定向到将在其他预先安排的端口上运行的docker容器的http(例如2001,2002,...) 。每个站点都拥有自己的文件,如下所示,但每个站点都有不同的预先安装的端口。外部用户将访问相同IP地址的端口80,但具有不同的网站名称,从这些名称,nginx将不可见地处理必要的内部连接。

  3. 链接网站文件,以便它们显示在目录 / etc / nginx / sites-enabled 中,然后重新启动 nginx的。之后,您可以删除其中一个链接并重新启动nginx,如果您需要临时禁用访问站点。

  4. 通过添加 docker run来启动系统重新启动的容器命令到 /etc/rc.local ,并将预先安排的主机端口(localhost:2001)重定向到容器端口80,例如 docker run -d -p localhost:2001:80 imageA

  1. install Nginx on the host. On Debian/Ubuntu: apt-get install nginx. Note: I've assumed you don't have apache or some other web server on the host already...
  2. For each site, write an nginx site file in directory /etc/nginx/sites-available that redirects that site to a docker container's http that will run on some other prearranged port (for example, 2001, 2002, ...). Each site gets its own file, like the one below, but with different prearranged ports for each site. External users will access these at port 80 of the same IP address but with different web site names, and from these names nginx will handle the necessary internal connections invisibly.
  3. symlink the site files so they show up in directory /etc/nginx/sites-enabled and restart nginx. Later, you can remove one of these links and restart nginx if you need to temporarily disable access to a site.
  4. start the containers on system restart by adding docker run commands to /etc/rc.local, and redirecting the prearranged host port (localhost:2001) to container port 80 e.g. docker run -d -p localhost:2001:80 imageA

如果容器已关闭,您将从nginx中获取网关错误。这可以自定义以显示自定义HTML页面。为了更健壮,可能会更好地管理 supervisord 或重新生成死进程的其他进程管理器中的容器。

If a container is down you will get a gateway error from nginx. This could be customized to show a custom HTML page. For more robustness, it might be better to manage the containers in supervisord or some other process manager that respawns dead processes.

这是一个重定向到2001年的nginx站点文件示例:

Here is an example nginx site file for a redirection to port 2001:

upstream dockerA { 
         server localhost:2001;
}

server {
       listen 192.168.1.8:80;  // REPLACE WITH HOST NUMERIC IP ADDRESS
       root /var/web/siteAstaticfiles;
       index index.html;
       server_name www.siteA.com;

       location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://dockerA;
        }               
}

在这个例子中,虽然是HTML的本地磁盘目录文件是为站点A设置的,它不被使用。相反,所有请求都将发送到上游。我还没有测试是否可以安全地省略 index 行。

In this example, although a local disk directory for HTML files is set up for site A, it is not used. Instead all requests are sent to the upstream. I haven't tested whether the root and index lines can be safely omitted.

这篇关于如何托管两个Docker容器在同一台服务器上暴露端口80的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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