| ... | ... |
@@ -1,6 +1,6 @@ |
| 1 | 1 |
:title: Docker Examples |
| 2 | 2 |
:description: Examples on how to use Docker |
| 3 |
-:keywords: docker, hello world, node, nodejs, python, couch, couchdb, redis, ssh, sshd, examples |
|
| 3 |
+:keywords: docker, hello world, node, nodejs, python, couch, couchdb, redis, ssh, sshd, examples, postgresql |
|
| 4 | 4 |
|
| 5 | 5 |
|
| 6 | 6 |
|
| ... | ... |
@@ -20,3 +20,4 @@ Contents: |
| 20 | 20 |
running_redis_service |
| 21 | 21 |
running_ssh_service |
| 22 | 22 |
couchdb_data_volumes |
| 23 |
+ postgresql_service |
| 23 | 24 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,149 @@ |
| 0 |
+:title: PostgreSQL service How-To |
|
| 1 |
+:description: Running and installing a PostgreSQL service |
|
| 2 |
+:keywords: docker, example, package installation, postgresql |
|
| 3 |
+ |
|
| 4 |
+.. _postgresql_service: |
|
| 5 |
+ |
|
| 6 |
+PostgreSQL Service |
|
| 7 |
+================== |
|
| 8 |
+ |
|
| 9 |
+.. note:: |
|
| 10 |
+ |
|
| 11 |
+ A shorter version of `this blog post`_. |
|
| 12 |
+ |
|
| 13 |
+.. _this blog post: http://zaiste.net/2013/08/docker_postgresql_how_to/ |
|
| 14 |
+ |
|
| 15 |
+Installing PostgreSQL on Docker |
|
| 16 |
+------------------------------- |
|
| 17 |
+ |
|
| 18 |
+For clarity I won't be showing commands output. |
|
| 19 |
+ |
|
| 20 |
+Run an interactive shell in Docker container. |
|
| 21 |
+ |
|
| 22 |
+.. code-block:: bash |
|
| 23 |
+ |
|
| 24 |
+ docker run -i -t base /bin/bash |
|
| 25 |
+ |
|
| 26 |
+Update its dependencies. |
|
| 27 |
+ |
|
| 28 |
+.. code-block:: bash |
|
| 29 |
+ |
|
| 30 |
+ apt-get update |
|
| 31 |
+ |
|
| 32 |
+Install ``python-software-properies``. |
|
| 33 |
+ |
|
| 34 |
+.. code-block:: bash |
|
| 35 |
+ |
|
| 36 |
+ apt-get install python-software-properties |
|
| 37 |
+ apt-get install software-properties-common |
|
| 38 |
+ |
|
| 39 |
+Add Pitti's PostgreSQL repository. It contains the most recent stable release |
|
| 40 |
+of PostgreSQL i.e. ``9.2``. |
|
| 41 |
+ |
|
| 42 |
+.. code-block:: bash |
|
| 43 |
+ |
|
| 44 |
+ add-apt-repository ppa:pitti/postgresql |
|
| 45 |
+ apt-get update |
|
| 46 |
+ |
|
| 47 |
+Finally, install PostgreSQL 9.2 |
|
| 48 |
+ |
|
| 49 |
+.. code-block:: bash |
|
| 50 |
+ |
|
| 51 |
+ apt-get -y install postgresql-9.2 postgresql-client-9.2 postgresql-contrib-9.2 |
|
| 52 |
+ |
|
| 53 |
+Now, create a PostgreSQL superuser role that can create databases and other roles. |
|
| 54 |
+Following Vagrant's convention the role will be named `docker` with `docker` |
|
| 55 |
+password assigned to it. |
|
| 56 |
+ |
|
| 57 |
+.. code-block:: bash |
|
| 58 |
+ |
|
| 59 |
+ sudo -u postgres createuser -P -d -r -s docker |
|
| 60 |
+ |
|
| 61 |
+Create a test database also named ``docker`` owned by previously created ``docker`` |
|
| 62 |
+role. |
|
| 63 |
+ |
|
| 64 |
+.. code-block:: bash |
|
| 65 |
+ |
|
| 66 |
+ sudo -u postgres createdb -O docker docker |
|
| 67 |
+ |
|
| 68 |
+Adjust PostgreSQL configuration so that remote connections to the database are |
|
| 69 |
+possible. Make sure that inside ``/etc/postgresql/9.2/main/pg_hba.conf`` you have |
|
| 70 |
+following line: |
|
| 71 |
+ |
|
| 72 |
+.. code-block:: bash |
|
| 73 |
+ |
|
| 74 |
+ host all all 0.0.0.0/0 md5 |
|
| 75 |
+ |
|
| 76 |
+Additionaly, inside ``/etc/postgresql/9.2/main/postgresql.conf`` uncomment |
|
| 77 |
+``listen_address`` so it is as follows: |
|
| 78 |
+ |
|
| 79 |
+.. code-block:: bash |
|
| 80 |
+ |
|
| 81 |
+ listen_address='*' |
|
| 82 |
+ |
|
| 83 |
+*Note:* this PostgreSQL setup is for development only purposes. Refer to |
|
| 84 |
+PostgreSQL documentation how to fine-tune these settings so that it is enough |
|
| 85 |
+secure. |
|
| 86 |
+ |
|
| 87 |
+Create an image and assign it a name. ``<container_id>`` is in the Bash prompt; |
|
| 88 |
+you can also locate it using ``docker ps -a``. |
|
| 89 |
+ |
|
| 90 |
+.. code-block:: bash |
|
| 91 |
+ |
|
| 92 |
+ docker commit <container_id> <your username>/postgresql |
|
| 93 |
+ |
|
| 94 |
+Finally, run PostgreSQL server via ``docker``. |
|
| 95 |
+ |
|
| 96 |
+.. code-block:: bash |
|
| 97 |
+ |
|
| 98 |
+ CONTAINER=$(docker run -d -p 5432 \ |
|
| 99 |
+ -t <your username>/postgresql \ |
|
| 100 |
+ /bin/su postgres -c '/usr/lib/postgresql/9.2/bin/postgres \ |
|
| 101 |
+ -D /var/lib/postgresql/9.2/main \ |
|
| 102 |
+ -c config_file=/etc/postgresql/9.2/main/postgresql.conf') |
|
| 103 |
+ |
|
| 104 |
+Connect the PostgreSQL server using ``psql``. |
|
| 105 |
+ |
|
| 106 |
+.. code-block:: bash |
|
| 107 |
+ |
|
| 108 |
+ CONTAINER_IP=$(docker inspect $CONTAINER | grep IPAddress | awk '{ print $2 }' | tr -d ',"')
|
|
| 109 |
+ psql -h $CONTAINER_IP -p 5432 -d docker -U docker -W |
|
| 110 |
+ |
|
| 111 |
+As before, create roles or databases if needed. |
|
| 112 |
+ |
|
| 113 |
+.. code-block:: bash |
|
| 114 |
+ |
|
| 115 |
+ psql (9.2.4) |
|
| 116 |
+ Type "help" for help. |
|
| 117 |
+ |
|
| 118 |
+ docker=# CREATE DATABASE foo OWNER=docker; |
|
| 119 |
+ CREATE DATABASE |
|
| 120 |
+ |
|
| 121 |
+Additionally, publish there your newly created image on Docker Index. |
|
| 122 |
+ |
|
| 123 |
+.. code-block:: bash |
|
| 124 |
+ |
|
| 125 |
+ docker login |
|
| 126 |
+ Username: <your username> |
|
| 127 |
+ [...] |
|
| 128 |
+ |
|
| 129 |
+.. code-block:: bash |
|
| 130 |
+ |
|
| 131 |
+ docker push <your username>/postgresql |
|
| 132 |
+ |
|
| 133 |
+PostgreSQL service auto-launch |
|
| 134 |
+------------------------------ |
|
| 135 |
+ |
|
| 136 |
+Running our image seems complicated. We have to specify the whole command with |
|
| 137 |
+``docker run``. Let's simplify it so the service starts automatically when the |
|
| 138 |
+container starts. |
|
| 139 |
+ |
|
| 140 |
+.. code-block:: bash |
|
| 141 |
+ |
|
| 142 |
+ docker commit <container_id> <your username>/postgresql -run='{"Cmd": \
|
|
| 143 |
+ ["/bin/su", "postgres", "-c", "/usr/lib/postgresql/9.2/bin/postgres -D \ |
|
| 144 |
+ /var/lib/postgresql/9.2/main -c \ |
|
| 145 |
+ config_file=/etc/postgresql/9.2/main/postgresql.conf"], PortSpecs": ["5432"]} |
|
| 146 |
+ |
|
| 147 |
+From now on, just type ``docker run <your username>/postgresql`` and PostgreSQL |
|
| 148 |
+should automatically start. |