It seems that the issue might be related to the way you are creating the Redis pool in your FastAPI application.
When you run the application outside the Docker container, it might be able to connect to the Redis server directly because it can find it on the host machine. However, when you run the application inside a Docker container, it won't be able to connect to the Redis server directly because it is not running inside the same network as the host machine.
To solve this issue, you need to create a Docker network that includes both the Redis container and the FastAPI container, and then connect your FastAPI container to this network. You can then use the name of the Redis container as the hostname when creating the Redis pool in your FastAPI application.
Here is an example of how you can create a Docker network and connect the containers to it:
cssdocker network create mynetwork
docker run -d --name redis --network mynetwork redis
docker run -it --name fastapi --network mynetwork myimage
Replace "myimage" with the name of the Docker image that you are using to run your FastAPI application.
Then, in your FastAPI application, you can create the Redis pool like this:
csharpredis_client = await asyncio_redis.Pool.create(
host="redis",
port=REDIS_PORT,
poolsize=6
)
Note that we are using "redis" as the hostname here, which corresponds to the name of the Redis container that we created earlier.
I hope this helps you solve the issue with your FastAPI application running inside a Docker container.
Comments
Post a Comment