gpt4 book ai didi

docker - 如何在一个docker容器中启动两个服务

转载 作者:行者123 更新时间:2023-12-05 09:10:22 25 4
gpt4 key购买 nike

我需要在 docker 中启动两个服务/命令,从谷歌我知道我可以使用 ENTRYPOINT 和 CMD 来传递不同的命令。但是当我启动容器时,只有 ENTRYPOINT 脚本运行而 CMD 似乎没有运行。因为我是一个新的 docker ,你能帮助我如何运行两个命令吗?

docker 文件:

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"]

开始绑定(bind).sh:

#! /bin/bash

/usr/sbin/named.init start

感谢和问候,穆罕默德·纳文

最佳答案

您可以使用 supervisor 工具来管理单个 docker 容器内的多个服务。

查看以下示例(使用单个 CMD 运行 Redis 和 Django 服务器):

docker 文件:

# 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

最终输出: Redis and Gunicorn service in same docker container

你可以阅读我的完整文章,链接如下: Link for complete article

关于docker - 如何在一个docker容器中启动两个服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61533652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com