简体   繁体   English

Docker不保留postgres卷[django]

[英]Docker not persisting postgres volume [django]

There are many questions that have been asked on here about similar issues that I went through such as this , this , this and this that are very similar but none of the solutions there solve my problem. 关于我经历过的类似问题,这里有很多问题要问,例如thisthisthisthis非常相似,但是那里的解决方案都不能解决我的问题。 Please don't close this question. 请不要关闭这个问题。

Problem: 问题:

I am running django with nginx and postgres on docker. 我在docker上使用nginx和postgres运行django。 Secret information is stored in an .env file. 机密信息存储在.env文件中。 My postgres data is not persisting with docker-compose up/start and docker-compose down/stop/restart . 我的postgres数据无法通过docker-compose up/startdocker-compose down/stop/restart持久化。

This is my docker-compose file: 这是我的docker-compose文件:

version: '3.7'

services:
  web:
    build: ./app
    command: gunicorn umngane_project.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - ./app/:/usr/src/app/
    expose:
      - 8000
    environment:
      - SECRET_KEY=${SECRET}
      - SQL_ENGINE=django.db.backends.postgresql
      - SQL_DATABASE=postgres
      - SQL_USER=${POSTGRESQLUSER}
      - SQL_PASSWORD=${POSTGRESQLPASSWORD}
      - SQL_HOST=db
      - SQL_PORT=5432
      - SU_NAME=${SU_NAME}
      - SU_EMAIL=${SU_EMAIL}
      - SU_PASSWORD=${SU_PASSWORD}
    depends_on:
      - db
  db:
    image: postgres:11.2-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
  nginx:
    build: ./nginx
    volumes:
      - static_volume:/usr/src/app/assets
    ports:
      - 1337:80
    depends_on:
      - web

volumes:
  postgres_data:
    external: true # I tried running without this and the result is the same
  static_volume:

My entrypoint scipt is this: 我的入口点是:

python manage.py flush --no-input
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser --user "${SU_NAME}" --email "${SU_EMAIL}" --password "${SU_PASSWORD}"
python manage.py collectstatic --no-input

exec "$@"

where createsuperuser is a custom module that creates a superuser in the application. 其中createsuperuser是一个自定义模块,可在应用程序中创建一个超级用户。

This setup is not persisting the information in postgres_data . 此设置不会将信息保留在postgres_data

Additional information: 附加信息:

Before doing anything, I check to see that there is no volume named postgres_data using docker volume ls and get just that. 在做任何事情之前,我先检查使用docker volume ls是否没有名为postgres_data docker volume ls ,然后就可以了。

At which point I run docker-compose up -d / docker-compose up -d --build and everything works out fine with no errors. 此时,我运行docker-compose up -d / docker-compose up -d --build ,一切正常,没有错误。

I run docker inspect postgres_data and it shows "CreatedAt": "X1" 我运行"CreatedAt": "X1" docker inspect postgres_data ,它显示"CreatedAt": "X1"

I am able to login as the superuser. 我能够以超级用户身份登录。 I proceed to create admin users, logout as the superuser and then login as any of the admin users with no problem. 我继续创建管理员用户,以超级用户身份注销,然后以任何管理员用户身份登录都没有问题。 I run docker exec -it postgres_data psql -U <postgres_user> to make sure the admin users are in the database and find just that. 我运行docker exec -it postgres_data psql -U <postgres_user>以确保管理员用户在数据库中并找到了。

At which point I proceed to run docker-compose down / docker-compose stop with no problem. 在这一点上我继续运行docker-compose down / docker-compose stop没问题。 I run docker volume ls and it shows that postgres_data is still there. 我运行docker volume ls ,它显示postgres_data仍然存在。

I run docker inspect postgres_data and it shows "CreatedAt": "X2" 我运行"CreatedAt": "X2" docker inspect postgres_data ,它显示"CreatedAt": "X2"

To test that everything works as expected I run docker-compose up -d / docker-compose up -d --build / docker-compose start / docker-compose restart . 为了测试一切正常,我运行docker-compose up -d / docker-compose up -d --build / docker-compose start / docker-compose restart

I run docker inspect postgres_data and it shows "CreatedAt": "X3" 我运行"CreatedAt": "X3" docker inspect postgres_data ,它显示"CreatedAt": "X3"

At which point I proceed to try and login as an admin user and am not able to. 此时,我继续尝试以管理员用户身份登录,但无法登录。 I run docker exec -it postgres_data psql -U <postgres_user> again but this time only see the superuser, no admin users. 我再次运行docker exec -it postgres_data psql -U <postgres_user> ,但这一次只看到超级用户,没有管理员用户。

(Explanation: I am here using the forward slash to show all the different things I tried on different attempts. I tried every combination of commands shown here.) (说明:我在这里使用正斜杠来显示我在不同尝试中尝试过的所有不同内容。我尝试了此处显示的所有命令组合。)

The issue is you run "flush" in your entrypoint script which clears the database. 问题是您在清除数据库的入口点脚本中运行“刷新”。 The entrypoint will run whenever you boot or recreate the container. 每当您启动或重新创建容器时,入口点都将运行。

One way of having persistent data is specifying an actual path on the disk instead of creating a volume: 具有持久性数据的一种方法是在磁盘上指定实际路径,而不是创建卷:

...
  db:
    image: postgres:11.2-alpine
    volumes:
      - "/local/path/to/postgres/data:/var/lib/postgresql/data/"
...

This way, the container's postgres data location is mapped to a path you specify. 这样,容器的postgres数据位置将映射到您指定的路径。 This way, the data persists directly on disk unless purposely deleted. 这样,除非有意删除数据,否则数据将直接保留在磁盘上。 A docker volume, as far as I know, is going to be removed on container removal. 据我所知,将在删除容器时删除一个docker卷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM