我正在尝试将我的Django-postgres应用程序使用docker-compose容器化并运行两个命令。
以下是我的Dockerfile:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
我的docker-compose.yml文件如下:
version: '3'
services:
web:
build: .
command: bash -c "python app/manage.py migrate && python app/manage.py runserver 0.0.0.0:8000"
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
db:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: password
我的 settings.py 文件中有以下数据库代码:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'password',
'HOST': 'db',
'PORT': '5432',
}
}
当我运行以下命令时,我的数据库和Web容器已启动:
docker-compose run web python app/manage.py migrate
docker-compose run web python app/manage.py runserver 0.0.0.0:8000
但是当我运行时
docker-compose up
出错了,错误信息如下:
web_1 | django.db.utils.OperationalError: could not connect to server: Connection refused web_1 | Is the server running on host "db" (172.23.0.2) and accepting web_1 | TCP/IP connections on port 5432?
请问有人能帮我找出我错在哪里吗?