Docker多个入口点 [英] Docker multiple entrypoints
问题描述
说我有以下Dockerfile:
Say I have the following Dockerfile:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y apache2
RUN apt-get install -y mongod #pretend this exists
EXPOSE 80
ENTRYPOINT ["/usr/sbin/apache2"]
ENTRYPOINT
命令可以使 apache2
在容器启动时启动。当容器以命令 service mongod start
开头时,我还希望能够启动 mongod
。但是,根据文档,只能有一个 ENTRYPOINT
在Dockerfile中。那么正确的方法是什么?
The ENTRYPOINT
command makes it so that apache2
starts when the container starts. I want to also be able to start mongod
when the the container starts with the command service mongod start
. According to the documentation however, there must be only one ENTRYPOINT
in a Dockerfile. What would be the correct way to do this then?
推荐答案
正如Jared Markell所说,如果您想在一个程序中启动多个进程, docker容器,您必须使用 supervisor 。您将必须配置主管来告诉他启动您的不同流程。
As Jared Markell said, if you wan to launch several processes in a docker container, you have to use supervisor. You will have to configure supervisor to tell him to launch your different processes.
我在博客文章,但您确实有一个这里的不错文章详细介绍了如何以及为何在Docker中使用超级用户。
I wrote about this in this blog post, but you have a really nice article here detailing how and why using supervisor in Docker.
基本上,您将希望执行以下操作:
Basically, you will want to do something like:
FROM ubuntu
RUN apt-get update
RUN apt-get install -y apache2
RUN apt-get install -y mongod #pretend this exists
RUN apt-get install -y supervisor # Installing supervisord
ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf
EXPOSE 80
ENTRYPOINT ["/usr/bin/supervisord"]
并添加一个配置文件managerd.conf
And add a configuration a file supervisord.conf
[supervisord]
nodaemon=true
[program:mongodb]
command=/etc/mongod/mongo #To adapt, I don't know how to launch your mongodb process
[program:apache2]
command=/usr/sbin/apache2 -DFOREGROUND
编辑:由于此答案收到了很多支持,我想以此作为警告,警告说不要将Supervisor 视为运行多个作业的最佳实践。相反,您可能有兴趣为您的不同进程创建多个容器并通过 docker compose 对其进行管理。
简而言之, Docker Compose 允许您在一个文件中定义应用程序所需的所有容器,并在一个命令中启动它们。
EDIT: As this answer has received quite lot of upvotes, I want to precise as a warning that using Supervisor is not considered as a best practice to run several jobs. Instead, you may be interested in creating several containers for your different processes and managing them through docker compose. In a nutshell, Docker Compose allows you to define in one file all the containers needed for your app and launch them in one single command.
这篇关于Docker多个入口点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!