对 django postgres 应用程序进行 Dockerizing ,为用户获取密码身份验证失败"root"角色"root"不存在



嗨,我正在对我的django-postgres应用程序进行码头化,而我将用户指定为admin,但我仍然以root身份连接到postgres数据库,并收到错误。

DockerFile

FROM ubuntu:20.04
RUN apt update && apt install python3-pip  python3-dev -y
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
WORKDIR /code
COPY requirements.txt /code/
RUN pip3 install --upgrade pip
RUN apt-get install libffi-dev
RUN pip3 install cffi
RUN pip3 install -r requirements.txt 
COPY ./entrypoint.sh .
RUN sed -i 's/r$//g' /code/entrypoint.sh
RUN chmod +x /code/entrypoint.sh
COPY . /code
RUN python3 manage.py collectstatic --no-input
ENTRYPOINT ["/code/entrypoint.sh"]

入口点.sh

#!/bin/sh
if [ "$DATABASE" = "postgres" ]
then
echo "Waiting for postgres..."
while ! nc -z $SQL_HOST $SQL_PORT; do
sleep 0.1
done
echo "PostgreSQL started"
fi
exec "$@"

docker compose.yml

version: "3.3"

services: 
db_new:
image: postgres:12.0-alpine
container_name: db_new
ports:
- 5432:5432
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=admin
- POSTGRES_DB=docker2


redis:
image: "redis:alpine"

web:
restart: always
container_name: web
build:
context: .
dockerfile: Dockerfile
command: bash -c "/usr/local/bin/daphne -b 0.0.0.0 -p 8000 setup.asgi:application"
volumes:
- static_volume:/code/static/
- media_volume:/code/media/
ports:
- "8000:8000"
env_file:
- ./.env
depends_on:
- db_new
- redis
celery:
build: .
command: /usr/local/bin/celery -A setup worker -l info
depends_on:
- db_new
- redis
celery-beat:
build: .
command: /usr/local/bin/celery -A setup beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler
depends_on:
- db_new
- redis
nginx:
build: ./nginx
volumes:
- static_volume:/code/static/
- media_volume:/code/media/
ports:
- 80:80
depends_on:
- web
volumes:
postgres_data:
static_volume:
media_volume:

错误:

2021-12-17 10:49:19.602 UTC [27] FATAL:  password authentication failed for user "root"
db_new         | 2021-12-17 10:49:19.602 UTC [27] DETAIL:  Role "root" does not exist.
db_new         |        Connection matched pg_hba.conf line 95: "host all all all md5"

伙计们,请帮帮我。谢谢你们的帮助。

当我指定用户为admin时,为什么它连接为root用户。我缺少什么。

你能给我们看看你的.env文件和django设置文件吗?你应该在你的django设置文件中有这样的东西:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'docker2',
'USER': 'admin',
'PASSWORD': 'admin',
'HOST': 'db_new',
'PORT': '5432',
}
}

相关内容

  • 没有找到相关文章

最新更新