在docker容器中自动运行服务 [英] Run a service automatically in a docker container

查看:113
本文介绍了在docker容器中自动运行服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个简单的图像:一个保存Riak(一个NoSQL数据库)的图像。图像以code> riak start 作为CMD启动Riak服务。现在,如果我使用 docker run -d quintenk / riak-dev 作为守护进程运行它,那么它会启动Riak进程(我可以在日志中看到)。但是,它会在几秒钟后自动关闭。如果我使用 docker run -i -t quintenk / riak-dev / bin / bash 运行它,则riak进程不会启动(更新:请参阅有关此解释的答案) 。实际上,根本没有服务运行。我可以使用终端手动启动它,但是我希望Riak自动启动。我认为这个行为会发生在其他服务上,Riak只是一个例子。



所以,运行/重新启动容器应该会自动启动Riak。






为了参考,这里是可以创建图像的Docker文件(更新:使用所选答案更改):

  FROM ubuntu:12.04 
RUN apt-get update
运行apt-get install -y openssh-server curl
运行curl http://apt.basho.com/gpg/basho.apt.key | apt-key add -
RUN bash -cecho deb http://apt.basho.com precise main> /etc/apt/sources.list.d/basho.list
RUN apt -get update
运行apt-get -y安装riak
运行perl -p -i -e的/(?< = \ {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

编辑:-f根据sesm的说法,在CMD中更改为-F。






MY OWN ANSWER



在Docker工作了一段时间后,我收到了使用supervisord来调用我的进程的习惯,如果你想要代码示例,请查看 https://github.com/Krijger/docker-cookbooks 我使用我的主管形象作为我所有其他图像的基础我使用主管a href =http://blog.trifork.com/2014/03/11/using-supervisor-with-do cker-to-manage-processes-supporting-image-inheritance /rel =noreferrer> here 。

解决方案

p>为了保持Docker容器运行,您需要在前台保持进程的活动。



所以你可能用Dockerfile中的最后一行替换为

  CMD / bin / riak控制台

甚至

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

请注意,你不能有多行CMD语句,只有最后一个运行。


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.

解决方案

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.

这篇关于在docker容器中自动运行服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆