Skip to content

Is it possible to initialize database while creating the image, NOT when starting the container  #672

Closed
@godomainz

Description

@godomainz

Is it possible to create my custom database while I create the image (docker image build --tag mydb:latest) ?
If I put my sql file to "/docker-entrypoint-initdb.d/" database will execute every time I start the container from that image.I don't want to run the database script every time I start because it is a huge database.

This is for automated testing. If I bind the volume, Database will become persistance. I don't want that either. Because my test cases will fail.

This is my Dockerfile

FROM postgres:10
COPY seed.sql         /tmp/
COPY init-db.sh       /tmp/
WORKDIR /tmp/
RUN ["chmod", "+x", "init-db.sh"]
RUN ./init-db.sh

This is my init-db.sh

#!/bin/bash
set -e
RETRIES=5
cd /usr/lib/postgresql/10/bin/
gosu postgres initdb
su postgres -c './pg_ctl start -D "/var/lib/postgresql/data"'
until psql -U postgres -d postgres -c "select 1" > /dev/null 2>&1 || [ $RETRIES -eq 0 ]; do
  echo "Waiting for postgres server, ${RETRIES}-- remaining attempts..."
  RETRIES=$((RETRIES--))
  sleep 1
done

psql -v ON_ERROR_STOP=1 -U postgres <<-EOSQL
    \i /tmp/seed.sql;
EOSQL

Usng above Dockerfile and init-db.sh I can see my database gets executed when I create the image.
But when I start the container with that image I don't see the database created by seed.sql.
So how can I create my own "postgres docker image" with my own database ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionUsability question, not directly related to an error with the image

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions