简体   繁体   English

如何托管在同一服务器上公开端口80的两个Docker容器

[英]How to host two Docker containers exposing port 80 on the same server

I have 2 websites that clients need to connect to on port 80. Each website runs in its own container. 我有两个客户端需要连接到端口80的网站。每个网站都在自己的容器中运行。 I want to run both containers on the same Docker host. 我想在同一个Docker主机上运行这两个容器。

I understand that port 80 can only be exposed on the Host one time. 我知道端口80只能在主机上暴露一次。 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) ? 有哪些解决方案具有最小的开销/管理,这将允许我在同一主机上简单地运行两个容器(同时仍然允许客户端到达端口80上的每个容器)?

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) 网站1和网站2都应该在端口80上显示客户端Web浏览器并且具有友好URL(即:www.web1.com,www.web2.com)

Use an nginx reverse proxy : 使用nginx反向代理

  1. install Nginx on the host. 在主机上安装Nginx On Debian/Ubuntu: apt-get install nginx . 在Debian / Ubuntu上: apt-get install nginx Note: I've assumed you don't have apache or some other web server on the host already... 注意:我假设您已经在主机上没有apache或其他Web服务器......
  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, ...). 对于每个站点,在目录/etc/nginx/sites-available中编写一个nginx站点文件,将该站点重定向到docker容器的http,该站点将在其他预先安排的端口上运行(例如,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. 外部用户将在相同IP地址但具有不同网站名称的端口80访问这些,并且从这些名称中,nginx将无形地处理必要的内部连接。
  3. symlink the site files so they show up in directory /etc/nginx/sites-enabled and restart nginx . 符号链接站点文件,以便它们显示在目录/etc/nginx/sites-enabled并重新启动nginx Later, you can remove one of these links and restart nginx if you need to temporarily disable access to a site. 稍后,如果您需要暂时禁用对站点的访问,则可以删除其中一个链接并重新启动nginx。
  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 eg docker run -d -p localhost:2001:80 imageA 通过向/etc/rc.local添加docker run命令,并将预先安排好的主机端口(localhost:2001)重定向到容器端口80,例如docker run -d -p localhost:2001:80 imageA在系统重启时启动容器

If a container is down you will get a gateway error from nginx. 如果容器关闭,您将从nginx收到网关错误。 This could be customized to show a custom HTML page. 这可以自定义以显示自定义HTML页面。 For more robustness, it might be better to manage the containers in supervisord or some other process manager that respawns dead processes. 为了更加健壮,最好在supervisord或其他重新生成死进程的进程管理器中管理容器。

Here is an example nginx site file for a redirection to port 2001: 以下是重定向到端口2001的示例nginx站点文件:

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;
        }               
}

In this example, although a local disk directory for HTML files is set up for site A, it is not used. 在此示例中,虽然为站点A设置了HTML文件的本地磁盘目录,但未使用它。 Instead all requests are sent to the upstream. 而是将所有请求发送到上游。 I haven't tested whether the root and index lines can be safely omitted. 我没有测试是否可以安全地省略rootindex行。

使用docker,docker-compose和nginx-proxy在单个主机上托管多个网站的更合适的方法: https//blog.florianlopes.io/host-multiple-websites-on-single-host-docker/

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

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