14

I'm new to Docker, and I'm trying to put my Django rest API in a container with Nginx, Gunicorn and Postgres, using docker-compose and docker-machine. Following this tutorial: https://realpython.com/blog/python/django-development-with-docker-compose-and-machine/

Most of my code is the same as the tutorial's (https://github.com/realpython/dockerizing-django). with some minor name changes.

this my docker-compose.yml (I changed the gunicorn command to runserver for debugging purposes)

web: restart: always build: ./web expose: - "8000" links: - postgres:postgres - redis:redis volumes: - /usr/src/app - /usr/src/app/static env_file: .env environment: DEBUG: 'true' command: /usr/local/bin/python manage.py runserver nginx: restart: always build: ./nginx/ ports: - "80:80" volumes: - /www/static volumes_from: - web links: - web:web postgres: restart: always image: postgres:latest ports: - "5432:5432" volumes: - pgdata:/var/lib/postgresql/data/ redis: restart: always image: redis:latest ports: - "6379:6379" volumes: - redisdata:/data 

And this is in my settings.py of Django:

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'postgres', 'USER': 'postgres', 'PASSWORD': 'postgres', 'HOST': 'postgres', 'PORT': '5432', } } 

Nginx and postgres (and redis) are up and running, however my django server wont start, by this error:

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? 

I've googled a lot and I've verified that postgres is running, on port 5432, I can connect to it using psql command .

I am lost. What is my mistake?

EDIT: It appears that it is not using my settings.py file or something, since it's asking if the server is running on localhost, while settings should be looking for postgres.

3 Answers 3

17

When those who have this problem please check your settings.py:

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'dbname', 'USER': 'user', 'PASSWORD': 'password', 'HOST': 'db' } } 

Your HOST:'db' and docker-compose file db name should be same. If you want to rename from db, make sure that you change in docker-compose file and setting.py:

db: restart: always image: postgres:latest ports: - "5432:5432" volumes: - pgdata:/var/lib/postgresql/data/ 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man! Finally I got it working after spending almost an entire day into this.
1

Checkout your manage.py, there should be a line

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") 

if there is no such line, put it

set your DJANGO_SETTINGS_MODULE with respect to PYTHONPATH.

UPD i cloned your repo and launched the web service by changing command in docker-compose.yml

- command: /usr/local/bin/gunicorn docker_django.wsgi:application -w 2 -b :8000 + command: python manage.py runserver 0.0.0.0:8000 

I'm sure DJANGO_SETTINGS_MODULE is correct.

2 Comments

Thanks for the reply, but the line is there! Everything works locally.
@Your update, it is correct. And the repo I mentioned works out of the box for me :). I've managed to get it working, however I'm not sure how exactly.
0

I'm exactly facing the same issue, while runing my django app with docker on aws ec2 instance.

I noticed that this error only happend for the first time the docker image is build, so to fix i juste ran :

CTRL + C then docker-compose up again and everything worked fine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.