将Postgres数据保存到Docker命名卷中的目录中

时间:2018-12-04 19:45:54

标签: postgresql docker

问题

我有一个使用postgres的应用程序。我希望能够备份初始数据库数据,这样我就不必在每个部署中都重新输入它。但是,尽管在我的撰写文件中设置了命名卷。

我不确定是如何让postgres将其数据保存到与该卷关联的目录中。我也不确定如何将目录与命名卷关联。我想要的是让Docker主机服务器能够在指定的卷的关联目录中查看postgress数据。

有人可以提供一个解释/一些例子来解决这个问题吗?现在,即使该卷与compose文件中的docker服务相关联,它也不会将任何数据写入database_volume /目录。这就是我要解决的问题。

代码

这是我的Dockerfile:

FROM python:3.6

ARG requirements=requirements/production.txt
ENV DJANGO_SETTINGS_MODULE=sasite.settings.production_test

WORKDIR /app

COPY manage.py /app/
COPY requirements/ /app/requirements/ 

RUN pip install -r $requirements

COPY config config
COPY sasite sasite
COPY templates templates
COPY logs logs

ADD /scripts/docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod a+x /docker-entrypoint.sh

EXPOSE 8001

ENTRYPOINT ["/docker-entrypoint.sh"]

CMD ["/usr/local/bin/gunicorn", "--config", "config/gunicorn.conf", "--log-config", "config/logging.conf", "-e", "DJANGO_SETTINGS_MODULE=sasite.settings.production_test", "-w", "4", "-b", "0.0.0.0:8001", "sasite.wsgi:application"]

还有我的docker-compose.yml:

version: "3.2"
services:
  app:
    restart: always
    build:
      context: .
      dockerfile: Dockerfile.prodtest
      args:
        requirements: requirements/production.txt
    container_name: dj01
    environment:
      - DJANGO_SETTINGS_MODULE=sasite.settings.production_test
      - PYTHONDONTWRITEBYTECODE=1
    volumes:
      - ./:/app
      - /static:/static
      - /media:/media
    networks:
      - main
    depends_on:
      - db
  db:
    restart: always
    image: postgres:10.1-alpine
    container_name: ps01
    environment:
      POSTGRES_DB: sasite_db
      POSTGRES_USER: pguser
      POSTGRES_PASSWORD: pguser123
    ports:
      - "5432:5432"
    volumes:
      - database_volume:/var/lib/postgresql/data
    networks:
      - main
  nginx:
    restart: always
    image: nginx
    container_name: ng01
    volumes:
      - ./config/nginx-prodtest.conf:/etc/nginx/conf.d/default.conf:ro
      - ./static:/usr/share/nginx/sasite/static
      - ./media:/usr/share/nginx/sasite/media
    ports:
      - "80:80"
      - "443:443"
    networks:
      - main
    depends_on:
      - app

networks:
  main:

volumes:
  database_volume:
    driver_opts:
      type: none
      device: ./database_volume
      o: bind

0 个答案:

没有答案