Browse code

Add a Docker example for running Riak.

Hector Castro authored on 2013/09/04 05:29:07
Showing 2 changed files
... ...
@@ -22,3 +22,4 @@ Contents:
22 22
    couchdb_data_volumes
23 23
    postgresql_service
24 24
    mongodb
25
+   running_riak_service
25 26
new file mode 100644
... ...
@@ -0,0 +1,151 @@
0
+:title: Running a Riak service
1
+:description: Build a Docker image with Riak pre-installed
2
+:keywords: docker, example, package installation, networking, riak
3
+
4
+Riak Service
5
+==============================
6
+
7
+.. include:: example_header.inc
8
+
9
+The goal of this example is to show you how to build a Docker image with Riak
10
+pre-installed.
11
+
12
+Creating a ``Dockerfile``
13
+
14
+Create an empty file called ``Dockerfile``:
15
+
16
+.. code-block:: bash
17
+
18
+    touch Dockerfile
19
+
20
+Next, define the parent image you want to use to build your image on top of.
21
+We’ll use `Ubuntu <https://index.docker.io/_/ubuntu/>`_ (tag: ``latest``),
22
+which is available on the `docker index <http://index.docker.io>`_:
23
+
24
+.. code-block:: bash
25
+
26
+    # Riak
27
+    #
28
+    # VERSION       0.1.0
29
+
30
+    # Use the Ubuntu base image provided by dotCloud
31
+    FROM ubuntu:latest
32
+    MAINTAINER Hector Castro hector@basho.com
33
+
34
+Next, we update the APT cache and apply any updates:
35
+
36
+.. code-block:: bash
37
+
38
+    # Update the APT cache
39
+    RUN sed -i.bak 's/main$/main universe/' /etc/apt/sources.list
40
+    RUN apt-get update
41
+    RUN apt-get upgrade -y
42
+
43
+After that, we install and setup a few dependencies:
44
+
45
+- ``curl`` is used to download Basho's APT repository key
46
+- ``lsb-release`` helps us derive the Ubuntu release codename
47
+- ``openssh-server`` allows us to login to containers remotely and join Riak
48
+  nodes to form a cluster
49
+- ``supervisor`` is used manage the OpenSSH and Riak processes
50
+
51
+.. code-block:: bash
52
+
53
+    # Install and setup project dependencies
54
+    RUN apt-get install -y curl lsb-release supervisor openssh-server
55
+
56
+    RUN mkdir -p /var/run/sshd
57
+    RUN mkdir -p /var/log/supervisor
58
+
59
+    RUN locale-gen en_US en_US.UTF-8
60
+
61
+    ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf
62
+
63
+    RUN echo 'root:basho' | chpasswd
64
+
65
+Next, we add Basho's APT repository:
66
+
67
+.. code-block:: bash
68
+
69
+    RUN curl -s http://apt.basho.com/gpg/basho.apt.key | apt-key add --
70
+    RUN echo "deb http://apt.basho.com $(lsb_release -cs) main" > /etc/apt/sources.list.d/basho.list
71
+    RUN apt-get update
72
+
73
+After that, we install Riak and alter a few defaults:
74
+
75
+.. code-block:: bash
76
+
77
+    # Install Riak and prepare it to run
78
+    RUN apt-get install -y riak
79
+    RUN sed -i.bak 's/127.0.0.1/0.0.0.0/' /etc/riak/app.config
80
+    RUN echo "ulimit -n 4096" >> /etc/default/riak
81
+
82
+Almost there. Next, we add a hack to get us by the lack of ``initctl``:
83
+
84
+.. code-block:: bash
85
+
86
+    # Hack for initctl
87
+    # See: https://github.com/dotcloud/docker/issues/1024
88
+    RUN dpkg-divert --local --rename --add /sbin/initctl
89
+    RUN ln -s /bin/true /sbin/initctl
90
+
91
+Then, we expose the Riak Protocol Buffers and HTTP interfaces, along with SSH:
92
+
93
+.. code-block:: bash
94
+
95
+    # Expose Riak Protocol Buffers and HTTP interfaces, along with SSH
96
+    EXPOSE 8087 8098 22
97
+
98
+Finally, run ``supervisord`` so that Riak and OpenSSH are started:
99
+
100
+.. code-block:: bash
101
+
102
+    CMD ["/usr/bin/supervisord"]
103
+
104
+Create a ``supervisord`` configuration file
105
+
106
+Create an empty file called ``supervisord.conf``. Make sure it's at the same
107
+level as your ``Dockerfile``:
108
+
109
+.. code-block:: bash
110
+
111
+    touch supervisord.conf
112
+
113
+Populate it with the following program definitions:
114
+
115
+.. code-block:: bash
116
+
117
+    [supervisord]
118
+    nodaemon=true
119
+
120
+    [program:sshd]
121
+    command=/usr/sbin/sshd -D
122
+    stdout_logfile=/var/log/supervisor/%(program_name)s.log
123
+    stderr_logfile=/var/log/supervisor/%(program_name)s.log
124
+    autorestart=true
125
+
126
+    [program:riak]
127
+    command=bash -c ". /etc/default/riak && /usr/sbin/riak console"
128
+    pidfile=/var/log/riak/riak.pid
129
+    stdout_logfile=/var/log/supervisor/%(program_name)s.log
130
+    stderr_logfile=/var/log/supervisor/%(program_name)s.log
131
+
132
+Build the Docker image for Riak
133
+
134
+Now you should be able to build a Docker image for Riak:
135
+
136
+.. code-block:: bash
137
+
138
+    docker build -t "<yourname>/riak" .
139
+
140
+Next steps
141
+
142
+Riak is a distributed database. Many production deployments consist of `at
143
+least five nodes <http://basho.com/why-your-riak-cluster-should-have-at-least-
144
+five-nodes/>`_. See the `docker-riak <https://github.com/hectcastro /docker-
145
+riak>`_ project details on how to deploy a Riak cluster using Docker and
146
+Pipework.