Having trouble communicating between docker-compose services

后端 未结 2 1564
悲哀的现实
悲哀的现实 2021-01-28 04:39

I have the following docker-compose file:

version: \"3\"

services:
  scraper-api:
    build: ./ATPScraper
    volumes:
      - ./ATPScraper:/usr/sr         


        
相关标签:
2条回答
  • 2021-01-28 05:02

    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.

    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.

    0 讨论(0)
  • 2021-01-28 05:10

    As a side note: when no network is specified inside docker-compose.yml, default network will be created for you with the following name [dir location of docker_compose.yml]_default. For example, if docker_compose.yml is in app folder. the network will be named app_default.

    Now, inside this network, containers are reachable by their service names. So scraper-api host should resolve to the right container.

    It could be that you are using wrong endpoint URL. In the question, you mentioned /api/top_10 as an endpoint, but URL to test was http://scraper-api:80/api/test_api which is inconsistent.

    Also, it could be that you confused the order of the ports in docker-compose.yml for scraper-api service:

    ports:
       - "5000:80"
    

    5000 is being exposed to host where docker is running. 80 is internal app port. Normally, flask apps are listening on 5000, so I thought you might have meant to say:

    ports:
       - "80:5000"
    

    In which case, between containers you have to use :5000 as destination port in URLs: http://scraper-api:5000 as an example (+ endpoint suffix, of course).

    To check connectivity, you might want to bash into client container, and see if things are connecting:

    docker-compose exec test-app bash
    wget http://scraper-api
    wget http://scraper-api:5000
    

    etc.

    If you get a response, then you have connectivity, just need to figure out correct endpoint URL.

    0 讨论(0)
提交回复
热议问题