我正在尝试使用Django后端和postgresql数据库在Anger上使用Angular 4前端在Docker上部署我的项目。此时我的文件如下所示。我注意到这是否正确完成?我使用heroku container:push web --app myproject
推送它但它不起作用(Logs)。我假设如果我使用Docker,我不必创建Procfile等?可能由于缺少数据库迁移而导致错误吗?
我不知道我正朝着正确的方向前进,但我正在尝试迁移我的数据库。也许我的错误是由于缺乏db引起的?
当我运行heroku run python manage.py migrate
时,我得到:
django.db.utils.OperationalError:无法翻译主机名" db" 地址:名称或服务未知
日志:
2017-07-07T10:27:30.448951+00:00 heroku[web.1]: State changed from crashed to starting
2017-07-07T10:27:30.436282+00:00 heroku[web.1]: Process exited with status 0
2017-07-07T10:27:50.846928+00:00 heroku[web.1]: Starting process with command `python3`
2017-07-07T10:27:53.350381+00:00 heroku[web.1]: Process exited with status 0
2017-07-07T10:27:53.365013+00:00 heroku[web.1]: State changed from starting to crashed
2017-07-07T10:27:53.876208+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host= myproject.herokuapp.com request_id=e1f8edfc-7dc4-4bd3-91be-0a853e948452 fwd="109.173.154.199" dyno= connect= service= status=503 bytes= protocol=https
2017-07-07T10:28:43.444860+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host= myproject.herokuapp.com request_id=361846d1-41cd-403a-989f-4c29a0d1179e fwd="109.173.154.199" dyno= connect= service= status=503 bytes= protocol=https
2017-07-07T10:28:43.984059+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host= myproject.herokuapp.com request_id=658795c8-0ba7-4988-9d39-34601b1334c7 fwd="109.173.154.199" dyno= connect= service= status=503 bytes= protocol=https
2017-07-07T10:28:44.673789+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host= myproject.herokuapp.com request_id=63fbabdc-7ba7-4997-81be-c601a0d83368 fwd="109.173.154.199" dyno= connect= service= status=503 bytes= protocol=https
2017-07-07T10:29:53.091547+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host= myproject.herokuapp.com request_id=a3943544-9dc4-44f0-b788-a462cdfba7d0 fwd="109.173.154.199" dyno= connect= service= status=503 bytes= protocol=https
2017-07-07T10:29:54.419623+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host= myproject.herokuapp.com request_id=eb5191e2-21d9-410c-823e-c57bb0fb7fa4 fwd="109.173.154.199" dyno= connect= service= status=503 bytes= protocol=https
项目树:
├── Backend
│ ├── AI
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-36.pyc
│ │ │ ├── settings.cpython-36.pyc
│ │ │ ├── urls.cpython-36.pyc
│ │ │ └── wsgi.cpython-36.pyc
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ └── manage.py
├── Dockerfile
├── init.sql
├── Frontend
│ └── angularProject
├── Dockerfile
│ └── all files in my angular project
├── docker-compose.yml
└── requirements.txt
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 :(得分:0)
在我看来,你没有把容器链接到需要的地方。如果您希望 django 显示 db ,请将links
指令添加到 docker-compose.yml :
...
django:
build: .
command: python3 MainDirectory/backend/myProject/manage.py runserver 0.0.0.0:8001
volumes:
- .:/code
ports:
- "8001:8001"
depends_on:
- db
links:
- db
这样就可以正确解析 db 主机名。希望。
希望这会有所帮助。 ...
答案 1 :(得分:0)
您应该使用docker networks将容器链接在一起。
这样的事情:
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_USER: aso
POSTGRES_PASSWORD: somepass
networks:
- app
django:
build: .
command: python3 MainDirectory/backend/myProject/manage.py runserver 0.0.0.0:8001
volumes:
- .:/code
ports:
- "8001:8001"
networks:
- app
depends_on:
- db
angular:
build: MainDirectory/frontend
ports:
- "4200:4200"
networks:
- app
depends_on:
- django
networks:
app:
这使您可以更轻松地添加和删除需要通信而不是链接的容器和服务。