[英]Error -2 connecting to redis://redis:6379:6379. Name or service not known
I was hoping to get some insight to what I am missing, currently trying to run a docker-compose config with python (walrus for db wrapper) and redis image, but I keep receiving the same error:我希望对我所缺少的有所了解,目前正在尝试使用 python (数据库包装器的海象)和 redis 图像运行 docker-compose 配置,但我一直收到相同的错误:
redis.exceptions.ConnectionError: Error -2 connecting to redis://redis:6379. redis.exceptions.ConnectionError:连接到 redis://redis:6379 时出现错误 -2。 Name or service not known.
名称或服务未知。
I tried different solutions on stack overflow to fix this but still nothing is working.我尝试了不同的堆栈溢出解决方案来解决这个问题,但仍然没有任何效果。
Here are the related docker-compose config:以下是相关的 docker-compose 配置:
version: '3.3'
services:
redis:
image: redis:latest
container_name: redis
ports:
- "6379:6379"
command: ["redis-server"]
entrypoint: redis-server --appendonly yes
consumers:
build: ./consumers
container_name: consumers
environment:
- REDIS_HOST=redis://redis
command: "./run.sh"
depends_on:
- redis
restart: always
tty: true
networks:
default:
driver: bridge
Dockerfile: Dockerfile:
FROM python:3.10
WORKDIR /consumers
# Copy Dependencies
COPY requirements.txt requirements.txt
COPY run.sh .
# Install Dependencies
RUN pip install -r requirements.txt
COPY . .
ENV REDIS_HOST=redis://redis
RUN chmod a+x run.sh
# Run executable consumer.py
CMD [ "./run.sh"]
And connection in python using walrus to redis:并在 python 中使用海象连接到 redis:
rdb = Database(host=os.getenv("REDIS_HOST", "localhost"), port=6379)
Locally without docker the setup works fine.在本地没有 docker 的设置工作正常。 Any direction in this case would be really appreciated.
在这种情况下,任何方向都将不胜感激。
Thank you谢谢
The following configuration made it work, removed entrypoint, created a new custom network and exposed port.以下配置使其工作,删除入口点,创建新的自定义网络和公开端口。 REDIS_HOST was modified to redis ie container name.
REDIS_HOST 修改为 redis 即容器名称。 All together made it work but while trying only one of these the problem persisted.
所有这些都使它起作用,但是在仅尝试其中一种时,问题仍然存在。
version: '3.5'
services:
redis:
image: redis:latest
container_name: redis
ports:
- "6379:6379"
expose:
- 6379:6379
command: ["redis-server"]
networks:
- connections
consumers-g1:
build: ./consumers
container_name: consumers-g1
environment:
- REDIS_HOST=redis
command: "./run.sh"
expose:
- 6379:6379
networks:
- connections
restart: always
tty: true
networks:
connections:
name: connections
driver: bridge
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.