如何使用Docker容器化Django应用程序以及PostgreSQL数据库和现有数据?

huangapple go评论90阅读模式
英文:

How to dockerize django app with postgres database and existing data?

问题

我有一个本地的PostgreSQL数据库,并且有两个正在运行的Docker容器:Django应用程序和PostgreSQL数据库。

我已经进行了迁移,表已生成,但表是空的 - 表是空的。

例如,在终端中。

所以这是Dockerfile:

# 拉取官方基础镜像
FROM python:3.9-alpine3.13

# 设置工作目录
WORKDIR /usr/src/app
EXPOSE 8000
# 设置环境变量
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# 安装 psycopg2 依赖
RUN apk update \
    && apk add linux-headers postgresql-dev gcc python3-dev musl-dev

# 安装依赖
RUN pip install --upgrade pip
COPY ./requirements.txt .
COPY ./requirements.dev.txt . 
RUN pip install -r requirements.txt

# 复制 entrypoint.sh
COPY ./entrypoint.sh .
RUN sed -i 's/\r$//g' /usr/src/app/entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh
# 复制项目
COPY . .
# 运行 entrypoint.sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]

docker-compose 文件:

version: '3.9'

services:
  app:
    build:
      context: .
      args:
        - DEV=true
    ports:
      - "8000:8000"
    volumes:
      - .:/app
    command: >
      sh -c "python ./manage.py migrate &&
             python ./manage.py runserver 0:8000"      
    env_file:
      - ./.env

    depends_on:
      - db

  db:
    image: postgres:13-alpine
    container_name: postgres
    volumes:
      - dev-db-data:/var/lib/postgresql/data:/usr/local/var/postgresql/
    env_file:
      - ./.env
    ports:
      - '5432:5432'

volumes:
  dev-db-data:
  dev-static-data:

和 entrypoint.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

python manage.py flush --no-input
python manage.py makemigrations --merge
python manage.py migrate --noinput

exec "$@"

所以当我运行容器时:docker-compose up。

然后我转到:http://127.0.0.1:8000/admin/login/?next=/admin/

我看到了Django的管理面板。但我无法登录。因为表:accounts_account中没有数据

问题:如何从现有的PostgreSQL数据库中获取数据填充到Docker容器中的表格中?

好的,我将SQL文件复制到Docker容器的pgd_snapshots文件夹中。

然后我登录到PostgreSQL:psql -h localhost -U postgres

然后切换到用户psql -h localhost -U animalzijn

然后我看到了这个:

animalzijn=#

接下来我该做什么?

英文:

I have a local postgres database with data in it. And I have two docker containers running:
django app and the postgress database.

And I have done the migrations. the tables are generated, but the tables are empty - the tables are emtpy.

For example in the terminal.

So this is the dockerfile:

# pull official base image
FROM python:3.9-alpine3.13

# set work directory
WORKDIR /usr/src/app
EXPOSE 8000
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apk update \
    && apk add linux-headers postgresql-dev gcc python3-dev musl-dev

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
COPY ./requirements.dev.txt . 
RUN pip install -r requirements.txt

# copy entrypoint.sh
COPY ./entrypoint.sh .
RUN sed -i 's/\r$//g' /usr/src/app/entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh
# copy project
COPY . .
# run entrypoint.sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]

docker-compose file:

version: '3.9'

services:
  app:
   build:
     context: .
     args:
       - DEV=true
   ports:
      - "8000:8000"
   volumes:
      - .:/app
   command: >
      sh -c "python ./manage.py migrate &&      
             python ./manage.py runserver 0:8000"
   env_file:
      - ./.env

   depends_on:
      - db

  db:
    image: postgres:13-alpine
    container_name: postgres
    volumes:
      - dev-db-data:/var/lib/postgresql/data:/usr/local/var/postgresql/
    env_file:
      - ./.env
    ports:
      - '5432:5432'
      
volumes:
    dev-db-data:
    dev-static-data:

and entrypoint.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

python manage.py flush --no-input
python manage.py makemigrations --merge
python manage.py migrate --noinput

exec "$@"

So When I run the containers: docker-compose up.

And I go to: http://127.0.0.1:8000/admin/login/?next=/admin/

I see the admin panel from django. But I can't login. Because the table: accounts_account has no data in it

Question: how to get the data from the existing postgress database in the tables in the docker container?

Oke, I copied the sql file to the folder name pgd_snapshots of the docker container.

and I login in postgres: psql -h localhost -U postgres

and then I switch to user psql -h localhost -U animalzijn
And then I see this:

animalzijn=#

What I have to do next?

答案1

得分: 1

你可以使用Postgres工具pg_dump导出本地数据库。登录到本地实例的SQL Shell,使用pgdump命令将数据库导出到一个SQL文件中(例如):

pg_dump database_name > database_name_20230717.sql

通常,我会将这个SQL文件保存到安装Postgres的位置下的/pg_snapshots目录中。

然后将此SQL文件复制到挂载到Postgres容器的卷内的/pg_snapshots目录中。

登录到容器,切换到postgres用户,然后运行psql来导入数据库:

psql database_name < database_name_20230717.sql

来源:https://www.netguru.com/blog/how-to-dump-and-restore-postgresql-database

英文:

You can export your local database using the postgres tool pg_dump. Login to the SQL Shell for your local instance and use the pgdump command to export the database to a sql file (e.g.):

pg_dump database_name &gt; database_name_20230717.sql

I usually do this to the /pg_snapshots directory which should be under /data wherever you installed postgres

Then copy this sql file to the /pg_snapshots inside the volume mounted to your postgres container.

Login to you container, su to postgres and run psql to import the db:

psql database_name &lt; database_name_20230717.sql

source: https://www.netguru.com/blog/how-to-dump-and-restore-postgresql-database

huangapple
  • 本文由 发表于 2023年7月17日 22:55:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定