1

I want to build postgres docker container for testing some issue. I have:

  1. Archived folder of postgres files(/var/lib/postgres/data/)

  2. Dockerfile that place folder into doccker postgres:latest.

I want:

  1. Docker image that reset self-state after recreate image.

  2. Container that have database state based on passed into the container postgres files

  3. I don't want to wait for a long time operation of backup and restore existing database in /docker-entrypoint-initdb.d initialization script.

  4. I DON'T WANT TO USE VOLUMES because I don't need to store new data between restart (That's why this post is different from How to use a PostgreSQL container with existing data?. In that post volumes are used)

My suggestion is to copy postgres files(/var/lib/postgres/data/) from host machine into docker's /var/lib/postgres/data/ in build phase.

But postgres docker replace this files when initdb phase is executing.

How to ask Postgres docker not overriding database files?

e.g. Dockerfile

FROM postgres:latest


COPY ./postgres-data.tar.gz /opt/pg-data/
WORKDIR /opt/pg-data
RUN tar -xzf postgres-data.tar.gz
RUN mv ./data/ /var/lib/postgresql/data/pg-data/

Run command

docker run -p 5432:5432 -e PGDATA=/var/lib/postgresql/data/pg-data --name database-immage1 database-docker
theSemenov
  • 161
  • 1
  • 11
  • Possible duplicate of [How to use a PostgreSQL container with existing data?](https://stackoverflow.com/questions/35679995/how-to-use-a-postgresql-container-with-existing-data) – LinPy Sep 03 '19 at 11:25
  • 1
    It is not the same as the previous post link. In that post they use a) volumes b) make dump of db and c) restore from dump. I don't want that step because is tedious for 30 database with its own user and make dump it is not so fast. I have an archive with database directory and I have to replace it inside the docker. They restore dump when image running, I have to restore dump when container build because I don't have to spend much time until database restore every time when I am restoring the container – theSemenov Sep 03 '19 at 12:32

1 Answers1

2

If you don't really need to create a custom image with the database snapshot you could use volumes. Un-tar the database files somewhere on the host say ~/pgdata then run the image. Example:

docker run -v ~/pgdata:/var/lib/postgresql/data/ -p 5432:5432 postgres:9.5

The files must be compatible with the postgres version of the image so use the same image version as the archived database.

If, instead, you must recreate the image you don't need to uncompress the database archive. The ADD instruction will do that for you. Make sure the tar does not contain any leading directory.

The Dockerfile:

FROM postgres:latest
ADD ./postgres-data.tar.gz /var/lib/postgresql/data/

Build it:

docker build . -t database-docker

Run without overriding the environment variable PGDATA. Note that you copy the files in /var/lib/postgresql/data but the PGDATA points to /var/lib/postgresql/data/pg-data.

Run the container:

docker run -p 5432:5432 --name  database-image1 database-docker
b0gusb
  • 3,160
  • 2
  • 8
  • 25
  • As I write in question I don't want to use volume. I want to leave all extra data after I recreate the image – theSemenov Sep 03 '19 at 22:28