我有两个链接的Docker容器sorted_cheap[i] = pairs[i].second
和api
。 frontend
是frontend
容器中的ReactJS应用。这是Dockerfile:
nginx
和运行命令:
FROM nginx:alpine
COPY ./build /usr/share/nginx/html/
当我docker run --name frontend --link api:api -p 80:80 frontend_img
sh
内并跑步时:
frontend
工作正常,我能够获取用户。但是,当我的应用尝试获取数据时,我在Chrome控制台中收到$ wget http://api:8080/api/users
错误。
这是我的JS代码:
ERR_NAME_NOT_RESOLVED
我想念什么?我是否需要在NGINX中配置某些内容以便能够从fetchUsers() {
return axios.get('http://api:8080/api/users')
.then(response => response.data)
.then(users => this.setState({ users }));
}
或其他内容中获取数据?
答案 0 :(得分:0)
解决了!感谢这个公认的答案docker nginx ERR_NAME_NOT_RESOLVED
使用反向代理连接到容器时,将使用所有URL 应用程序需要指向该反向代理,而不是 应用。通常,您可以通过在URL中提供路径而不使用 主机名。
首先,我创建了一个default.conf
nginx文件,添加了一个location
语句以将调用重定向到我的api容器:
location /api/ {
proxy_pass http://api:8080;
}
然后,在我的Web应用程序中,我更改了JS代码,因此针对/api/users
进行了请求,但没有主机名:
fetchUsers() {
return axios.get('/api/users')
.then(response => response.data)
.then(users => this.setState({ users }));
}
最后,在Dockerfile中,我将默认的default.conf
nginx文件替换为编辑后的文件:
FROM nginx:alpine
COPY default.conf /etc/nginx/conf.d
COPY ./build /usr/share/nginx/html/
仅此而已!