title: Run a service automatically in a docker container
body: <p>I'm setting up a simple image: one that holds Riak (a NoSQL database). The image starts the Riak service with <code>riak start</code> as a CMD. Now, if I run it as a daemon with <code>docker run -d quintenk/riak-dev</code>, it does start the Riak process (I can see that in the logs). However, it closes automatically after a few seconds. If I run it using <code>docker run -i -t quintenk/riak-dev /bin/bash</code> the riak process is not started (UPDATE: see answers for an explanation for this). In fact, no services are running at all. I can start it manually using the terminal, but I would like Riak to start automatically. I figure this behavior would occur for other services as well, Riak is just an example.</p>
  
  <p>So, running/restarting the container should automatically start Riak. What is the correct approach of setting this up?</p>
  
  <hr>
  
  <p>For reference, here is the Dockerfile with which the image can be created (UPDATE: altered using the chosen answer):</p>
  
  <pre><code>FROM ubuntu:12.04
  RUN apt-get update
  RUN apt-get install -y openssh-server curl 
  RUN curl http://apt.basho.com/gpg/basho.apt.key | apt-key add -
  RUN bash -c "echo deb http://apt.basho.com precise main &gt; /etc/apt/sources.list.d/basho.list"
  RUN apt-get update
  RUN apt-get -y install riak
  RUN perl -p -i -e 's/(?&lt;=\{http,\s\[\s\{")127\.0\.0\.1/0.0.0.0/g' /etc/riak/app.config
  EXPOSE 8098 
  CMD /bin/riak start &amp;&amp; tail -F /var/log/riak/erlang.log.1
  </code></pre>
  
  <p>EDIT: -f changed to -F in CMD in accordance to sesm his remark</p>
  
  <hr>
  
  <h1>MY OWN ANSWER</h1>
  
  <p>After working with Docker for some time I picked up the habit of using supervisord to tun my processes. If you would like example code for that, check out <a href="https://github.com/Krijger/docker-cookbooks" rel="noreferrer">https://github.com/Krijger/docker-cookbooks</a>. I use my supervisor image as a base for all my other images. I blogged on using supervisor <a href="http://blog.trifork.com/2014/03/11/using-supervisor-with-docker-to-manage-processes-supporting-image-inheritance/" rel="noreferrer">here</a>.</p>