gpt4 book ai didi

docker 组成 : Cannot connect to Redis

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

我在这里学习 Docker Compose 教程 https://docs.docker.com/get-started/part5/#recap-optional

version: "3"
services:
web:
image: example/get-started:part-1
deploy:
replicas: 10
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "80:80"
networks:
- webnet
visualizer:
image: dockersamples/visualizer:stable
ports:
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints: [node.role == manager]
networks:
- webnet
redis:
image: redis
ports:
- "6379:6379"
volumes:
- ./data:/data
deploy:
placement:
constraints: [node.role == manager]
networks:
- webnet
driver:
build: .
links:
- redis
networks:
webnet:

虽然 Redis 似乎在 myvm1 上运行,但应用程序无法连接到它,并给出错误。

这是重要的应用程序代码:

from flask import Flask
from redis import Redis, RedisError
import os
import socket

redis = Redis(host="redis", db=0, socket_connect_timeout=0, socket_timeout=0)

app = Flask(__name__)

@app.route("/")
def hello():
try:
visits = redis.incr("counter")
except RedisError:
visits = "<i>cannot connect to redis. Counter disabled</i>"

html = "<h3>Hello {name}!</h3>" \
"<b>Hostname:</b> {hostname}<br/>" \
"<b>Visits:</b> {visits}"
return html.format(name=os.getenv("NAME", "World"), hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)

虚拟机 IP:

myvm1 - virtualbox Running tcp://192.168.99.101:2376
v17.07.0-ce

myvm2 - virtualbox Running tcp://192.168.99.102:2376
v17.07.0-ce

Redis 在虚拟机上正常运行。

有什么想法吗?网上有很多类似的讨论,但都没有帮助。

最佳答案

如果 redis 在 VM 上运行,则绑定(bind)可能不正确。您能否检查它是否绑定(bind)在 0.0.0.0 上,否则您需要编辑 redis配置绑定(bind)到 0.0.0.0 和外部服务连接到它的端口

关于 docker 组成 : Cannot connect to Redis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46006260/

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