gpt4 book ai didi

python - docker 无法从文件连接到 redis

转载 作者:IT王子 更新时间:2023-10-29 06:14:02 26 4
gpt4 key购买 nike

我正在使用 docker-compose up 运行 docker。

在一个文件中,我试图访问使用以下方法创建的 redis:

import redis

pool = redis.ConnectionPool(host='redis', port=6379, db=0)
r = redis.Redis(connection_pool=pool)
r.flushall()

这是我的 docker-compose.yml redis 部分:

web:
build: .
ports:
- "8000:8000"
links:
- db
- redis
environment:
- DATABASE_URL=postgres://user:openhouse2016@db:5432/chat
- REDIS_URL=redis://redis:6379/1
volumes:
- .:/opt/app
db:
image: praiskup/postgresql:APIv1.0.1-fedora23
# volumes:
# - ./db:/var/lib/pgsql/data
environment:
- POSTGRESQL_DATABASE=chat
- POSTGRESQL_USER=user
- POSTGRESQL_PASSWORD=openhouse2016
- POSTGRESQL_CONTAINER_OPTS=assert_external_data = false
worker:
build: .
environment:
- DATABASE_URL=postgres://user:openhouse2016@db:5432/chat
- REDIS_URL=redis://redis:6379/1
links:
- db
- redis
volumes:
- .:/opt/app
# we need to wait for database setup
command: bash -c "sleep 7 && exec python /opt/app/channels-example/manage.py runworker -v3"
redis:
image: redis
ports:
- "6379:6379"
migrator:
build: .
environment:
- DATABASE_URL=postgres://user:openhouse2016@db:5432/chat
- REDIS_URL=redis://redis:6379/1
links:
- db
# it indeed takes this long to start the database
command: bash -c "sleep 5 && exec python /opt/app/channels-example/manage.py migrate"

运行后:

docker-compose down
docker-compose build
docker-compose up

一切运行良好,redis 表示它已准备好接受端口 6379 上的连接。

但是,上面的 python 代码生成:

redis.exceptions.ConnectionError: Error -5 connecting to redis:6379. No address associated with hostname.

如何从 python 文件连接到 redis?

编辑:

docker ps -a

redis 显示:

3a494c9da73d     redis        "docker-entrypoint.sh" 
10 seconds ago Up 9 seconds 0.0.0.0:6379->6379/tcp

最佳答案

迁移器没有链接到 redis。这修复了它:

migrator:
build: .
environment:
- DATABASE_URL=postgres://user:openhouse2016@db:5432/chat
- REDIS_URL=redis://redis:6379/1
links:
- db
- redis #this line here was missing
# it indeed takes this long to start the database
command: bash -c "sleep 5 && exec python /opt/app/channels-example/manage.py migrate"

关于python - docker 无法从文件连接到 redis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40321002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com