Sign inSign up

danhixon/precise-postgres

By danhixon

•Updated about 11 years ago

Postgres 9.3 on Ubuntu 12.04 - Fork of pitrho/docker-precise-postgres

Image
0

394

danhixon/precise-postgres repository overview

⁠Docker Precise Postgres

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.

⁠Building the image

Clone the repository

export IMGTAG="precise-postgres"
git clone https://github.com/danhixon/precise-postgres.git precise-postgres
cd precise-postgres
docker build -t $IMGTAG .

Verify you have the image locally

docker images | grep "$IMGTAG"

⁠Example usage

⁠Basic usage

Start the image, creating an admin user "docker" with password "docker".

PGID=$(docker run -d $IMGTAG)
PGIP=$(docker inspect -format='{{.NetworkSettings.IPAddress}}' $PGID)

Now you should be able to connect with psql as such

psql -h $PGIP -U docker -d postgres

You'll get prompted for a password, enter 'docker'. (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 the start_postgres.sh shell script, which is the ENTRYPOINT defined in the Dockerfile.
  • 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.
⁠Persisting data across container restarts

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 docker -p docker -d /tmp/pgdata/)
PGIP=$(docker inspect -format='{{.NetworkSettings.IPAddress}}' $PGID)

Again, you can connect from the host system like

psql -h $PGIP -U docker -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 docker 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.

⁠Customizing the PostgreSQL configuration

You can override each of the following PostgreSQL configuration file locations⁠

  • data_directory,
  • config_file,
  • hba_file, and
  • ident_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 docker -p docker -c /tmp/pgconfig/postgresql.conf)
PGIP=$(docker inspect -format='{{.NetworkSettings.IPAddress}}' $PGID)

⁠License

MIT. See the LICENSE file.

⁠Acknowledgements

I started with the excellent Precise Postgres Image from Pit Rho⁠.

Tag summary

Content type

Image

Digest

sha256:73e9cb0ac…

Size

105.6 MB

Last updated

about 11 years ago

docker pull danhixon/precise-postgres