This repository contains a Dockerfile and associated scripts for building a PostgreSQL Docker image from an Ubuntu 12.04 LTS base image. This particular PostgreSQL Docker image makes it easy to
- make your database persistent across container restarts; and,
- configure PostgreSQL without changing the image, but instead by passing in arguments when the container is started.
Clone the repository
export IMGTAG="pitrho/precise-postgres"
git clone https://github.com/pitrho/docker-precise-postgres.git
cd docker-precise-postgres
docker build -t $IMGTAG .
Verify you have the image locally
docker images | grep "$IMGTAG"
Start the image, creating an admin user "foo" with password "bar".
PGID=$(docker run -d $IMGTAG -u foo -p bar)
PGIP=$(docker inspect -format='{{.NetworkSettings.IPAddress}}' $PGID)
Now you should be able to connect with psql
as such
psql -h $PGIP -U foo -d postgres
You'll get prompted for a password, enter 'bar'. (Clearly, you will need
to have a PostgreSQL client installed to have the psql
command.)
A few comments:
- The
-u
and-p
parameters are passed to thestart_postgres.sh
shell script, which is theENTRYPOINT
defined in theDockerfile
. - You can see the other configuration options in the
start_postgres.sh
script. - When run in this manner, the PostgreSQL data directory is on the container's union file system. So, when the container is stopped or killed, your data will be deleted.
To persist data, you'll need to use Docker volumes.
On the host system, create a directory that will house the persisted data.
mkdir -p /tmp/pgdata
Now, mount that as a volume when you start up the container and tell PostgreSQL to store its data there
PGID=$(docker run -v /tmp/pgdata/:/tmp/pgdata/ -d $IMGTAG -u foo -p bar -d /tmp/pgdata/)
PGIP=$(docker inspect -format='{{.NetworkSettings.IPAddress}}' $PGID)
Again, you can connect from the host system like
psql -h $PGIP -U foo -d postgres
Go ahead and make some changes, e.g. creating a table, etc. Then, stop the container
docker stop $PGID
Now, let's start it up again. Since we created the foo
user previously, and
our data were persisted in the volume, there's no need to pass the -u
and
-p
parameters this time
PGID=$(docker run -v /tmp/pgdata/:/tmp/pgdata/ -d $IMGTAG -d /tmp/pgdata/)
PGIP=$(docker inspect -format='{{.NetworkSettings.IPAddress}}' $PGID)
Now, when you connect to the PostgreSQL instance, you'll notice the changes you made previously are still present.
You can override each of the following PostgreSQL configuration file locations
data_directory
,config_file
,hba_file
, andident_file
.
To do so, you'll need to put them in a directory that is exposed to the running container as a volume. (The Docker cp command can only copy files from a contain, alas.)
For example, imagine we have a custom PostgreSQL config_file at /tmp/pgconfig/postgresql.conf
and we want to start PostgreSQL using this. We'd start the container like
PGID=$(docker run -v /tmp/pgconfig/:/tmp/pgconfig/ -d $IMGTAG -u foo -p bar -c /tmp/pgconfig/postgresql.conf)
PGIP=$(docker inspect -format='{{.NetworkSettings.IPAddress}}' $PGID)
MIT. See the LICENSE file.
We started with the excellent PostgreSQL Docker image from Discourse.