Dockerize PostgreSQL

Installing PostgreSQL on Docker

Assuming there is no Docker image that suits your needs on the Docker
Hub, you can create one yourself.

Start by creating a new Dockerfile:

Note:
This PostgreSQL setup is for development-only purposes. Refer to the
PostgreSQL documentation to fine-tune these settings so that it is
suitably secure.

#
# example Dockerfile for https://docs.docker.com/examples/postgresql_service/
#
FROMubuntu# Add the PostgreSQL PGP key to verify their Debian packages.
# It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc
RUNapt-keyadv --keyserverhkp://p80.pool.sks-keyservers.net:80 --recv-keysB97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8# Add PostgreSQL's repository. It contains the most recent stable release
# of PostgreSQL, ``9.3``.
RUNecho"deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list# Install ``python-software-properties``, ``software-properties-common`` and PostgreSQL 9.3
# There are some warnings (in red) that show up during the build. You can hide
# them by prefixing each apt-get statement with DEBIAN_FRONTEND=noninteractive
RUNapt-getupdate && apt-getinstall -ypython-software-propertiessoftware-properties-commonpostgresql-9.3postgresql-client-9.3postgresql-contrib-9.3# Note: The official Debian and Ubuntu images automatically ``apt-get clean``
# after each ``apt-get``
# Run the rest of the commands as the ``postgres`` user created by the ``postgres-9.3`` package when it was ``apt-get installed``
USERpostgres# Create a PostgreSQL role named ``docker`` with ``docker`` as the password and
# then create a database `docker` owned by the ``docker`` role.
# Note: here we use ``&&\`` to run commands one after the other - the ``\``
# allows the RUN command to span multiple lines.
RUN /etc/init.d/postgresqlstart &&\
psql --command"CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
createdb -Odockerdocker# Adjust PostgreSQL configuration so that remote connections to the
# database are possible.
RUNecho"host all all 0.0.0.0/0 md5" >> /etc/postgresql/9.3/main/pg_hba.conf# And add ``listen_addresses`` to ``/etc/postgresql/9.3/main/postgresql.conf``
RUNecho"listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf# Expose the PostgreSQL port
EXPOSE5432# Add VOLUMEs to allow backup of config, logs and databases
VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
# Set the default command to run when starting the container
CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"]

Build an image from the Dockerfile assign it a name.

$ docker build -t eg_postgresql .

Run the PostgreSQL server container (in the foreground):

$ docker run --rm-P--name pg_test eg_postgresql

There are 2 ways to connect to the PostgreSQL server. We can use Link
Containers, or we can access it from our host
(or the network).

Note: The --rm removes the container and its image when
the container exits successfully.

Using container linking

Containers can be linked to another container’s ports directly using
-link remote_name:local_alias in the client’s
docker run. This sets a number of environment
variables that can then be used to connect: