Reputation: 1368
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?
Upvotes: 3
Views: 6338
Reputation: 31
Add dependency in your 'web' service like below:
depends_on:
- db
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 22697
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.
Upvotes: 0
Reputation: 2074
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.
Upvotes: 2