[英]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反向代理 :
apt-get install nginx
. apt-get install nginx
。 Note: I've assumed you don't have apache or some other web server on the host already... /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. /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. 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. 我没有测试是否可以安全地省略
root
和index
行。
使用docker,docker-compose和nginx-proxy在单个主机上托管多个网站的更合适的方法: https : //blog.florianlopes.io/host-multiple-websites-on-single-host-docker/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.