A Dockerfile that produces a container that will run PostgreSQL on centos6
The work is based on famous paintedfox/postgresql, I forked to have it for centos based
- CentOS 6.5 is used as latest ( CentOS 6.4 has small issues for
- SSH daemon is added
- pwgen is removed
- /initdb for first run, no database is created during initialized
This example creates the image with the tag
larrycai/postgresql, but you can
change this to use your own username.
$ docker build -t="larrycai/postgresql" .
Alternately, you can run the following if you have make installed.
You can also specify a custom docker username like so:
$ make DOCKER_USER=larrycai
Container Creation / Running
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.
(TODO) 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
If you set DB=database_name, when the container runs it will create a new
database with the USER having full ownership of it.
Initialize the db
In order to use new data, the container needs to be initialzed
$ mkdir -p /tmp/postgresql $ docker run -p 5432:5432 --name="postgresql" -i -t -v /tmp/postgresql:/data -e DB="db" -e USER="user" -e PASS="user" larrycai/postgresql /initdb
Actually it moves the initialized the db to new volume
cp -R /var/lib/pgsql/9.3/data/* $DATA_DIR
if you want to re-initialized (reset) the db, please remove
$ mkdir -p /tmp/postgresql $ docker run -d --name="postgresql" -p 5432:5432 -P -v /tmp/postgresql:/data larrycai/postgresql
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:
Connecting to the Database
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
Linking with the Database Container
You can link a container to the database container. You may want to do this to
keep web application processes that need to connect to the database in
a separate container.
To demonstrate this, we can spin up a new container like so:
$ docker run -t -i --link postgresql:db ubuntu bash
This assumes you're already running the database container with the name
--link postgresql:db will give the linked container the
alias db inside of the new container.
From the new container you can connect to the database by running the following
$ apt-get install -y postgresql-client $ psql -U postgres \ -h "$DB_PORT_5432_TCP_ADDR" \ -p "$DB_PORT_5432_TCP_PORT"
If you ran the postgresql container with the flags
-e USER=<user> and
PASS=<pass>, then the linked container should have these variables available
in its environment. Since we aliased the database container with the name
db, the environment variables from the database container are copied into the
linked container with the prefix
several dockerfiles are referred