我使用Angular 4前端,Django后端和Postgresql数据库在Herer上部署我的Docker项目。此时我的文件如下所示。当我打开应用程序时,我收到错误:
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
本地一切似乎都正常。我使用docker exec -ti name /bin/bash
然后python {path}\manage.py migrate
并添加数据库。
我在Heroku上迁移数据库时可能有问题吗?
Procfile:
web: sh -c 'cd PROJECT/backend/project && gunicorn project.wsgi --log-file -'
项目树:
DockerProject
├── Dockerfile
├── Procfile
├── init.sql
├── requirements.txt
├── docker-compose.yml
└── PROJECT
├── frontend
└── all files
└── backend
└── project
├── prices
├── manage.py
└── project
└── all backend files
Frontend的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"]
主目录的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/
搬运工-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;
答案 0 :(得分:1)
运行Postgres的db
容器只存在于本地。它没有部署到Heroku,无论如何Heroku不支持docker-compose,所以Django容器不知道什么" db"是指。
您应该使用通过dj-database-url覆盖数据库设置的正常模式,以便在生产中您的应用程序使用环境变量中指定的Postgres附加组件。