[英]Redis on Docker: Error 111 connecting to 0.0.0.0:6379. Connection refused
[英]docker-compose + django + redis - Error 111 connecting to 127.0.0.1:6379. Connection refused
我已經仔細閱讀了此答案 ,卻無法弄清楚如何將其應用於我的問題,因此,如果有答案,請務必進行澄清。
我也是docker和docker-compose的菜鳥。
我有一個簡單的docker-compose.yml
version: '3'
services:
redis:
image: "redis:alpine"
web:
build: . # current directory
command: bash -c "python /app/src/manage.py migrate &&
python /app/src/manage.py runserver 0.0.0.0:8000"
volumes:
- .:/app
ports:
- "8000:8000"
當我運行這個具有: docker-compose up
一切似乎確定:
$ docker-compose up
Starting hackerspace_redis_1 ...
Starting hackerspace_redis_1 ... done
Starting hackerspace_web_1 ...
Starting hackerspace_web_1 ... done
Attaching to hackerspace_redis_1, hackerspace_web_1
redis_1 | 1:C 19 Jul 2019 16:49:10.644 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 19 Jul 2019 16:49:10.644 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 19 Jul 2019 16:49:10.644 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 19 Jul 2019 16:49:10.645 * Running mode=standalone, port=6379.
redis_1 | 1:M 19 Jul 2019 16:49:10.645 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 19 Jul 2019 16:49:10.645 # Server initialized
redis_1 | 1:M 19 Jul 2019 16:49:10.645 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 | 1:M 19 Jul 2019 16:49:10.645 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1 | 1:M 19 Jul 2019 16:49:10.645 * DB loaded from disk: 0.000 seconds
redis_1 | 1:M 19 Jul 2019 16:49:10.645 * Ready to accept connections
web_1 | Operations to perform:
web_1 | Apply all migrations: account, admin, announcements, auth, badges, comments, contenttypes, courses, django_summernote, djconfig, djcytoscape, flatpages, notifications, portfolios, prerequisites, profile_manager, quest_manager, sessions, sites, socialaccount, suggestions, utilities
web_1 | Running migrations:
web_1 | No migrations to apply.
web_1 | Performing system checks...
web_1 |
web_1 | System check identified no issues (0 silenced).
web_1 | July 19, 2019 - 09:49:16
web_1 | Django version 2.0.13, using settings 'hackerspace_online.settings'
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
我可以通過瀏覽器127.0.0.0:8000訪問django應用程序。 但是,當我嘗試登錄該應用程序時,我得到:
位於/ accounts / login /的ConnectionError
錯誤111連接到127.0.0.1:6379。 拒絕連接。
這是我在Django設置中連接到Redis的方法:
REDIS_HOST = os.environ.get('REDIS_HOST', '127.0.0.1')
REDIS_PORT = os.environ.get('REDIS_PORT', '6379')
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://{}:{}/1".format(REDIS_HOST, REDIS_PORT),
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
},
#...
}
如何讓django應用連接到Redis容器?
請注意,據我所知,沒有其他使用端口6379的端口(通過sudo lsof -i -P -n | grep LISTEN
)
Docker中的127.0.0.1
幾乎總是表示“此容器”。 如果您正在Docker Compose下運行應用程序
...為您的應用設置一個網絡。 為服務每個容器連接默認的網絡,是由網絡上的其他容器都可達 ,並在相同的容器名稱主機名被他們發現 。
也就是說,在此docker-compose.yml
文件的上下文中,有主機名redis
和web
指向兩個容器。
您已經完成了重要的配置工作之一。 當您的代碼說
REDIS_HOST = os.environ.get('REDIS_HOST', '127.0.0.1')
您可以設置一個覆蓋內置默認值的環境變量。 因此,這里您只需docker-compose.yml
文件添加一個環境變量設置:
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
如果您使用的是docker-compose,則需要引用容器的名稱(即redis),而不是localhost。 Localhost是每個容器,docker-compose將連接隱式網絡上的每個容器,它們需要為其解析名稱。 將它們視為獨立的機器。 在配置網絡時,它有助於獲得視角。 因此,您嘗試連接到Redis的快速調整應該可以解決該問題。
文檔https://docs.docker.com/compose/networking/描述了當您沒有自己明確創建默認網絡時如何創建默認網絡。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.