021
021

Reputation: 333

Python code inside container fails to access Redis inside second container

Context : I'm trying to run through a Docker container a Plotly-Dash/Flask based web application that connects to a Redis server that runs inside a second container. I'm trying to achieve something close to this example, only with my application.

So I have in my project folder :

  1. The main application videoblender.py inside a package named apps
  2. A dockerfile named Dockerfile
  3. A docker-compose file named docker-compose

Problem : When I run my program through the command docker-compose up --build, the building succeed then I get an error saying the following [Errno -3] Temporary failure in name resolution.

What I've tried : I've tried to run the example from the link above which a simplified example of what I'm trying to achieve, and it worked. So the problem seem to be somewhere in my specific implementation of it.

My code works fine outside of containers, with a local redis server running at localhost:6379. When I run it locally, I assign to the host parameter of the Redis object constructor the value 0.0.0.0, or localhost, it doesn't matter which one.

Additional informations and files :

docker-compose.yml :

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

Dockerfile file of my web application :

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"]

Code that raises the exception :

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

Called in the main python code videoblender.py

ra = RedisAccess()

Full error :

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.

Upvotes: 0

Views: 599

Answers (1)

Marat
Marat

Reputation: 15738

in the docker-compose.yml, under web section, add:

links:
  - redis

Upvotes: 2

Related Questions