【发布时间】:2018-11-23 01:15:17
【问题描述】:
这是我的 docker-compose 文件,它在同一主机上启动 NodeJS/PM2 容器和 React/Nginx 容器。
version: '3'
services:
nodejs:
image: nodejs_pm2:1.00
container_name: NODE_CONTAINER
ports:
- "8000:8000"
build:
context: ./nodejs
dockerfile: Dockerfile-nodejs
react:
image: react_nginx:1.00
container_name: REACT_CONTAINER
ports:
- "3000:3000"
build:
context: ./react-app
dockerfile: Dockerfile-react
depends_on:
- nodejs
如您所见,我的react 服务depends_on nodejs。这应该链接容器,对吗?
问题 1) 假设这是真的,向我的节点后端发出 HTTP 请求的正确方法是什么?
这是我尝试对我的 nginx 配置文件执行的操作:
upstream backend {
server nodejs:8000
}
server {
listen 3000;
location ~ ^/api/[0-9a-z]+/$ {
proxy_pass http://backend/;
include /etc/nginx/proxy_params;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
这不起作用。事实上,这完全阻止了前端出现。我正在尝试做的是来自我的反应应用程序的axios.get("http://backend/api/*")。我查看了几个 stackoverflow 帖子,但似乎没有一个对我有用。
问题 2) 后续问题,如果连接容器有什么意义
两个容器位于同一主机上,我不能像往常一样向localhost:port 发出请求吗?
【问题讨论】:
标签: node.js http docker nginx containers