在docker-compose服务之间进行通讯时遇到问题 [英] Having trouble communicating between docker-compose services

查看:94
本文介绍了在docker-compose服务之间进行通讯时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 docker-compose 文件:

version: "3"

services:
  scraper-api:
    build: ./ATPScraper
    volumes:
      - ./ATPScraper:/usr/src/app
    ports:
      - "5000:80"
  test-app:
    build: ./test-app
    volumes:
      - "./test-app:/app"
      - "/app/node_modules"
    ports:
      - "3001:3000"
    environment:
      - NODE_ENV=development
    depends_on:
      - scraper-api

构建以下 Dockerfile 的:

scraper-api (python 烧瓶应用程序):

scraper-api (a python flask application):

FROM python:3.7.3-alpine

WORKDIR /usr/src/app

COPY requirements.txt ./

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["python", "./app.py"]

test-app (针对api的测试 react 应用程序):

test-app (a test react application for the api):

# base image
FROM node:12.2.0-alpine

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:/app/src/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /app/package.json
RUN npm install --silent
RUN npm install react-scripts@3.0.1 -g --silent
RUN npm install axios -g

# start app
CMD ["npm", "start"]

诚然,在Docker联网方面,我是一个新手,但是我试图让 React 应用,以便与 scraper-api 进行通信。例如, scraper-api 具有以下端点: / api / top_10 。我尝试了以下网址的各种排列:
http:// scraper-api:80 / api / test_api 。他们中没有一个人为我工作。

Admittedly, I'm a newbie when it comes to Docker networking, but I am trying to get the react app to communicate with the scraper-api. For example, the scraper-api has the following endpoint: /api/top_10. I have tried various permutations of the following url: http://scraper-api:80/api/test_api. None of them have been working for me.

我一直在清理互联网,却找不到真正的解决方案。

I've been scavenging the internet and I can't really find a solution.

推荐答案

React应用程序在最终用户的浏览器中运行,根本不知道这个 Docker是存在的,也不知道任何Docker Compose网络建立。对于碰巧是由Docker托管的浏览器应用程序,需要将其配置为使用主机的 DNS名称或IP地址以及后端服务的已发布端口。

The React application runs in the end user's browser, which has no idea this "Docker" thing exists at all and doesn't know about any of the Docker Compose networking setup. For browser apps that happen to be hosted out of Docker, they need to be configured to use the host's DNS name or IP address, and the published port of the back-end service.

常见的设置(Docker或其他方式)是将浏览器应用程序和后端应用程序都放在反向代理后面。在这种情况下,您可以使用没有主机名的相对URL,例如 / api /...,它们将被解释为相同的主机和端口,从而绕开了这个问题完全。

A common setup (Docker or otherwise) is to put both the browser apps and the back-end application behind a reverse proxy. In that case you can use relative URLs without host names like /api/..., and they will be interpreted as "the same host and port", which bypasses this problem entirely.

这篇关于在docker-compose服务之间进行通讯时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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