在Docker容器中使用PostgreSQL数据库的Django项目 - 目录/ var / lib / postgresql / data存在但不是空的

问题描述 投票:0回答:1

我尝试在Docker容器中运行Django项目。到现在为止,一切都运转良好。我没有改变任何东西,但我在底部显示错误。有什么想法有什么不对?我的PostgreSQL设置以这种方式看待:

DATABASES = {
 "default":
 {
  'ENGINE': 'django.db.backends.postgresql',
  "NAME": 'myproject',
  "HOST": "db",
  "PORT": "5432",
 }
 }

DATABASES = {'default': dj_database_url.config(default='postgres://[email protected]:5432/qdsfks')}

码头工人,compose.yml:

version: '3'

services:
  db:
    image: postgres
    environment:
      POSTGRES_USER: myproject
      POSTGRES_PASSWORD: somepass
    volumes:
      - "./local_pgdata:/var/lib/postgresql/data/pgdata"
  django:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8001
    volumes:
      - .:/code
    ports:
      - "8001:8001"
    depends_on:
      - db

日志:

db_1      | The files belonging to this database system will be owned by user "postgres".
db_1      | This user must also own the server process.
db_1      | 
db_1      | The database cluster will be initialized with locale "en_US.utf8".
db_1      | The default database encoding has accordingly been set to "UTF8".
db_1      | The default text search configuration will be set to "english".
db_1      | 
db_1      | Data page checksums are disabled.
db_1      | 
db_1      | initdb: directory "/var/lib/postgresql/data" exists but is not empty
db_1      | If you want to create a new database system, either remove or empty
db_1      | the directory "/var/lib/postgresql/data" or run initdb
db_1      | with an argument other than "/var/lib/postgresql/data".
python django python-3.x postgresql heroku
1个回答
1
投票

如果要使用现有主机数据库,则只应将主机文件系统映射到docker容器。否则,我建议删除卷映射并使用自定义Dockerfile加载现有内容,如下所述。

./Web/pro即/settings.朋友

改进:您还需要在settings.py中设置USER和PASSWORD

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ['POSTGRES_NAME'],
        'USER': os.environ['POSTGRES_USER'],
        'PASSWORD': os.environ['POSTGRES_PASSWORD'],
        'HOST': os.environ['POSTGRES_HOST'],
        'PORT': os.environ['POSTGRES_PORT'],
    }
}

./docker-compose.阴谋论

db:
    restart: always
    build:
      context: ./db
      dockerfile: Dockerfile
    env_file: .env
    expose:
      - "5432"

django:
    restart: always
    build:
      context: ./web
      dockerfile: Dockerfile
    volumes:
      - .:/app
    command: bash -c "cd ./web && ./manage.py makemigrations && ./manage.py migrate && ./manage.py runserver"
    expose:
      - "8000"
    env_file: .env
    depends_on:
      - db
    links:
      - db

./.恶女

POSTGRES_NAME=db_name
POSTGRES_USER=db_user
POSTGRES_PASSWORD=password
POSTGRES_HOST=db
POSTGRES_PORT=5432

./db/Dockerfile

FROM postgres
RUN mkdir /app
WORKDIR /app
ADD ./ /app/ # add all sources if you need them
ADD ./path-to-your-csv.csv /app/
RUN [import content here]

在这里,您可以找到通过initdb:Load custom sql script加载自定义sql代码的方法。您还将找到一种加载csv文件内容的方法。

此外,您应该考虑使用fixture而不是加载csv或sql脚本。这会更容易。

希望这有助于解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.