Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a service automatically in a docker container

Tags:

docker

riak

I'm setting up a simple image: one that holds Riak (a NoSQL database). The image starts the Riak service with riak start as a CMD. Now, if I run it as a daemon with docker run -d quintenk/riak-dev, 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 docker run -i -t quintenk/riak-dev /bin/bash 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.

So, running/restarting the container should automatically start Riak. What is the correct approach of setting this up?


For reference, here is the Dockerfile with which the image can be created (UPDATE: altered using the chosen answer):

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 > /etc/apt/sources.list.d/basho.list" RUN apt-get update RUN apt-get -y install riak RUN perl -p -i -e 's/(?<=\{http,\s\[\s\{")127\.0\.0\.1/0.0.0.0/g' /etc/riak/app.config EXPOSE 8098  CMD /bin/riak start && tail -F /var/log/riak/erlang.log.1 

EDIT: -f changed to -F in CMD in accordance to sesm his remark


MY OWN ANSWER

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 https://github.com/Krijger/docker-cookbooks. I use my supervisor image as a base for all my other images. I blogged on using supervisor here.

like image 405
qkrijger Avatar asked Jun 22 '13 15:06

qkrijger


People also ask

How do I run a service in a Docker container?

Run Docker Container as a Service Docker team recommends to use cross-platform built-in restart policy for running container as a service. For this, configure your docker service to start on system boot and simply add parameter --restart unless-stopped to the docker run command that starts YouTrack.

Does Docker run automatically?

Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts. Restart policies ensure that linked containers are started in the correct order. Docker recommends that you use restart policies, and avoid using process managers to start containers.

How do I keep Docker container running after service?

Dockerfile Command to Keep the Container Running Method 1: You can use the -t (pseudo-tty) docker parameter to keep the container running. Method 2: You can run the container directly passing the tail command via arguments as shown below. Method 3: Another method is to execute a sleep command to infinity.


2 Answers

To keep docker containers running, you need to keep a process active in the foreground.

So you could probably replace that last line in your Dockerfile with

CMD /bin/riak console 

Or even

CMD /bin/riak start && tail -F /var/log/riak/erlang.log.1 

Note that you can't have multiple lines of CMD statements, only the last one gets run.

like image 105
Gigablah Avatar answered Oct 11 '22 12:10

Gigablah


Using tail to keep container alive is a hack. Also, note, that with -f option container will terminate when log rotation happens (this can be avoided by using -F instead).

A better solution is to use supervisor. Take a look at this tutorial about running Riak in a Docker container.

like image 23
sesm Avatar answered Oct 11 '22 13:10

sesm