I'm setting up an application in Docker that uses Django and Postgres. I'm unable to connect to Postgres from the app. When I run either:
docker-compose run web python manage.py runserverdocker-compose run web python manage.py migrate
I get the following error:
django.db.utils.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Here's my Dockerfile:
FROM python:2.7
ADD requirements.txt /app/requirements.txt
WORKDIR /app/
RUN pip install -r requirements.txt
My docker-compose.yml:
version: '2'
services:
db:
image: postgres:9.4
hostname: db
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=postgres
ports:
- "5432:5432"
web:
build:
context: .
dockerfile: Dockerfile
hostname: web
volumes:
- .:/app
ports:
- "8000:8000"
links:
- db
depends_on:
- db
command:
python manage.py runserver 0.0.0.0:8000
and the database settings in Django:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
docker ps -a shows:
e9df7e1644ce web "python manage.py ..." About a minute ago Up About a minute 0.0.0.0:8000->8000/tcp web_1
60801d3256e4 postgres:9.4 "docker-entrypoint..." About a minute ago Up About a minute 0.0.0.0:5432->5432/tcp db_1
Django v1.10.5
Docker for Mac v1.13.1
docker-compose v 1.11.1
Here are the logs:
depends_on: - dbwould prevent the web container from being spun up before the db container, no?Attached to web_1, db_1, and then it's just a bunch of logs fromdb_1.links however. Worth checking it out anyway. Can you attach the logs to the question so that anyone looking at it can understand the situation better?