Docker组合服务无法通过服务名称进行通信 [英] Docker composed services can't communicate by service name

查看:67
本文介绍了Docker组合服务无法通过服务名称进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tldr:我无法通过其服务名称与docker组合服务进行通信,以向运行在网络容器中的api发出请求.

tldr: I can't communicate with a docker composed service by its service name in order to make requests to an api running in networked containers.

我有一个单页应用程序,它向json api发出请求.其Dockerfile如下所示:

I have a single page application that makes requests to a json api. Its Dockerfile looks like this:

FROM nginx:alpine
COPY dist /usr/share/nginx/html
EXPOSE 80

构建过程就可以完成,并将所有静态资产放在dist目录中,然后将其复制到nginx Web服务器的html目录中.

A build process does it's thing and puts all the static assets in a dist directory which is then copied to the html directory of the nginx web server.

我有一个由json-server驱动的模拟json API.其Dockerfile如下所示:

I have a mock json api powered by json-server. Its Dockerfile looks like this:

FROM node:7.10.0-alpine

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app/
RUN npm install

COPY . /usr/src/app

EXPOSE 3000

CMD [ "npm", "start" ]

我有一个docker-compose文件,如下所示:

I have a docker-compose file that looks like this:

version: '2'

services:
  badass-ui:
    image: mydocker-hub/badass-ui
    container_name: badass-ui
    ports:
      - "80:80"

  badderer-api:
    image: mydocker-hub/badderer-api
    container_name: badderer-api
    ports:
      - "3000:3000"

我能够成功地构建两个容器,并且能够在两个容器顺利运行的情况下运行"docker-compose up".从badass-ui到badderer-api:3000/用户的提取请求将返回"net :: ERR_NAME_NOT_RESOLVED".提取对 http://192.168.99.100:3000/users 的请求(或任何容器IP可能是)工作正常.我认为通过使用docker compose,我可以将docker-compose.yml中定义的服务的名称引用为域名,这样就可以通过域名在容器之间进行通信.这似乎不起作用.我的docker-compose.yml有什么问题吗?我使用的是Windows的Docker Quickstart终端随附的工具在Windows 10家庭版上.我正在使用docker-compose版本1.13.0,docker版本17.05.0-ce,docker计算机版本0.11.0和VirtualBox 5.1.20.

I'm able to build both containers successfully, and am able to run "docker-compose up" with both containers running smoothly. Fetch requests from badass-ui to badderer-api:3000/users returns "net::ERR_NAME_NOT_RESOLVED". Fetch requests to http://192.168.99.100:3000/users (or whatever the container IP may be) work fine. I thought by using docker compose I would be able to reference the name of a service defined in docker-compose.yml as a domain name, and that would enable communication between the containers via domain name. This doesn't seem to work. Is there something wrong with my docker-compose.yml? I'm on Windows 10 Home edition, using the tools that come with the Docker Quickstart terminal for Windows. I'm using docker-compose version 1.13.0, docker version 17.05.0-ce, docker-machine version 0.11.0 and VirtualBox 5.1.20.

推荐答案

由于您使用的是docker-compose.yml版本2,因此不需要链接.组成网络中的容器应能够按服务名称解析其他组成容器.

Since you are using docker-compose.yml version 2, links should not be necessary. Containers within a compose network should be able to resolve other compose containers by service name.

阅读有关问题的评论似乎可以解决网络和主机名解析问题,因此问题似乎出在您的Web UI中.我看不到您将任何类型的配置传递给UI应用程序,说明在何处可以找到api.也许您的用户界面中有一个指向api的硬编码网址,从而导致错误?

Reading the comments on your question it seems like the networking and host name resolution works, so it seems like the problem is in your web UI. I don't see you passing any type of configuration to the UI application saying where to find the api. Maybe there is a hard coded url to the api in your UI causing the error?

您的UI是客户端/javascript应用程序吗?您确定该应用程序实际上不是从您的浏览器拨打电话吗?您在本地计算机而非docker上运行的浏览器将无法解析badderrer-api主机名.

Is your UI a client side/javascript app? Are you sure the app isn't actually making the call from your browser? Your browser running on your local machine and not in docker will not be able resolve the badderrer-api hostname.

这篇关于Docker组合服务无法通过服务名称进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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