title: Error connecting Celery to Redis when using Docker
body: <p>I have used <code>docker-compose</code> to create a docker instance that contains redis and celery. This will build successfully, however, in my python script when I try add something to my queue i get the following error:</p>
  
  <pre><code>Error 8 connecting to redis:6379. nodename nor servname provided, or not known.
  </code></pre>
  
  <p>I have tried changing my the celeryconfig file addresses, and have experimented with a solution suggested here: <a href="https://github.com/mozilla-services/cliquet/issues/664" rel="nofollow noreferrer">https://github.com/mozilla-services/cliquet/issues/664</a> but I have not been able to fix it.</p>
  
  <p>Has anyone else encountered an issue like this?</p>
  
  <p>** a part of example.py **</p>
  
  <pre><code>app = Celery('server', broker='redis://redis:6379/0')
  app.config_from_object(celeryconfig)
  
  @app.task(trail=True)
  def count_words_at_url(url):
      time.sleep(50)
      resp = requests.get(url)
      return len(resp.text.split())
  </code></pre>
  
  <p><strong>celeryconfig.py</strong></p>
  
  <pre><code>result_backend = 'redis://redis:6379/0'
  broker_url = 'redis://redis:6379/0'
  worker_prefetch_multiplier = 1
  worker_concurrency  =1
  </code></pre>
  
  <p><strong>Dockerfile.celery</strong></p>
  
  <pre><code>FROM python:3.6-alpine
  WORKDIR /usr/src/app
  COPY requirements.txt .
  RUN pip install --no-cache-dir -r requirements.txt
  COPY . .
  CMD ulimit -n 1024
  CMD celery -A celeryExample worker --concurrency=1 --loglevel=info
  </code></pre>
  
  <p><strong>docker-compose.yml</strong></p>
  
  <pre><code>version: '3'
  
  services:
    redis:
      image: redis
      ports:
        - 6379:6379
    celery: 
      build: 
          context: .
          dockerfile: Dockerfile.celery
      links: 
        - redis:redis
      depends_on:
        - redis
      volumes: 
          - .:/usr/src/app    
  </code></pre>
  
  <p>With the accepted answer (still need to split broker and backend to separate queues), I have managed to get everything to work as should be with the following changes :</p>
  
  <p><strong>example.py</strong>:</p>
  
  <pre><code>broker_url = os.environ.get('broker_url', 'redis://localhost:6379'),
  result_backend = os.environ.get('result_backend', 'redis://localhost:6379')
  
  app = Celery('server', broker = broker_url,
                         backend = result_backend)
  app.config_from_object(celeryconfig)
  
  @app.task(trail=True)
  def count_words_at_url(url):
      time.sleep(50)
      resp = requests.get(url)
      return len(resp.text.split())
  </code></pre>
  
  <p><strong>Dockerfile.celery</strong></p>
  
  <pre><code>FROM python:3.6-alpine
  ENV CELERY_BROKER_URL redis://redis:6379/0
  ENV CELERY_RESULT_BACKEND redis://redis:6379/0
  ENV C_FORCE_ROOT true
  WORKDIR /usr/src/app
  COPY requirements.txt .
  RUN pip install --no-cache-dir -r requirements.txt
  COPY . .
  CMD celery -A celeryExample worker --concurrency=1 --loglevel=info 
  </code></pre>
  
  <p><strong>celeryconfig.py</strong></p>
  
  <pre><code>worker_prefetch_multiplier = 1
  worker_concurrency  =1
  </code></pre>