skip to Main Content

I have dockerized a existing django app and an existing postgres database.the tables are created in the docker container. But the tables are empty . There is no data in it.

if I connect to the docker container. And I do a d+

                                                                         
|  Schema |                   Name                   |   Type   |     Owner     | Persistence |    Size    | Description  
| --------+------------------------------------------+----------+---------------+-------------+------------+------------- 
|  public | hotelWelzijnAdmin_animal                | table    | hotelwelzijn | permanent   | 8192 bytes |              
|  public | hotelWelzijnAdmin_animal_id_seq         | sequence | hotelwelzijn | permanent   | 8192 bytes |              
|  public | hotelWelzijnAdmin_category              | table    | hotelwelzijn | permanent   | 8192 bytes |            
|  public | hotelWelzijnAdmin_category_id_seq       | sequence | hotelwelzijn | permanent   | 8192 bytes |              
|  public | accounts_account                         | table    | hotelwelzijn | permanent   | 0 bytes    |             
|  public | accounts_account_groups                  | table    | hotelwelzijn | permanent   | 0 bytes    |              
|  public | accounts_account_groups_id_seq           | sequence | hotelwelzijn | permanent   | 8192 bytes |              
|  public | accounts_account_id_seq                  | sequence | hotelwelzijn | permanent   | 8192 bytes |              
|  public | accounts_account_user_permissions        | table    | hotelwelzijn | permanent   | 0 bytes    |              
|  public | accounts_account_user_permissions_id_seq | sequence | hotelwelzijn | permanent   | 8192 bytes |            
|  public | auth_group                               | table    | hotelwelzijn | permanent   | 0 bytes    |              
|  public | auth_group_id_seq                        | sequence | hotelwelzijn | permanent   | 8192 bytes |                
|  public | auth_group_permissions                   | table    | hotelwelzijn | permanent   | 0 bytes    |               
|  public | auth_group_permissions_id_seq            | sequence | hotelwelzijn | permanent   | 8192 bytes |              
|  public | auth_permission                          | table    | hotelwelzijn | permanent   | 8192 bytes |             
|  public | auth_permission_id_seq                   | sequence | hotelwelzijn | permanent   | 8192 bytes |          

Then the list of tables are shown. But the tables are empty.

Because if I do for example:

welzijn-# select * from accounts_account

no data is shown

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"]

and dockercompose 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
    env_file:
      - ./.env
    ports:
      - '5432:5432'
      
volumes:
    dev-db-data:
    dev-static-data:

And this is the entrypoint.sh code:

#!/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 "$@"

Question: how to copy data from the host to the docker file?

2

Answers


  1. Can you share the entrypoint.sh? Looks like the migrations are applied correctly but there is no data in your tables. You can create some items with python manage.py shell. Also your data is saved because you’re mounting a volume in your db container

    Login or Signup to reply.
  2. I would backup the data from your host postgres and then bring it into the container. there are a few ways to do this.

    You could backup and restore postgres directly: https://www.postgresql.org/docs/current/backup.html (no migrations necessary).

    Or you could do it with Django fixtures: https://docs.djangoproject.com/en/4.2/topics/db/fixtures/

    You’ll mount this file somewhere in you container: https://docs.docker.com/storage/bind-mounts/

    From there you can either load the fixtures or postgres backup as part of the startup command.

    This issue explains it also has some good details:

    Copy Local database table data into docker postgtesql database in Mac osx

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search