Home > OS >  docker aiohttp service cannot connect to another docker fastapi service
docker aiohttp service cannot connect to another docker fastapi service

Time:11-12

I have two services, first is an FastAPI, another is a service which is calling this service each X seconds.

FastAPI:

app = FastApi()
@app.post("/accounts/", status_code=status.HTTP_200_OK)
async def account_split():
    return {"details": "Accounts updated."}

aiohttp:

async def process():
    while True:
        for i in range(10):
            await asyncio.sleep(delay)

            async with ClientSession() as session:
                try:
                    async with session.post("http://0.0.0.0:8080/accounts/", json=json.dumps({"i": i}), ssl=False) as response:
                        response = await response.read()
                        print(response)
                except ClientConnectionError as e:
                    print('Connection Error', str(e))


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(process())
    loop.run_forever()

docker-compose:

version: "3.9"
services:
  aio:
    build: ./aio
    container_name: aio
    networks:
      - default
    restart: on-failure
    depends_on:
      - api
    command: ["python", "./aio/main.py"]

  api:
    build: ./api
    container_name: api
    networks:
      - default
    ports:
      - "8080:8080"
    restart: on-failure
    command: ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8080"]

networks:
  default: {}

I am still getting this error

Cannot connect to host 0.0.0.0:8080 ssl:False [Connect call failed ('0.0.0.0', 8080)]

Not sure what is the cause, any ideas? Thanks

CodePudding user response:

According to the Docker Compose documentation, a container created using api config joins the default network under the name api. Therefore you should use it instead of 0.0.0.0, like so http://api:8080/accounts/.

Also, it should be taken into account, that depends_on does not wait for the container to be “ready” before starting the dependant - only until it have been started.

  • Related