Search code examples
postgresqldockerdocker-composepg

I need to build docker image with postgres with data included


So, I need docker image with: my program that use postgresql, postgresql and some data in it I use this article: https://medium.com/@sharmaNK/build-postgres-docker-image-with-data-included-489bd58a1f9e

My steps:

  1. docker-compose build //Creates ps_image_with_data
  2. docker run --name ps-export -p 5432:5432 -i postgres
  3. Connect to 5432 db, using dbeaver; create table; put some rows in the table
  4. docker exec -it my_container_with_data_id bash
  5. mkdir /postgres
  6. cp -r /var/lib/postgresql/data/* /postgres
  7. docker commit ps-export ps_image_with_data
  8. docker run --env PGDATA=postgres -p 5432:5432 -i ps_image_with_data // here I've tried both ps_image_with_data and new image that was created on step 7

And after 8 step I got this:

2018-11-08 12:44:52.549 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 2018-11-08 12:44:52.549 UTC [1] LOG: listening on IPv6 address "::", port 5432 2018-11-08 12:44:52.615 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" 2018-11-08 12:44:52.750 UTC [22] LOG: database system was interrupted; last known up at 2018-11-08 11:56:27 UTC 2018-11-08 12:45:25.042 UTC [22] LOG: could not remove cache file "global/pg_internal.init": Permission denied 2018-11-08 12:45:25.042 UTC [22] LOG: could not remove cache file "base/13067/pg_internal.init": Permission denied 2018-11-08 12:45:25.042 UTC [22] LOG: database system was not properly shut down; automatic recovery in progress 2018-11-08 12:45:25.127 UTC [22] LOG: redo starts at 0/166CF68 2018-11-08 12:45:25.127 UTC [22] LOG: invalid record length at 0/166D048: wanted 24, got 0 2018-11-08 12:45:25.127 UTC [22] LOG: redo done at 0/166D010 2018-11-08 12:45:25.259 UTC [22] PANIC: could not rename file "pg_logical/replorigin_checkpoint.tmp" to "pg_logical/replorigin_checkpoint": Permission denied 2018-11-08 12:45:25.357 UTC [1] LOG: startup process (PID 22) was terminated by signal 6: Aborted 2018-11-08 12:45:25.358 UTC [1] LOG: aborting startup due to startup process failure 2018-11-08 12:45:25.359 UTC [1] LOG: database system is shut down

Can someone help me to sort it out?


Solution

  • How about

    1. docker run --name ps-export -p 5432:5432 -i postgres

    2. Connect to 5432 db, using dbeaver; create table; put some rows in the table

    3. Make a dump (data.sql) to your host

    4. Add in your docker-compose.yml, under the psql server

    `

    volumes:
    - ./data.sql:/docker-entrypoint-initdb.d/init.sql
    
    1. docker-compose up

    If you want to include data.sql in the image, instead of step 4, copy it to /docker-entrypoint-initdb.d/init.sql in your dockerfile.