【问题标题】:Python code inside container fails to access Redis inside second container容器内的 Python 代码无法访问第二个容器内的 Redis
【发布时间】:2020-11-14 12:14:02
【问题描述】:

上下文:我正在尝试通过 Docker 容器运行基于 Plotly-Dash/Flask 的 Web 应用程序,该应用程序连接到在第二个容器内运行的 Redis 服务器。我正在尝试仅通过我的应用程序实现接近this example 的目标。

所以我的项目文件夹中有:

  1. 主应用程序videoblender.py 在名为apps 的包内
  2. 名为 Dockerfile 的 dockerfile
  3. 名为 docker-compose 的 docker-compose 文件

问题:当我通过命令docker-compose up --build 运行我的程序时,构建成功然后我收到一个错误,提示以下[Errno -3] Temporary failure in name resolution

我尝试过的内容:我尝试从上面的链接中运行示例,该示例是我尝试实现的简化示例,并且成功了。所以问题似乎出在我的具体实现中。

我的代码在容器之外运行良好,本地 redis 服务器运行在 localhost:6379。当我在本地运行它时,我将值0.0.0.0localhost赋给Redis对象构造函数的host参数,不管是哪一个。

其他信息和文件

docker-compose.yml

version: '0'
services:
  web:
    build: .
    ports:
      - "8003:8003"
  redis:
    image: "redis:alpine"

Dockerfile 我的网络应用程序文件:

FROM python:3.6-slim
# copy needed things
ADD pocs /code/pocs
ADD apps /code/apps
ADD requirements.txt /code
ADD setup.py /code

WORKDIR /code

# libgl1-mesa-glx is needed for openCV
RUN apt-get update && \
    apt install -y libglib2.0-dev libsm6 libxext6 libxrender1 && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

RUN pip install -r requirements.txt

RUN pip install .

CMD ["python", "apps/videoblender.py"]

引发异常的代码:

class RedisAccess(object):    
    def __init__(self, host='redis', port=6379, db=0):
        self.redis_server = redis.Redis(host=host, port=port, db=db)

在主要python代码中调用videoblender.py

ra = RedisAccess()

完全错误:

web_1    | Traceback (most recent call last):
web_1    |   File "/usr/local/lib/python3.6/site-packages/redis/connection.py", line 559, in connect
web_1    |     sock = self._connect()
web_1    |   File "/usr/local/lib/python3.6/site-packages/redis/connection.py", line 585, in _connect
web_1    |     socket.SOCK_STREAM):
web_1    |   File "/usr/local/lib/python3.6/socket.py", line 745, in getaddrinfo
web_1    |     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
web_1    | socket.gaierror: [Errno -3] Temporary failure in name resolution
web_1    | 
web_1    | During handling of the above exception, another exception occurred:
web_1    | 
web_1    | Traceback (most recent call last):
web_1    |   File "/usr/local/lib/python3.6/site-packages/redis/client.py", line 2281, in 
smembers
web_1    |     return self.execute_command('SMEMBERS', name)
web_1    |   File "/usr/local/lib/python3.6/site-packages/redis/client.py", line 898, in execute_command
web_1    |     conn = self.connection or pool.get_connection(command_name, **options)
web_1    |   File "/usr/local/lib/python3.6/site-packages/redis/connection.py", line 1192, in get_connection
web_1    |     connection.connect()
web_1    |   File "/usr/local/lib/python3.6/site-packages/redis/connection.py", line 563, in connect
web_1    |     raise ConnectionError(self._error_message(e))
web_1    | redis.exceptions.ConnectionError: Error -3 connecting to redis:6379. Temporary failure in name resolution.

【问题讨论】:

    标签: python docker redis docker-compose dockerfile


    【解决方案1】:

    在 docker-compose.yml 的 web 部分下,添加:

    links:
      - redis
    

    【讨论】:

    • 就这么简单。谢谢,这真的是一种解脱。
    猜你喜欢
    • 2019-01-05
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 2020-06-05
    相关资源
    最近更新 更多