Browse code

rewrite the PostgreSQL example using a Dockerfile, and add details to it

Docker-DCO-1.1-Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au> (github: SvenDowideit)

Sven Dowideit authored on 2014/02/07 08:37:10
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,53 @@
0
+#
1
+# example Dockerfile for http://docs.docker.io/en/latest/examples/postgresql_service/
2
+#
3
+
4
+FROM ubuntu
5
+MAINTAINER SvenDowideit@docker.com
6
+
7
+# Add the PostgreSQL PGP key to verify their Debian packages.
8
+# It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc 
9
+RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
10
+
11
+# Add PostgreSQL's repository. It contains the most recent stable release
12
+#     of PostgreSQL, ``9.3``.
13
+RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
14
+
15
+# Update the Ubuntu and PostgreSQL repository indexes
16
+RUN apt-get update
17
+
18
+# Install ``python-software-properties``, ``software-properties-common`` and PostgreSQL 9.3
19
+#  There are some warnings (in red) that show up during the build. You can hide
20
+#  them by prefixing each apt-get statement with DEBIAN_FRONTEND=noninteractive
21
+RUN apt-get -y -q install python-software-properties software-properties-common
22
+RUN apt-get -y -q install postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3
23
+
24
+# Note: The official Debian and Ubuntu images automatically ``apt-get clean``
25
+# after each ``apt-get`` 
26
+
27
+# Run the rest of the commands as the ``postgres`` user created by the ``postgres-9.3`` package when it was ``apt-get installed``
28
+USER postgres
29
+
30
+# Create a PostgreSQL role named ``docker`` with ``docker`` as the password and
31
+# then create a database `docker` owned by the ``docker`` role.
32
+# Note: here we use ``&&\`` to run commands one after the other - the ``\``
33
+#       allows the RUN command to span multiple lines.
34
+RUN    /etc/init.d/postgresql start &&\
35
+    psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
36
+    createdb -O docker docker
37
+
38
+# Adjust PostgreSQL configuration so that remote connections to the
39
+# database are possible. 
40
+RUN echo "host all  all    0.0.0.0/0  md5" >> /etc/postgresql/9.3/main/pg_hba.conf
41
+
42
+# And add ``listen_addresses`` to ``/etc/postgresql/9.3/main/postgresql.conf``
43
+RUN echo "listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf
44
+
45
+# Expose the PostgreSQL port
46
+EXPOSE 5432
47
+
48
+# Add VOLUMEs to allow backup of config, logs and databases
49
+VOLUME	["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
50
+
51
+# Set the default command to run when starting the container
52
+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"]
... ...
@@ -9,152 +9,109 @@ PostgreSQL Service
9 9
 
10 10
 .. include:: example_header.inc
11 11
 
12
-.. note::
13
-
14
-    A shorter version of `this blog post`_.
15
-
16
-.. _this blog post: http://zaiste.net/2013/08/docker_postgresql_how_to/
17
-
18 12
 Installing PostgreSQL on Docker
19 13
 -------------------------------
20 14
 
21
-Run an interactive shell in a Docker container.
22
-
23
-.. code-block:: bash
24
-
25
-    sudo docker run -i -t ubuntu /bin/bash
26
-
27
-Update its dependencies.
28
-
29
-.. code-block:: bash
30
-
31
-    apt-get update
32
-
33
-Install ``python-software-properties``, ``software-properties-common``, ``wget`` and ``vim``.
34
-
35
-.. code-block:: bash
36
-
37
-    apt-get -y install python-software-properties software-properties-common wget vim
38
-
39
-Add PostgreSQL's repository. It contains the most recent stable release
40
-of PostgreSQL, ``9.3``.
41
-
42
-.. code-block:: bash
43
-
44
-    wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
45
-    echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
46
-    apt-get update
47
-
48
-Finally, install PostgreSQL 9.3
15
+Assuming there is no Docker image that suits your needs in `the index`_, you 
16
+can create one yourself.
49 17
 
50
-.. code-block:: bash
18
+.. _the index: http://index.docker.io
51 19
 
52
-    apt-get -y install postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3
20
+Start by creating a new Dockerfile:
53 21
 
54
-Now, create a PostgreSQL superuser role that can create databases and
55
-other roles.  Following Vagrant's convention the role will be named
56
-``docker`` with ``docker`` password assigned to it.
22
+.. note::
57 23
 
58
-.. code-block:: bash
24
+    This PostgreSQL setup is for development only purposes. Refer
25
+    to the PostgreSQL documentation to fine-tune these settings so that it
26
+    is suitably secure.
59 27
 
60
-    su postgres -c "createuser -P -d -r -s docker"
28
+.. literalinclude:: postgresql_service.Dockerfile
61 29
 
62
-Create a test database also named ``docker`` owned by previously created ``docker``
63
-role.
30
+Build an image from the Dockerfile assign it a name.
64 31
 
65 32
 .. code-block:: bash
66 33
 
67
-    su postgres -c "createdb -O docker docker"
34
+    $ sudo docker build -t eg_postgresql .
68 35
 
69
-Adjust PostgreSQL configuration so that remote connections to the
70
-database are possible. Make sure that inside
71
-``/etc/postgresql/9.3/main/pg_hba.conf`` you have following line:
36
+And run the PostgreSQL server container (in the foreground):
72 37
 
73 38
 .. code-block:: bash
74 39
 
75
-    host    all             all             0.0.0.0/0               md5
76
-
77
-Additionaly, inside ``/etc/postgresql/9.3/main/postgresql.conf``
78
-uncomment ``listen_addresses`` like so:
40
+    $ sudo docker run -rm -P -name pg_test eg_postgresql
79 41
 
80
-.. code-block:: bash
42
+There are  2 ways to connect to the PostgreSQL server. We can use 
43
+:ref:`working_with_links_names`, or we can access it from our host (or the network).
81 44
 
82
-    listen_addresses='*'
45
+.. note:: The ``-rm`` removes the container and its image when the container 
46
+          exists successfully.
83 47
 
84
-.. note::
48
+Using container linking
49
+^^^^^^^^^^^^^^^^^^^^^^^
85 50
 
86
-    This PostgreSQL setup is for development only purposes. Refer
87
-    to PostgreSQL documentation how to fine-tune these settings so that it
88
-    is secure enough.
89
-
90
-Exit.
51
+Containers can be linked to another container's ports directly using 
52
+``-link remote_name:local_alias`` in the client's ``docker run``. This will
53
+set a number of environment variables that can then be used to connect:
91 54
 
92 55
 .. code-block:: bash
93 56
 
94
-    exit
95
-
96
-Create an image from our container and assign it a name. The ``<container_id>``
97
-is in the Bash prompt; you can also locate it using ``docker ps -a``.
57
+    $ sudo docker run -rm -t -i -link pg_test:pg eg_postgresql bash
98 58
 
99
-.. code-block:: bash
59
+    postgres@7ef98b1b7243:/$ psql -h $PG_PORT_5432_TCP_ADDR -p $PG_PORT_5432_TCP_PORT -d docker -U docker --password
100 60
 
101
-    sudo docker commit <container_id> <your username>/postgresql
61
+Connecting from your host system
62
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
102 63
 
103
-Finally, run the PostgreSQL server via ``docker``.
64
+Assuming you have the postgresql-client installed, you can use the host-mapped port
65
+to test as well. You need to use ``docker ps`` to find out what local host port the 
66
+container is mapped to first:
104 67
 
105 68
 .. code-block:: bash
106 69
 
107
-    CONTAINER=$(sudo docker run -d -p 5432 \
108
-      -t <your username>/postgresql \
109
-      /bin/su postgres -c '/usr/lib/postgresql/9.3/bin/postgres \
110
-        -D /var/lib/postgresql/9.3/main \
111
-        -c config_file=/etc/postgresql/9.3/main/postgresql.conf')
112
-
113
-Connect the PostgreSQL server using ``psql`` (You will need the
114
-postgresql client installed on the machine.  For ubuntu, use something
115
-like ``sudo apt-get install postgresql-client``).
70
+    $ docker ps
71
+    CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS              PORTS                                      NAMES
72
+    5e24362f27f6        eg_postgresql:latest   /usr/lib/postgresql/   About an hour ago   Up About an hour    0.0.0.0:49153->5432/tcp                    pg_test
73
+    $ psql -h localhost -p 49153 -d docker -U docker --password
116 74
 
117
-.. code-block:: bash
75
+Testing the database
76
+^^^^^^^^^^^^^^^^^^^^
118 77
 
119
-    CONTAINER_IP=$(sudo docker inspect -format='{{.NetworkSettings.IPAddress}}' $CONTAINER)
120
-    psql -h $CONTAINER_IP -p 5432 -d docker -U docker -W
121
-
122
-As before, create roles or databases if needed.
78
+Once you have authenticated and have a ``docker =#`` prompt, you can
79
+create a table and populate it.
123 80
 
124 81
 .. code-block:: bash
125 82
 
126 83
     psql (9.3.1)
127 84
     Type "help" for help.
128 85
 
129
-    docker=# CREATE DATABASE foo OWNER=docker;
130
-    CREATE DATABASE
131
-
132
-Additionally, publish your newly created image on the Docker Index.
86
+    docker=# CREATE TABLE cities (
87
+    docker(#     name            varchar(80),
88
+    docker(#     location        point
89
+    docker(# );
90
+    CREATE TABLE
91
+    docker=# INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');
92
+    INSERT 0 1
93
+    docker=# select * from cities;
94
+         name      | location  
95
+    ---------------+-----------
96
+     San Francisco | (-194,53)
97
+    (1 row)
133 98
 
134
-.. code-block:: bash
99
+Using the container volumes
100
+^^^^^^^^^^^^^^^^^^^^^^^^^^^
135 101
 
136
-    sudo docker login
137
-    Username: <your username>
138
-    [...]
102
+You can use the defined volumes to inspect the PostgreSQL log files and to backup your
103
+configuration and data:
139 104
 
140 105
 .. code-block:: bash
141 106
 
142
-    sudo docker push <your username>/postgresql
143
-
144
-PostgreSQL service auto-launch
145
-
146
-Running our image seems complicated. We have to specify the whole command with
147
-``docker run``. Let's simplify it so the service starts automatically when the
148
-container starts.
149
-
150
-.. code-block:: bash
107
+    docker run -rm --volumes-from pg_test -t -i busybox sh
151 108
 
152
-    sudo docker commit -run='{"Cmd": \
153
-      ["/bin/su", "postgres", "-c", "/usr/lib/postgresql/9.3/bin/postgres -D \
154
-      /var/lib/postgresql/9.3/main -c \
155
-      config_file=/etc/postgresql/9.3/main/postgresql.conf"], "PortSpecs": ["5432"]}' \
156
-      <container_id> <your username>/postgresql
109
+    / # ls
110
+    bin      etc      lib      linuxrc  mnt      proc     run      sys      usr
111
+    dev      home     lib64    media    opt      root     sbin     tmp      var
112
+    / # ls /etc/postgresql/9.3/main/
113
+    environment      pg_hba.conf      postgresql.conf
114
+    pg_ctl.conf      pg_ident.conf    start.conf
115
+    /tmp # ls /var/log
116
+    ldconfig    postgresql
157 117
 
158
-From now on, just type ``docker run <your username>/postgresql`` and
159
-PostgreSQL should automatically start.