如何在一个docker容器中启动两个服务
我需要在docker中启动两个服务/命令,从google我得到,我可以使用ENTRYPOINT和CMD来传递不同的命令。但是当我启动容器时,只有ENTRYPOINT脚本运行,CMD似乎没有运行。既然我是一个新的码头工人,你能帮助我如何运行两个命令吗 Dockerfile:如何在一个docker容器中启动两个服务,docker,docker-compose,dns,dockerfile,named,Docker,Docker Compose,Dns,Dockerfile,Named,我需要在docker中启动两个服务/命令,从google我得到,我可以使用ENTRYPOINT和CMD来传递不同的命令。但是当我启动容器时,只有ENTRYPOINT脚本运行,CMD似乎没有运行。既然我是一个新的码头工人,你能帮助我如何运行两个命令吗 Dockerfile: FROM registry.suse.com/suse/sle15 ADD repolist/*.repo /etc/zypp/repos.d/ RUN zypper refs && zypper refr
FROM registry.suse.com/suse/sle15
ADD repolist/*.repo /etc/zypp/repos.d/
RUN zypper refs && zypper refresh
RUN zypper in -y bind
COPY docker-entrypoint.d/* /docker-entrypoint.d/
COPY --chown=named:named named /var/lib/named
COPY --chown=named:named named.conf /etc/named.conf
COPY --chown=named:named forwarders.conf /etc/named.d/forwarders.conf
ENTRYPOINT [ "./docker-entrypoint.d/startbind.sh" ]
CMD ["/usr/sbin/named","-g","-t","/var/lib/named","-u","named"]
startbind.sh:
#! /bin/bash
/usr/sbin/named.init start
谢谢和问候,
Mohamed Naveen在
startbind.sh
你可以做:
#! /bin/bash
#start second servvice here, and push it to background:
/usr/sbin/secondesrvice.init start &
#then run the last commands:
/usr/sbin/named.init start
您的
/usr/sbin/named.init start
(入口点上的最后一个命令)命令不能进入后台,您需要将其保持在前台
如果最后一个命令未保留在前台,Docker将退出。您可以将两个服务添加到startbind.sh。您也可以使用RUN命令。在docker容器中运行execute命令。如果不起作用,您可以让我继续帮助您。您可以使用
supervisor
工具在单个docker容器中管理多个服务
查看以下示例(使用单个CMD运行Redis和Django服务器):
Dockerfile:
# Base Image
FROM alpine
# Installing required tools
RUN apk --update add nano supervisor python3 redis
# Adding Django Source code to container
ADD /django_app /src/django_app
# Adding supervisor configuration file to container
ADD /supervisor /src/supervisor
# Installing required python modules for app
RUN pip3 install -r /src/django_app/requirements.txt
# Exposing container port for binding with host
EXPOSE 8000
# Using Django app directory as home
WORKDIR /src/django_app
# Initializing Redis server and Gunicorn server from supervisors
CMD ["supervisord","-c","/src/supervisor/service_script.conf"]
service_script.conf文件
## service_script.conf
[supervisord] ## This is the main process for the Supervisor
nodaemon=true ## This setting is to specify that we are not running in daemon mode
[program:redis_script] ## This is the part where we give the name and add config for our 1st service
command=redis-server ## This is the main command to run our 1st service
autorestart=true ## This setting specifies that the supervisor will restart the service in case of failure
stderr_logfile=/dev/stdout ## This setting specifies that the supervisor will log the errors in the standard output
stderr_logfile_maxbytes = 0
stdout_logfile=/dev/stdout ## This setting specifies that the supervisor will log the output in the standard output
stdout_logfile_maxbytes = 0
## same setting for 2nd service
[program:django_service]
command=gunicorn --bind 0.0.0.0:8000 django_app.wsgi
autostart=true
autorestart=true
stderr_logfile=/dev/stdout
stderr_logfile_maxbytes = 0
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes = 0
最终输出:
您可以阅读我关于此的完整文章,链接如下:
在容器内运行多个服务的选项在这篇docker官方文章中描述得非常好: .
我建议您回顾一下为什么您需要在一个容器中包含两个服务(共享数据卷、init等),通过正确分离服务,您将拥有可扩展的体系结构、更有用的日志、更轻松的生命周期/资源管理和更轻松的测试。Hi Ron,但是容器立即退出您的
/usr/sbin/named.init start
命令不能进入后台,您需要将其保持在前台。在init脚本中应该有一个标志,标准答案是“在两个单独的容器中”。它们必须在同一个容器中有什么原因吗?(两个容器可以使用不同的命令运行同一个映像。)run
在映像构建阶段运行命令。它无法启动后台进程;在一个RUN
命令结束时运行的任何内容都将被停止,并且构建的映像从不包含正在运行的(后台)进程。您似乎复制并链接了您刚刚在个人网站上撰写的文章。您必须披露您与本网站的联系,否则您将看到您的答案被标记为垃圾邮件。