[英]Unable to connect redis from python inside docker container
Dockerfile Docker文件
FROM python:3.6-slim
ENV PYTHONUNBUFFERED 1
WORKDIR /usr/src/duck
COPY ./ /usr/src/duck
RUN apt-get update
RUN apt-get install -y redis-server
RUN pip install -r requirements.txt
CMD ["/bin/bash","-c","python manage.py runserver 0.0.0.0:8000"]
python code: python代码:
import redis
red = redis.StrictRedis(host="redis", port=6379, db=0)
red.set("working", "yes")
Here i am trying to dockerize both python and redis. 在这里,我正在尝试将python和redis都进行docker化。 Above is my Dockerfile and python code. 上面是我的Dockerfile和python代码。
It is throwing me below error: 它使我陷入错误:
while running python code. 在运行python代码时。
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/src/duck/settings/views.py", line 6, in check_redis
red.set("working", "yes")
File "/usr/local/lib/python3.6/site-packages/redis/client.py", line 1451, in set
return self.execute_command('SET', *pieces)
File "/usr/local/lib/python3.6/site-packages/redis/client.py", line 772, in execute_command
connection = pool.get_connection(command_name, **options)
File "/usr/local/lib/python3.6/site-packages/redis/connection.py", line 994, in get_connection
connection.connect()
File "/usr/local/lib/python3.6/site-packages/redis/connection.py", line 497, in connect
raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error -2 connecting to redis:6379. Name or service not known.
Please have a look. 请看一看。
The container is running only the python server, not redis. 容器仅运行python服务器,而不运行redis。 You should initiate the redis server on another container (probably the redis default image will do). 您应该在另一个容器上启动redis服务器(redis默认映像可能会这样做)。 And make the python server point to it. 并使python服务器指向它。
You are lucky, because the compose docs have an example with a python app and Redis server. 您很幸运,因为撰写文档中有一个包含python应用程序和Redis服务器的示例。 https://docs.docker.com/compose/gettingstarted/ https://docs.docker.com/compose/gettingstarted/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.