shell
shell

Reputation: 1937

docker cannot connect to redis from file

I am running docker using docker-compose up.

In a file I am trying to access the redis created using:

import redis

pool = redis.ConnectionPool(host='redis', port=6379, db=0)
r = redis.Redis(connection_pool=pool)
r.flushall()

Here is my docker-compose.yml redis portion:

web:
  build: .
  ports:
   - "8000:8000"
  links:
   - db
   - redis
  environment:
    - DATABASE_URL=postgres://user:openhouse2016@db:5432/chat
    - REDIS_URL=redis://redis:6379/1
  volumes:
    - .:/opt/app
db:
  image: praiskup/postgresql:APIv1.0.1-fedora23
#  volumes:
#   - ./db:/var/lib/pgsql/data
  environment:
   - POSTGRESQL_DATABASE=chat
   - POSTGRESQL_USER=user
   - POSTGRESQL_PASSWORD=openhouse2016
   - POSTGRESQL_CONTAINER_OPTS=assert_external_data = false
worker:
  build: .
  environment:
    - DATABASE_URL=postgres://user:openhouse2016@db:5432/chat
    - REDIS_URL=redis://redis:6379/1
  links:
   - db
   - redis
  volumes:
    - .:/opt/app
  # we need to wait for database setup
  command: bash -c "sleep 7 && exec python /opt/app/channels-example/manage.py runworker -v3"
redis:
  image: redis
  ports:
        - "6379:6379"
migrator:
  build: .
  environment:
   - DATABASE_URL=postgres://user:openhouse2016@db:5432/chat
   - REDIS_URL=redis://redis:6379/1
  links:
   - db
  # it indeed takes this long to start the database
  command: bash -c "sleep 5 && exec python /opt/app/channels-example/manage.py migrate"

After running:

docker-compose down
docker-compose build
docker-compose up

Everything runs fine and redis says it is ready to accept connections on port 6379.

However, the python code above generates:

redis.exceptions.ConnectionError: Error -5 connecting to redis:6379. No address associated with hostname.

How do I connect to redis from the python file?

EDIT:

docker ps -a

redis shows:

3a494c9da73d     redis        "docker-entrypoint.sh" 
10 seconds ago   Up 9 seconds  0.0.0.0:6379->6379/tcp

Upvotes: 1

Views: 1723

Answers (1)

shell
shell

Reputation: 1937

The migrator did not link to redis. This fixed it:

migrator:
  build: .
  environment:
   - DATABASE_URL=postgres://user:openhouse2016@db:5432/chat
   - REDIS_URL=redis://redis:6379/1
  links:
   - db
   - redis #this line here was missing
  # it indeed takes this long to start the database
  command: bash -c "sleep 5 && exec python /opt/app/channels-example/manage.py migrate"

Upvotes: 1

Related Questions