首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从本地django连接到docker db?

如何从本地django连接到docker db?
EN

Stack Overflow用户
提问于 2022-04-06 01:16:38
回答 1查看 118关注 0票数 0

如何从本地django连接到docker db?

代码语言:javascript
复制
version: '3'

services:

  redis-1:
    container_name: redis1
    build: ./docker/redis
    environment:
      X_REDIS_PORT: 7001
    networks:
      redisnet:
        ipv4_address: 10.0.0.11
    ports:
      - 7001:7001

  redis-2:
    container_name: redis2
    build: ./docker/redis
    environment:
      X_REDIS_PORT: 7002
    networks:
      redisnet:
        ipv4_address: 10.0.0.12
    ports:
      - 7002:7002

  redis-3:
    container_name: redis3
    build: ./docker/redis
    environment:
      X_REDIS_PORT: 7003
    networks:
      redisnet:
        ipv4_address: 10.0.0.13
    ports:
      - 7003:7003

  redis-4:
    container_name: redis4
    build: ./docker/redis
    environment:
      X_REDIS_PORT: 7004
    networks:
      redisnet:
        ipv4_address: 10.0.0.14
    ports:
      - 7004:7004

  redis-5:
    container_name: redis5
    build: ./docker/redis
    environment:
      X_REDIS_PORT: 7005
    networks:
      redisnet:
        ipv4_address: 10.0.0.15
    ports:
      - 7005:7005

  redis-6:
    container_name: redis6
    build: ./docker/redis
    environment:
      X_REDIS_PORT: 7006
    networks:
      redisnet:
        ipv4_address: 10.0.0.16
    ports:
      - 7006:7006

  redis-cluster:
    container_name: redis-cluster
    image: redis:latest
    command: redis-cli -p 7001 --cluster create 10.0.0.11:7001 10.0.0.12:7002 10.0.0.13:7003 10.0.0.14:7004 10.0.0.15:7005 10.0.0.16:7006 --cluster-replicas 1 --cluster-yes
    depends_on:
      - redis-1
      - redis-2
      - redis-3
      - redis-4
      - redis-5
      - redis-6
    networks:
      redisnet:
        ipv4_address: 10.0.0.2

  predixy:
    container_name: predixy
    build: ./docker/predixy
    depends_on:
      - redis-1
      - redis-2
      - redis-3
      - redis-4
      - redis-5
      - redis-6
    ports:
      - 7617:7617
    volumes:
      - ./docker/predixy/conf:/etc/predixy/conf
    networks:
      redisnet:
        ipv4_address: 10.0.0.3

networks:

  redisnet:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 10.0.0.0/16

这是我的docker-compose.yml文件,我想将位于端口7617上的predixy (集群代理)与django连接,如下所示。

代码语言:javascript
复制
# settings.py
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://0.0.0.0:7617/",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        },
    }
}
代码语言:javascript
复制
failed to connect to Redis: Connection refused (os error 111)

然而,django似乎无法连接到predixy。

作为您的信息,我还上传了predixy文件。

代码语言:javascript
复制
# docker/predixy/predixy.conf
################################### GENERAL ####################################
## Predixy configuration file example

## Specify a name for this predixy service
## redis command INFO can get this
Name PredixyExample

## Specify listen address, support IPV4, IPV6, Unix socket
## Examples:
# Bind 127.0.0.1:7617
# Bind 0.0.0.0:7617
# Bind /tmp/predixy

## Default is 0.0.0.0:7617
Bind 10.0.0.3:7617

## Worker threads
WorkerThreads 4

## Memory limit, 0 means unlimited

## Examples:
# MaxMemory 100M
# MaxMemory 1G
# MaxMemory 0

## MaxMemory can change online by CONFIG SET MaxMemory xxx
## Default is 0
MaxMemory 0

## Close the connection after a client is idle for N seconds (0 to disable)
## ClientTimeout can change online by CONFIG SET ClientTimeout N
## Default is 0
ClientTimeout 300


## IO buffer size
## Default is 4096
# BufSize 4096

################################### LOG ########################################
## Log file path
## Unspecify will log to stdout
## Default is Unspecified
Log ./predixy.log

## LogRotate support

## 1d rotate log every day
## nh rotate log every n hours   1 <= n <= 24
## nm rotate log every n minutes 1 <= n <= 1440
## nG rotate log evenry nG bytes
## nM rotate log evenry nM bytes
## time rotate and size rotate can combine eg 1h 2G, means 1h or 2G roate a time

## Examples:
# LogRotate 1d 2G
# LogRotate 1d

## Default is disable LogRotate


## In multi-threads, worker thread log need lock,
## AllowMissLog can reduce lock time for improve performance
## AllowMissLog can change online by CONFIG SET AllowMissLog true|false
## Default is true
# AllowMissLog false

## LogLevelSample, output a log every N
## all level sample can change online by CONFIG SET LogXXXSample N
LogVerbSample 0
LogDebugSample 0
LogInfoSample 10000
LogNoticeSample 1
LogWarnSample 1
LogErrorSample 1


################################### AUTHORITY ##################################
Include auth.conf

################################### SERVERS ####################################
Include cluster.conf
# Include sentinel.conf
# Include try.conf


################################### DATACENTER #################################
## LocalDC specify current machine dc
# LocalDC bj

## see dc.conf
# Include dc.conf


################################### COMMAND ####################################
## Custom command define, see command.conf
#Include command.conf

################################### LATENCY ####################################
## Latency monitor define, see latency.conf
Include latency.conf
代码语言:javascript
复制
## redis cluster server pool define
# cluster.conf
ClusterServerPool {
    MasterReadPriority 60
    StaticSlaveReadPriority 50
    DynamicSlaveReadPriority 50
    RefreshInterval 1
    ServerTimeout 1
    ServerFailureLimit 10
    ServerRetryTimeout 1
    Servers {
        + 10.0.0.11:7001
        + 10.0.0.12:7002
        + 10.0.0.13:7003
        + 10.0.0.14:7004
        + 10.0.0.15:7005
        + 10.0.0.16:7006
    }
}

我该如何解决这个问题?我在努力解决这个问题。我想要的是访问docker-cli并连接到redis-cli -h 10.0.0.3 -p 7617,这样我就可以看到从本地django输入的数据。

EN

回答 1

Stack Overflow用户

发布于 2022-04-06 09:21:23

predixy替换0.0.0.0,如下所示

代码语言:javascript
复制
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://predixy:7617/",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        },
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71760039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档