2

Regards I have been working on a Django Application, that runs on Redis, PostgreSQL, Celery, RabbitMQ. I have written a docker-compose to run all of these services in their separate containers.

Here's my docker-compose.yml

version: "3.2"

services:
  app:
    build:
      context: .
    image: &app app
    ports:
      - "8000:8000"
    env_file: &envfile
      - env.env
    volumes:
      - ./app:/app
    environment:
      - DB_HOST=db
    command: >
      sh -c "python manage.py wait_for_db &&
            python manage.py migrate &&
            python manage.py runserver 0.0.0.0:8000"
    depends_on:
      - db
      - redis
      - broker

  redis:
    restart: always
    image: redis:latest
    ports:
      - "6379:6379"

  db:
    image: postgres:12-alpine
    environment:
      - "POSTGRES_HOST_AUTH_METHOD=trust"

  worker:
    build: .
    image: *app
    restart: always
    env_file: *envfile
    command: ["celery", "worker", "--app=worker.worker.app", "--concurrency=1", "--hostname=worker@%h", "--loglevel=INFO"]
    volumes:
      - ./app:/app
    depends_on:
      - broker
      - redis
      - db

  broker:
    image: rabbitmq:3
    env_file: *envfile
    ports:
      - 5672:5672

  flower:
    image: zoomeranalytics/flower:0.9.1-4.0.2
    restart: "no"
    env_file: *envfile
    ports:
      - "5555:5555"
    depends_on:
      - broker

My application is working just fine, and the containers seems to be working, the problem arises when I push an Async job to the worker container. The worker container picks up the job, and start processing, I am trying to access the DB in the worker, but it's giving me the following error -

Task [640127f3-7769-4757-8c33-8de9052ca92c] raised unexpected: OperationalError('could not connect to server: No such file or directory\n\tIs the server running locally and accepting\n\tconnections on Unix domain socket "/tmp/.s.PGSQL.5432"?\n')

I understand that my worker container is trying to access DB, which it is not able to find, can someone please help me access my DB container through the Worker container, quite stuck with this.

I tried depends_on, links, but nothing seems to be working.

2
  • Can you add your configuration like connection string to database? Commented May 19, 2020 at 19:14
  • 1
    Got the issue resolved, I missed the DB_HOST part for worker container. Thanks for the help btw :) Commented May 19, 2020 at 19:34

1 Answer 1

4

Got it, thanks to my teammate.

I missed giving environment variable in the worker container, as soon as I passed, voila!

environment:
  - DB_HOST=db
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.