fastapi-cache icon indicating copy to clipboard operation
fastapi-cache copied to clipboard

Cannot connect to radis in Docker Container

Open Baicheng-MiQ opened this issue 3 years ago • 0 comments

After calling an API decorated by fastapi-cache:

Traceback (most recent call last): 
File "/usr/local/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi result = await app(self.scope, self.receive, self.send) 
File "/usr/local/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__ return await self.app(scope, receive, send) 
File "/usr/local/lib/python3.10/site-packages/fastapi/applications.py", line 269, in __call__ await super().__call__(scope, receive, send) File "/usr/local/lib/python3.10/site-packages/starlette/applications.py", line 124, in __call__ await self.middleware_stack(scope, receive, send) File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in __call__ raise exc 
File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__ await self.app(scope, receive, _send) 
File "/usr/local/lib/python3.10/site-packages/starlette/middleware/cors.py", line 84, in __call__ await self.app(scope, receive, send) 
File "/usr/local/lib/python3.10/site-packages/starlette/exceptions.py", line 93, in __call__ raise exc File "/usr/local/lib/python3.10/site-packages/starlette/exceptions.py", line 82, in __call__ await self.app(scope, receive, sender) 
File "/usr/local/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__ raise e 
File "/usr/local/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__ await self.app(scope, receive, send) 
File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 670, in __call__ await route.handle(scope, receive, send) 
File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 266, in handle await self.app(scope, receive, send) 
File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 65, in app response = await func(request) 
File "/usr/local/lib/python3.10/site-packages/fastapi/routing.py", line 227, in app raw_response = await run_endpoint_function( File "/usr/local/lib/python3.10/site-packages/fastapi/routing.py", line 160, in run_endpoint_function return await dependant.call(**values) 
File "/usr/local/lib/python3.10/site-packages/fastapi_cache/decorator.py", line 45, in inner ttl, ret = await backend.get_with_ttl(cache_key) File "/usr/local/lib/python3.10/site-packages/fastapi_cache/backends/redis.py", line 14, in get_with_ttl return await (pipe.ttl(key).get(key).execute()) 
File "/usr/local/lib/python3.10/site-packages/aioredis/client.py", line 4618, in execute conn = await self.connection_pool.get_connection("MULTI", self.shard_hint) 
File "/usr/local/lib/python3.10/site-packages/aioredis/connection.py", line 1416, in get_connection await connection.connect() File "/usr/local/lib/python3.10/site-packages/aioredis/connection.py", line 698, in connect raise 
ConnectionError(self._error_message(e)) aioredis.exceptions.ConnectionError: Error 111 connecting to 127.0.0.1:6379. 111.

My Dockerfile is:

FROM python:3.10

# INSTALL REDIS
RUN curl -fsSL https://packages.redis.io/gpg | gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
RUN echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/redis.list
RUN tee /etc/apt/sources.list.d/redis.list
RUN apt-get update
RUN apt-get -qq install redis
RUN apt-get -qq install redis-server

#
WORKDIR /code

#
COPY ./requirements.txt /code/requirements.txt

#
RUN pip -q install --no-cache-dir --upgrade -r /code/requirements.txt --use-deprecated=legacy-resolver
#


COPY . /code

# -h flag specifies host name and --daemonize flag let it runs on backstage
CMD ["redis-server","-h", "127.0.0.1", "--daemonize", "yes"]
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]

AND main.py file,

import aioredis
from fastapi import FastAPI, status, Query
from fastapi.middleware.cors import CORSMiddleware
from starlette.requests import Request
from starlette.responses import Response
from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend
from fastapi_cache.decorator import cache

@app.get("/")
@cache(expire=60)
async def index(request: Request, response: Response):
    return dict(hello="world")

@app.on_event("startup")
async def startup():
    redis =  aioredis.from_url("redis://127.0.0.1", encoding="utf8", decode_responses=True)
    FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")

Any assistance is much appreciated! 谢谢大佬

Baicheng-MiQ avatar Jul 23 '22 10:07 Baicheng-MiQ