Reputation: 9517
I have created a Dockerfile based on the https://index.docker.io/u/nornagon/postgres/ image. I want to create a Postgres database docker image as part of my CI process.
Here is my Dockerfile:
FROM nornagon/postgres
ADD sql sql
ADD create_db.sh /src/
USER postgres
RUN /src/create_db.sh
CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"]
Here is the create_db.sh bash script that the RUN command executes:
#! /bin/bash
/etc/init.d/postgresql start
psql --command "CREATE USER queuespy WITH SUPERUSER PASSWORD 'queuespy';"
createdb -O queuespy queuespy
psql -l
for i in /sql/tables/*.sql
do
psql --file=$i --dbname=queuespy
done
for i in /sql/static_data/*.sql
do
psql --file=$i --dbname=queuespy
done
/etc/init.d/postgresql stop
When I build the Dockerfile, it all seems to work fine. I get this output showing that the psql commands all executed successfully:
$ docker build -t queuespy/db .
Uploading context 45.57 kB
Uploading context
Step 0 : FROM nornagon/postgres
---> b60632eb52bf
Step 1 : MAINTAINER [email protected]
---> Using cache
---> 75fd9054927f
Step 2 : ADD sql sql
---> Using cache
---> 3c1bfe677e42
Step 3 : ADD create_db.sh /src/
---> bef6f8729c2a
Step 4 : USER postgres
---> Running in 4280e300d7f3
---> ce7a940abcd7
Step 5 : RUN /src/create_db.sh
---> Running in 196ead143566
uid=102(postgres) gid=105(postgres) groups=105(postgres),104(ssl-cert)
* Starting PostgreSQL 9.3 database server
...done.
CREATE ROLE
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
docker | docker | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
queuespy | queuespy | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(5 rows)
CREATE TABLE
INSERT 0 1
INSERT 0 1
INSERT 0 1
* Stopping PostgreSQL 9.3 database server
...done.
---> 882102c8256c
Successfully built 882102c8256c
I can run the image OK:
$ docker run -d -p 5432:5432 --name queuespy_db queuespy/db
But when I list the databases and users there's no sign of the ones I created in my Dockerfile, only the docker user created in the nornagon/postgres image?
Upvotes: 2
Views: 4227
Reputation: 4598
The problem is that the default location for the data directory is configured as a VOLUME in the base image. It seems the maintainer changed this shortly after you posted your question. Changes to the file system within VOLUME directories are ignored by Docker, as far as image commits are concerned.
I ran into this problem with the stackbrew Postgres image. To be able to create a new image containing postgres + custom schema, a new non-VOLUME data directory is needed. Postgres also needs to be configured to use this directory. In stackbrew's version, this is as simple as overriding the PGDATA environment variable in the custom Dockerfile:
FROM postgres
# Original 'PGDATA' is /var/lib/postgresql/data
RUN mkdir -p /var/lib/pgdata
ENV PGDATA /var/lib/pgdata
# RUN ... logic to create schema
Upvotes: 3