github.com/circular-dark/docker@v1.7.0/docs/examples/postgresql_service.md (about)

     1  <!--[metadata]>
     2  +++
     3  title = "Dockerizing PostgreSQL"
     4  description = "Running and installing a PostgreSQL service"
     5  keywords = ["docker, example, package installation,  postgresql"]
     6  [menu.main]
     7  parent = "smn_applied"
     8  +++
     9  <![end-metadata]-->
    10  
    11  # Dockerizing PostgreSQL
    12  
    13  > **Note**: 
    14  > - **If you don't like sudo** then see [*Giving non-root
    15  >   access*](/installation/binaries/#giving-non-root-access)
    16  
    17  ## Installing PostgreSQL on Docker
    18  
    19  Assuming there is no Docker image that suits your needs on the [Docker
    20  Hub](http://hub.docker.com), you can create one yourself.
    21  
    22  Start by creating a new `Dockerfile`:
    23  
    24  > **Note**: 
    25  > This PostgreSQL setup is for development-only purposes. Refer to the
    26  > PostgreSQL documentation to fine-tune these settings so that it is
    27  > suitably secure.
    28  
    29      #
    30      # example Dockerfile for https://docs.docker.com/examples/postgresql_service/
    31      #
    32  
    33      FROM ubuntu
    34      MAINTAINER SvenDowideit@docker.com
    35  
    36      # Add the PostgreSQL PGP key to verify their Debian packages.
    37      # It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc
    38      RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
    39  
    40      # Add PostgreSQL's repository. It contains the most recent stable release
    41      #     of PostgreSQL, ``9.3``.
    42      RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
    43  
    44      # Install ``python-software-properties``, ``software-properties-common`` and PostgreSQL 9.3
    45      #  There are some warnings (in red) that show up during the build. You can hide
    46      #  them by prefixing each apt-get statement with DEBIAN_FRONTEND=noninteractive
    47      RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3
    48  
    49      # Note: The official Debian and Ubuntu images automatically ``apt-get clean``
    50      # after each ``apt-get``
    51  
    52      # Run the rest of the commands as the ``postgres`` user created by the ``postgres-9.3`` package when it was ``apt-get installed``
    53      USER postgres
    54  
    55      # Create a PostgreSQL role named ``docker`` with ``docker`` as the password and
    56      # then create a database `docker` owned by the ``docker`` role.
    57      # Note: here we use ``&&\`` to run commands one after the other - the ``\``
    58      #       allows the RUN command to span multiple lines.
    59      RUN    /etc/init.d/postgresql start &&\
    60          psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
    61          createdb -O docker docker
    62  
    63      # Adjust PostgreSQL configuration so that remote connections to the
    64      # database are possible. 
    65      RUN echo "host all  all    0.0.0.0/0  md5" >> /etc/postgresql/9.3/main/pg_hba.conf
    66  
    67      # And add ``listen_addresses`` to ``/etc/postgresql/9.3/main/postgresql.conf``
    68      RUN echo "listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf
    69  
    70      # Expose the PostgreSQL port
    71      EXPOSE 5432
    72  
    73      # Add VOLUMEs to allow backup of config, logs and databases
    74      VOLUME  ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
    75  
    76      # Set the default command to run when starting the container
    77      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"]
    78  
    79  Build an image from the Dockerfile assign it a name.
    80  
    81      $ docker build -t eg_postgresql .
    82  
    83  And run the PostgreSQL server container (in the foreground):
    84  
    85      $ docker run --rm -P --name pg_test eg_postgresql
    86  
    87  There are 2 ways to connect to the PostgreSQL server. We can use [*Link
    88  Containers*](/userguide/dockerlinks), or we can access it from our host
    89  (or the network).
    90  
    91  > **Note**: 
    92  > The `--rm` removes the container and its image when
    93  > the container exits successfully.
    94  
    95  ### Using container linking
    96  
    97  Containers can be linked to another container's ports directly using
    98  `-link remote_name:local_alias` in the client's
    99  `docker run`. This will set a number of environment
   100  variables that can then be used to connect:
   101  
   102      $ docker run --rm -t -i --link pg_test:pg eg_postgresql bash
   103  
   104      postgres@7ef98b1b7243:/$ psql -h $PG_PORT_5432_TCP_ADDR -p $PG_PORT_5432_TCP_PORT -d docker -U docker --password
   105  
   106  ### Connecting from your host system
   107  
   108  Assuming you have the postgresql-client installed, you can use the
   109  host-mapped port to test as well. You need to use `docker ps`
   110  to find out what local host port the container is mapped to
   111  first:
   112  
   113      $ docker ps
   114      CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS              PORTS                                      NAMES
   115      5e24362f27f6        eg_postgresql:latest   /usr/lib/postgresql/   About an hour ago   Up About an hour    0.0.0.0:49153->5432/tcp                    pg_test
   116      $ psql -h localhost -p 49153 -d docker -U docker --password
   117  
   118  ### Testing the database
   119  
   120  Once you have authenticated and have a `docker =#`
   121  prompt, you can create a table and populate it.
   122  
   123      psql (9.3.1)
   124      Type "help" for help.
   125  
   126      $ docker=# CREATE TABLE cities (
   127      docker(#     name            varchar(80),
   128      docker(#     location        point
   129      docker(# );
   130      CREATE TABLE
   131      $ docker=# INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');
   132      INSERT 0 1
   133      $ docker=# select * from cities;
   134           name      | location
   135      ---------------+-----------
   136       San Francisco | (-194,53)
   137      (1 row)
   138  
   139  ### Using the container volumes
   140  
   141  You can use the defined volumes to inspect the PostgreSQL log files and
   142  to backup your configuration and data:
   143  
   144      $ docker run --rm --volumes-from pg_test -t -i busybox sh
   145  
   146      / # ls
   147      bin      etc      lib      linuxrc  mnt      proc     run      sys      usr
   148      dev      home     lib64    media    opt      root     sbin     tmp      var
   149      / # ls /etc/postgresql/9.3/main/
   150      environment      pg_hba.conf      postgresql.conf
   151      pg_ctl.conf      pg_ident.conf    start.conf
   152      /tmp # ls /var/log
   153      ldconfig    postgresql