= Running your database with data included
*Having to insert the data everytime you start a container can become a hassle. In this exercise we will put the data inside the Postgres image, so that it will be available on startup.*

The https://hub.docker.com/_/postgres/[Postgres] image provides a convenient way to execute scripts on startup of your database. All we have to do is add a .sh script to the docker-entrypoint-initdb.d-folder as per the documentation of the image.

== Steps to prepare the database
. Create the .sh script on your harddisk
. Create a Dockerfile that
.. Is build https://docs.docker.com/engine/reference/builder/#/from[from] postgres:9
.. https://docs.docker.com/engine/reference/builder/#/add[Adds] the .sh file to the docker-entrypoint-initdb.d folder in the container
. Build the image, run it and link it with the testnetapp container


.The script to add to the Dockerfile
----
#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
    CREATE DATABASE testnet;
    \c testnet
    CREATE TABLE kv (key varchar(100) PRIMARY KEY, value varchar(100));
    INSERT INTO kv VALUES ('provider','postgres database');
EOSQL
----
= Running your database with data included
*Having to insert the data everytime you start a container can become a hassle. In this exercise we will put the data inside the Postgres image, so that it will be available on startup.*

The https://hub.docker.com/_/postgres/[Postgres] image provides a convenient way to execute scripts on startup of your database. All we have to do is add a .sh script to the docker-entrypoint-initdb.d-folder as per the documentation of the image.

== Steps to prepare the database
. Create an empty folder
. Put the .sh script from below in that folder
. Create a Dockerfile in the folder that
.. Is build https://docs.docker.com/engine/reference/builder/#/from[from] postgres:9
.. https://docs.docker.com/engine/reference/builder/#/add[Adds] the .sh file to the docker-entrypoint-initdb.d folder in the container
. Build the image, run it and link it with the testnetapp container


.The script to add to the Dockerfile
----
#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
    CREATE DATABASE testnet;
    \c testnet
    CREATE TABLE kv (key varchar(100) PRIMARY KEY, value varchar(100));
    INSERT INTO kv VALUES ('provider','postgres database');
EOSQL
----
Create Dockerfile in empty folder
Create Dockerfile in empty folder
