简体   繁体   中英

Django with Postgresql on Heroku - Could not translate host name “db” to address: Name or service not known

I deployed on Heroku my project in Docker with Angular 4 frontend, Django backend and Postgresql database. At this moment my files look as shown below. When I open app I get error:

2017-07-11T19:51:14.485577+00:00 app[web.1]:     self.connect()
2017-07-11T19:51:14.485577+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
2017-07-11T19:51:14.485578+00:00 app[web.1]:     six.reraise(dj_exc_type, dj_exc_value, traceback)
2017-07-11T19:51:14.485578+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
2017-07-11T19:51:14.485578+00:00 app[web.1]:     raise value.with_traceback(tb)
2017-07-11T19:51:14.485579+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
2017-07-11T19:51:14.485579+00:00 app[web.1]:     self.connect()
2017-07-11T19:51:14.485579+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 189, in connect
2017-07-11T19:51:14.485580+00:00 app[web.1]:     self.connection = self.get_new_connection(conn_params)
2017-07-11T19:51:14.485580+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
2017-07-11T19:51:14.485580+00:00 app[web.1]:     connection = Database.connect(**conn_params)
2017-07-11T19:51:14.485581+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
2017-07-11T19:51:14.485581+00:00 app[web.1]:     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
2017-07-11T19:51:14.485582+00:00 app[web.1]: django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known

Locally everything seems to be working properly. I use docker exec -ti name /bin/bash then python {path}\\manage.py migrate and database is added.

Maybe there is a problem with my database migration on Heroku?

Procfile:

web: sh -c 'cd PROJECT/backend/project && gunicorn project.wsgi --log-file -'

Project tree:

DockerProject
      ├── Dockerfile
      ├── Procfile
      ├── init.sql
      ├── requirements.txt
      ├── docker-compose.yml
      └── PROJECT
            ├── frontend
                   └── all files
            └── backend
                  └── project
                        ├── prices
                        ├── manage.py
                        └── project
                              └── all backend files

Frontend's Dockerfile:

# Create image based on the official Node 6 image from dockerhub
FROM node:6

# Create a directory where our app will be placed
RUN mkdir -p /usr/src/app

# Change directory so that our commands run inside this new directory
WORKDIR /usr/src/app

# Copy dependency definitions
COPY package.json /usr/src/app

# Install dependecies
RUN npm install

# Get all the code needed to run the app
COPY . /usr/src/app

# Expose the port the app runs in
EXPOSE 4200

# Serve the app
CMD ["npm", "start"]

Main directory's Dockerfile:

FROM python:3.6.1
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip3 install -r requirements.txt
ADD . /code/

docker-compose.yml:

version: '3'

services:
  db:
    image: postgres
    environment:
      POSTGRES_USER: aso
      POSTGRES_PASSWORD: somepass
  django:
    build: .
    command: python3 MainDirectory/backend/myProject/manage.py runserver 0.0.0.0:8001
    volumes:
      - .:/code
    ports:
      - "8001:8001"
    depends_on:
      - db
  angular:
    build: MainDirectory/frontend
    ports:
      - "4200:4200"
    depends_on:
      - django

init.sql:

CREATE USER myUser;
CREATE DATABASE myProject;
GRANT ALL PRIVILEGES ON DATABASE myProject TO myUser;

Your db container, running Postgres, only exists locally. It isn't deployed to Heroku, and in any case Heroku does not support docker-compose, so the Django container has no knowledge of what "db" refers to.

You should use the normal pattern of overriding the database settings via dj-database-url so that in production your app uses the Postgres add-on as specified in the environment variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM