jwk*_*knz 1 subdomain networking docker microservices
我正在尝试设置可以通过一个主容器访问的多个 docker 容器。
例如:
http://localhost:80是主容器
http://localhost:80/site1是一个单独的容器
http://localhost:80/site2还是一个单独的容器
我知道该--link标志已被弃用,而新的处理方式是使用该--network标志。
当我使用--link(用于测试)时,我会在主机文件中看到我链接到的容器的条目。这就是我被困的地方。
所以我想使用 docker --networking 选项设置上述场景。
用例:/Site1可能是网站的管理区域或成员,但我希望将它们放在单独的容器中,以便我可以更轻松地维护它们。
容器是基于 apache2 的,但如果可能的话,希望避免编辑任何配置文件(但如果需要,我可以)
我该怎么办?
据我所知,docker 无法将 HTTP 请求路由到一个或另一个容器。您只能将主机的端口映射到一个容器。
您需要运行一个反向代理(例如 nginx)作为您的主容器,然后将请求路由到适当的容器。
这是一个如何设置它的示例
FROM node:6.11
WORKDIR /site1
COPY site1.js .
CMD node site1.js
EXPOSE 80
Run Code Online (Sandbox Code Playgroud)
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World 1\n');
}).listen(80);
Run Code Online (Sandbox Code Playgroud)
FROM node:6.11
WORKDIR /site2
COPY site2.js .
CMD node site2.js
EXPOSE 80
Run Code Online (Sandbox Code Playgroud)
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World 2\n');
}).listen(80);
Run Code Online (Sandbox Code Playgroud)
server {
listen 80;
# ~* makes the /site1 case insensitive
location ~* /site1 {
# Nginx can access the container by the service name
# provided in the docker-compose.yml file.
proxy_pass http://node-site1;
}
location ~* /site2 {
proxy_pass http://node-site2;
}
# Anything that didn't match the patterns above goes here
location / {
# proxy_pass http://some other container
return 500;
}
}
Run Code Online (Sandbox Code Playgroud)
version: "3"
services:
# reverse proxy
node-proxy:
image: nginx
restart : always
# maps config file into the proxy container
volumes:
- ./node-proxy/default.conf:/etc/nginx/conf.d/default.conf
ports:
- 80:80
links:
- node-site1
- node-site2
# first site
node-site1:
build: ./site1
restart: always
# second site
node-site2:
build: ./site2
restart: always
Run Code Online (Sandbox Code Playgroud)
要启动反向代理,两个站点都在此文件夹的根目录中输入docker-compose up -d并检查docker ps -a所有 docker 容器是否正在运行。
之后,您可以使用http://localhost/site1和http://localhost/site2访问这两个站点
文件夹 site1 和 site2 包含一个使用 nodejs 构建的小型网络服务器。他们都在监听80端口。“node-proxy”包含了告诉nginx什么时候返回哪个站点的配置文件。
这里有一些链接
| 归档时间: |
|
| 查看次数: |
2177 次 |
| 最近记录: |