Git Product home page Git Product logo

docker-precise-postgres's Introduction

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="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"

Example usage

Basic usage

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 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 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.

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 foo -p bar -c /tmp/pgconfig/postgresql.conf)
PGIP=$(docker inspect -format='{{.NetworkSettings.IPAddress}}' $PGID)

License

MIT. See the LICENSE file.

Acknowledgements

We started with the excellent PostgreSQL Docker image from Discourse.

Contributors

docker-precise-postgres's People

Contributors

kljensen avatar

Watchers

Dinesh  Bhat avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.