5

This community is my last resort for this problem, as I have been fighting with this for several hours now :( .

i work on a project where i try do the tests with docker-compose, with this command docker-compose run runserver python3.6 manage.py test but, I do not understand where it comes from and how resolve this. here are my mistakes

/usr/local/lib/python3.6/dist-packages/django/db/backends/postgresql/base.py:267: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the default database instead.

and

psycopg2.OperationalError: could not connect to server: Connection timed out Is the server running on host "postgres" (172.18.0.3) and accepting TCP/IP connections on port 5432?

So, The docker-compose up working very well.

Here is the docker-compose.yml:

version: '3'

services:
  postgres:
    image: postgres:latest
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - path_volume
    ports:
      - "5432:5432"

  elasticsearch:
    image: path_elastic_search
    ports:
      - "9200:9200"
    environment:
      - xpack.security.enabled=false
    ulimits:
      nofile:
        soft: 65536
        hard: 65536
    volumes:
      - path_volume

  rabbitmq:
    image: rabbitmq:3
    ports:
      - "5672:5672"
      - "15672:15672"

  redis:
    image: redis

  celery:
    build:
      context: .
    env_file: .env
    volumes:
      - .:/opt/project
    depends_on:
      - postgres
      - elasticsearch
      - rabbitmq
      - redis
    command: celery -A project worker --beat -l debug

  runserver:
    build:
      context: .
    env_file: .env
    volumes:
      - .:/opt/project
      - /opt/project/src
    depends_on:
      - postgres
      - elasticsearch
      - rabbitmq
      - redis
      - celery
    command: python3.6 manage.py runserver 0.0.0.0:8000
    ports:
      - 8000:8000
my dependencies:
  • Django==1.11.5
  • postgres==9

the response at command line docker-compose ps is:

            Name                          Command                State                                          Ports                                     
----------------------------------------------------------------------------------------------------------------------------------------------------------
datablitzapi_celery_1          celery -A datablitz worker ...   Up                                                                                        
datablitzapi_elasticsearch_1   /bin/bash bin/es-docker          Up         0.0.0.0:9200->9200/tcp, 9300/tcp                                               
datablitzapi_postgres_1        docker-entrypoint.sh postgres    Up         0.0.0.0:5432->5432/tcp                                                         
datablitzapi_rabbitmq_1        docker-entrypoint.sh rabbi ...   Up         0.0.0.0:15672->15672/tcp, 25672/tcp, 4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp
datablitzapi_redis_1           docker-entrypoint.sh redis ...   Up         6379/tcp                                                                       
datablitzapi_runserver_1       python3.6 manage.py runser ...   Exit 255   0.0.0.0:8000->8000/tcp    
willkoua
  • 115
  • 4
  • 12
  • have you tried using links instead of dependes_on only? – Nafees Anwar Mar 15 '19 at 20:25
  • Can you confirm that the postgres docker container is running? If you edit your question with the output of `docker ps` or `docker-compose ps` that would be helpful. – Jack Gore Mar 15 '19 at 20:27

1 Answers1

2

This issue is happening because Django expecting the database will be up and running (Read for Connections)

psycopg2.OperationalError: could not connect to server: Connection timed out Is the server running on host "postgres" (172.18.0.3) and accepting TCP/IP connections on port 5432?

What you need is to add some delay mechanism to make Django wait until your PostgreSQL container is actually ready to receive connections. For more details you can take a look at my answer in here, it was about MySQL and same scenario applies to PostgreSQL.

Regarding this issue and according to the following answer, it might be also because of the same reason

/usr/local/lib/python3.6/dist-packages/django/db/backends/postgresql/base.py:267: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the default database instead.

So start by implementing the wait-for-it which explained at the first link and then try again

Mostafa Hussein
  • 11,063
  • 3
  • 36
  • 61