为什么我不能多次使用Docker CMD来运行多个服务? [英] Why can't I use Docker CMD multiple times to run multiple services?

查看:564
本文介绍了为什么我不能多次使用Docker CMD来运行多个服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个名为centos + ssh的Dockerfile的基本映像。在centos + ssh的Docker文件中,我使用CMD来运行ssh服务。



然后我想构建一个运行名为rabbitmq的其他服务的图像,Dockerfile:

  FROM centos + ssh 
EXPOSE 22
EXPOSE 4149
CMD / opt / mq / sbin / rabbitmq-server启动

要启动rabbitmq容器,请运行:

  docker run -d -p 222:22 -p 4149:4149 rabbitmq 

但是ssh服务不起作用,它感觉rabbitmq的Dockerfile CMD覆盖了centos的CMD。


  1. CMD如何在docker图像?

  2. 如果我想要运行多个服务,怎么办?使用主管?


解决方案

即使CMD在Dockerfile中写下来,它确实是运行时信息。就像EXPOSE一样,但与例如RUN和ADD。这样,我的意思是,您可以稍后在扩展的Dockerfile中覆盖它,或者在运行命令中简单,这是您正在经历的。在任何时候,只能有一个CMD。



如果要运行多个服务,我确实会使用主管。您可以为每个服务创建一个管理员配置文件,将它们添加到一个目录中,并使用 supervisord -c / etc / supervisor 运行主管,指向一个主管配置文件加载所有的服务,看起来像

  [supervisord] 
nodaemon = true

[ include]
files = /etc/supervisor/conf.d/*.conf

如果你想要更多的细节,我在这个主题上写了一个博客: http://blog.trifork.com/2014/03/11/using-supervisor-with-docker-to-manage-processes-supporting-image-inheritance/


I have built a base image from Dockerfile named centos+ssh. In centos+ssh's Dockerfile, I use CMD to run ssh service.

Then I want to build a image run other service named rabbitmq,the Dockerfile:

FROM centos+ssh
EXPOSE 22
EXPOSE 4149
CMD /opt/mq/sbin/rabbitmq-server start

To start rabbitmq container,run:

docker run -d -p 222:22 -p 4149:4149 rabbitmq

but ssh service doesn't work, it sense rabbitmq's Dockerfile CMD override centos's CMD.

  1. How does CMD work inside docker image?
  2. If I want to run multiple service, how to? Using supervisor?

解决方案

Even though CMD is written down in the Dockerfile, it really is runtime information. Just like EXPOSE, but contrary to e.g. RUN and ADD. By this, I mean that you can override it later, in an extending Dockerfile, or simple in your run command, which is what you are experiencing. At all times, there can be only one CMD.

If you want to run multiple services, I indeed would use supervisor. You can make a supervisor configuration file for each service, ADD these in a directory, and run the supervisor with supervisord -c /etc/supervisor to point to a supervisor configuration file which loads all your services and looks like

[supervisord]
nodaemon=true

[include]
files = /etc/supervisor/conf.d/*.conf

If you would like more details, I wrote a blog on this subject here: http://blog.trifork.com/2014/03/11/using-supervisor-with-docker-to-manage-processes-supporting-image-inheritance/

这篇关于为什么我不能多次使用Docker CMD来运行多个服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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