且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

docker-compose + django + redis-错误111连接到127.0.0.1:6379.连接被拒绝

更新时间:2022-11-16 22:55:50

127.0.0.1 几乎总是表示此容器".如果您在 Docker Compose 下运行应用程序,

127.0.0.1 in Docker almost always means "this container". If you're running your application under Docker Compose, it

...为您的应用设置单个网络.服务的每个容器都加入默认网络,并且在该网络上的其他容器都可以可访问,在与容器名称相同的主机名下,它们也可以可发现.>

... sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

也就是说,在此 docker-compose.yml 文件的上下文中,有主机名 redis web 指向两个容器.

That is, within the context of this docker-compose.yml file, there are host names redis and web that point at the two containers.

您已经完成了重要的配置工作之一.当您的代码说

You've already done one of the important configuration things. When your code says

REDIS_HOST = os.environ.get('REDIS_HOST', '127.0.0.1')

您可以设置一个覆盖内置默认值的环境变量.因此,这里您只需要在 docker-compose.yml 文件中添加环境变量设置:

you can set an environment variable that overrides the built-in default value. So here you just need to add an environment variable setting to your docker-compose.yml file:

version: '3'
services:
  redis:
    image: "redis:alpine"
  web:
    build: . # current directory
    environment:
      - REDIS_HOST=redis
    ports:
      - "8000:8000"
    # application source code and default command are built into the image