A Dockerfile that produces a container that will run PostgreSQL.
This example creates the image with the tag paintedfox/postgresql
, but you can
change this to use your own username.
$ docker build -t="paintedfox/postgresql" .
Alternately, you can run the following if you have make installed...
$ make
You can also specify a custom docker username like so:
$ make DOCKER_USER=paintedfox
The PostgreSQL server is configured to store data in /data
inside the
container. You can map the container's /data
volume to a volume on the host
so the data becomes independant of the running container. There is also an
additional volume at /var/log/postgresql
which exposes PostgreSQL's logs.
This example uses /tmp/postgresql
to store the PostgreSQL data, but you can
modify this to your needs.
When the container runs, it creates a superuser with a random password. You
can set the username and password for the superuser by setting the container's
environment variables. This lets you discover the username and password of the
superuser from within a linked container or from the output of docker inspect postgresql
.
If you set DB=database_name, when the container runs it will create a new database with the USER having full ownership of it.
$ mkdir -p /tmp/postgresql
$ docker run -d --name="postgresql" \
-p 127.0.0.1:5432:5432 \
-v /tmp/postgresql:/data \
-e USER="super" \
-e DB="database_name" \
-e PASS="$(pwgen -s -1 16)" \
paintedfox/postgresql
Alternately, you can run the following if you have make installed...
$ make run
You can also specify a custom port to bind to on the host, a custom data directory, and the superuser username and password on the host like so:
$ sudo mkdir -p /srv/docker/postgresql
$ make run PORT=127.0.0.1:5432 \
DATA_DIR=/srv/docker/postgresql \
USER=super \
PASS=$(pwgen -s -1 16)
To connect to the PostgreSQL server, you will need to make sure you have
a client. You can install the postgresql-client
on your host machine by
running the following (Ubuntu 12.04LTS):
$ sudo apt-get install postgresql-client
As part of the startup for PostgreSQL, the container will generate a random password for the superuser. To view the login in run `docker logs