我在drf上做了一个小api,但是启动的时候出现错误
FROM ubuntu:22.04
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ARG DEBIAN_FRONTEND=noninteractive
ENV PATH=$PATH:/usr/bin/python3.11
# Установка зависимостей
RUN apt-get update && apt-get install -y --no-install-recommends \
python3.11 \
python3-pip \
postgresql \
postgresql-contrib \
&& \
apt-get clean && rm -rf /var/lib/apt/lists/*
# Обновление pip
RUN pip install --upgrade pip --no-cache-dir
# Установка зависимостей для проекта
COPY requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip install -r requirements.txt --no-cache-dir
# Копирование проекта
COPY . /app/
# Установка прав доступа
RUN chmod -R 755 /app/
# Очистка
RUN rm -rf /var/lib/apt/lists/*
docker-撰写:
version: '3.8'
volumes:
pgdata:
static:
media:
services:
django:
build:
context: .
ports:
- '8000:8000'
container_name: django
env_file:
- docker/env/.env.dev
volumes:
- ./:/app
- static:/app/static
- media:/app/media
depends_on:
- postgres
- redis
command: sh -c "python3 manage.py collectstatic --no-input &&
python3 manage.py makemigrations &&
python3 manage.py migrate &&
python3 manage.py runserver 0.0.0.0:8000"
nginx:
container_name: nginx
working_dir: /app
image: nginx:stable-alpine
restart: always
ports:
- "80:80"
volumes:
- static:/app/static
- media:/app/media
- ./docker/nginx/dev/:/etc/nginx/conf.d:ro
links:
- django
depends_on:
- django
postgres:
image: postgres:alpine
container_name: postgres
restart: always
env_file:
- docker/env/.env.dev
volumes:
- pgdata:/var/lib/postgresql/data/
environment:
POSTGRES_PASSWORD: example
redis:
image: redis:alpine
container_name: redis
env_file:
- docker/env/.env.dev
expose:
- 6379
volumes:
- ./docker/redis/data:/data
我做错了什么?错误:
django | connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
django | Is the server running on that host and accepting TCP/IP connections?
设置.py:
DATABASES = {
'default': {
'ENGINE': getenv("DB_ENGINE"),
'NAME': getenv("DB_NAME"),
'USER': getenv("DB_USER"),
'PASSWORD': getenv("DB_PASSWORD"),
'HOST': getenv("DB_HOST"),
'PORT': getenv("DB_PORT"),
}
}
.env:
DB_ENGINE=django.db.backends.postgresql
DB_NAME=remnovo
DB_USER=postgres
DB_PASSWORD=qwerty
DB_HOST=localhost
DB_PORT=5432