我试图使一个rails-api后端和反应前端网站与docker-compose。但是我不能使用带有我的主机名的axios向rails-api后端发送get请求(在docker-compose中说明)。我可以通过localhost访问后端,但当我将多容器应用程序部署到aws elastic beanstalk时,这将是一个问题(我假设)。
我正在尝试访问这样的值:
const value = await axios.get('http://website:3000/api/v1/examples');
这是在我的docker-compose.yml中:
version: '2'
services:
website:
depends_on:
- 'postgres'
- 'redis'
build: .
ports:
- '3000:3000'
volumes:
- '.:/app'
env_file:
- '.env'
frontend:
build:
context: ./../atwo-react-2
dockerfile: Dockerfile.dev
ports:
- '3001:3001'
volumes:
- ./../atwo-react-2:/app
depends_on:
- website
我得到的是这个错误
谷歌控制台中的GET http://website:3000/api/v1/examples net::ERR_NAME_NOT_RESOLVED
提前谢谢你。任何帮助都是受欢迎的。
发布于 2019-07-23 17:10:44
我设法从一个堆栈溢出帖子中获得了一些线索。在这里,Unable to have Docker containers communicate with each other。正如帖子中的答案所暗示的那样,我必须设置一个nginx容器来代理(重定向)请求。
我将我的axios请求更改为:
const value = await axios.get('/api/v1/examples');
并使用default.conf文件制作了一个nginx容器,如下所示:
upstream website {
server website:3000;
}
upstream frontend {
server frontend:3001;
}
server {
listen 80;
location / {
proxy_pass http://frontend;
}
location /sockjs-node {
proxy_pass http://frontend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location /api {
proxy_pass http://website;
}
}
希望这些信息能够帮助那些被困住的人。
https://stackoverflow.com/questions/57143314
复制相似问题