docker-compose does not start postgres

django postgresql docker-compose

1389 просмотра

2 ответа

683 Репутация автора

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?
Автор: Ashish K. Источник Размещён: 18.07.2016 05:57

Ответы (2)


2 плюса

807 Репутация автора

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.

Автор: Nir Размещён: 18.07.2016 08:47

0 плюса

15466 Репутация автора

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.

Автор: levi Размещён: 09.08.2016 11:53
Вопросы из категории :
32x32