3

I m creating a docker-compose config for an django app, the Dockerfile builds successfully but when I compose them up, django return an issue -- cannot connect to postgres.

I run docker-compose run web bash, found redis and posgres both cannot be connected.

My docker-compose.yml file

web:
 build: .
  ports:
    - "8000:8000"
environment:
  - 'DATABASE_HOST=db'
  - 'DATABASE_NAME=mydb'
  - 'DATABASE_USER=root'
  - 'DATABASE_PASSWORD=root'
links:
  - db
db:
  image: postgres:9.1

when running sudo docker-compose up i got the following error.

web_1 |   File "/usr/local/lib/python2.7/site packages/django/db/backends/postgresql/base.py", line 175, in  get_new_connection

web_1 |     connection = Database.connect(**conn_params)
web_1 |   File "/usr/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
web_1 |     conn = _connect(dsn, connection_factory=connection_factory, async=async)
web_1 | django.db.utils.OperationalError: could not connect to server: Connection refused
web_1 |     Is the server running on host "localhost" (::1) and accepting
web_1 |     TCP/IP connections on port 5432?
web_1 | could not connect to server: Connection refused
web_1 |     Is the server running on host "localhost" (127.0.0.1) and accepting
web_1 |     TCP/IP connections on port 5432?
4
  • Do you have anything in your Django settings to use those environment variables to define the database? Commented Jul 18, 2016 at 7:20
  • my django settings are: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'aiotadb', 'USER': 'root', 'PASSWORD': 'root', 'HOST': 'db', 'PORT': '5432', } } Commented Jul 18, 2016 at 7:36
  • also tried by changing 'HOST':'localhost' in settings file and changing 'DATABASE_HOST=localhost' in docker-compose.yml .still facing same issue. Commented Jul 18, 2016 at 7:50
  • I'm not sure why you're bothering to set those values in docker-compose.yml, since you aren't using them anywhere; you've hard-coded the settings. That error message is clearly showing that it is trying to access localhost, not db, though, so you should check that you really do have 'HOST': 'db'. Commented Jul 18, 2016 at 8:07

4 Answers 4

2

I also built a clustering with docker-compose, it probably will help you and answer your problem (here is the repo). You can see the docker-compose.yml file, and the django settings file (I marked the lines you need).

You can also clone this repo and get django, angular2, postgresql, and nginx containers, all link together already.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Nir I have tried these also,but still the error is same that Is the server running on host "localhost" (::1) and accepting.I have tried docker using sqlite3 and it worked as a charm.
@AshishK. notice my docker-compose.yml file, my server (django) depends_on database. It means that server container will always wait for database container and spin up after it, and not before. Also, upload your settings.py file, notice the 'HOST': 'database' and'PORT': 5432 in mine (I think that is your problem. your host should be the name of the database container).
Thanks.Found the issue.Running successfully now.
port 5432 is not able to listen anything :)
0

You are linking your web container with the postgres container but you are not defining database name, password and user.

  web:
   build: .
   ports:
    - "8000:8000"
   links:
    - db
  db:
    restart: always
    image: postgres:9.1
    ports:
      - "5432:5432"
    volumes:
      - pgvolume:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=root
      - POSTGRES_DB= aiotadb
      - POSTGRES_USER=root
   data:
    restart: always
    image: postgres:9.1
    volumes:
      - /var/lib/postgresql
    command: tail -f /dev/null

Also, if you already define your database options in your settings file, you don't need to declare it as env variables in web container.

Comments

0
version: '3'
services:
  basicproject:
    build: .
    container_name: basicproject-container
    depends_on:
      - postgres
    ports:
      - "8000:8000"

  postgres:
    image: postgres:9.4
    ports:
    - "5432"
    environment:
      - POSTGRES_USER=test
      - POSTGRES_PASSWORD=testing
      - POSTGRES_DB=test_db

Comments

0

Add dependency in your 'web' service like below:

depends_on:
  - db

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.