无法让Docker容器相互通信 [英] Unable to have Docker containers communicate with each other

查看:75
本文介绍了无法让Docker容器相互通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Docker容器,一个运行React应用(使用create-react-app构建),另一个运行Node应用API.我已经设置了一个docker-compose文件,并且根据文档,我应该能够使用服务名称在容器之间进行通信.

I have two Docker containers, one running a React app (built using create-react-app) and another with a Node app API. I have a docker-compose file set up, and according to the documentation I should be able to use the names of the services to communicate between containers.

但是,当我尝试从React应用程序向我的API的/login 端点发送请求时,我收到了 net :: ERR_NAME_NOT_RESOLVED 错误.我正在使用Unirest发送请求.

However, when I try to send a request to the /login endpoint of my API from the React app I received a net::ERR_NAME_NOT_RESOLVED error. I'm using Unirest to send the request.

我在网上做了很多挖掘工作,遇到了一些描述类似问题的事情,但是仍然找不到解决方案.当我运行 cat/etc/resolve.conf (请参见问题)在我的React容器中没有显示带有我的API的容器,但是Docker对我来说还算是一个新手,所以我不确定这是否是问题的一部分.我也尝试过在撰写文件中使用链接和用户定义的网络,但无济于事.

I've done a bunch of digging around online and have come across a few things describing similar issues but still haven't been able to find a solution. When I run cat /etc/resolve.conf (see this issue) in my React container the container with my API doesn't show up, but Docker is still fairly new to me so I'm not sure if that's part of the issue. I've also tried using links and user-defined networks in my compose file but to no avail.

我已经包含了 docker-compose.yml 文件的要点以及请求的代码段.任何帮助深表感谢!

I've included gists of my docker-compose.yml file as well as the code snippet of my request. Any help is much appreciated!

docker-compose.yml
对/logi的通用请求 n

推荐答案

如原始帖子的注释中所述,这是一个DNS问题.在此项目的用例中,配置DNS涉及一些工作,因此我通过使用环境变量根据是否在运行API来设置用于对API容器进行调用的URL来解决了我的问题开发或生产环境:

As discussed in the comments on the original post this was a DNS issue. Configuring the DNS was a little to involved for the use case of this project, so I've solved my problem by using an environment variable to set the URL that is used to make calls to my API container based on whether I'm running on a dev or prod environment:

(process.env.REACT_APP_URL_ROOT ||'/app')

在本地运行时,我将 REACT_APP_URL_ROOT 设置为我的本地主机地址,并在构建和部署React应用程序时将nginx容器配置为代理到/app .

I set REACT_APP_URL_ROOT to my localhost address when running locally, and have an nginx container configured to proxy to /app when I build and deploy the React app.

这篇关于无法让Docker容器相互通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆