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

我需要在 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

https://stackoverflow.com/questions/61533652/

相关文章:

reactjs - 是否有任何函数可以在 react-final-form 中获取表单值

amazon-web-services - AWS : Passing Job parameters

javascript - 如何在 React Native 中更改数组键名称

amazon-web-services - 通过 HTTPS 提供静态 S3 网站时降低 Cloud

java - eclipse maven deploy war to nexus OSS Acces

python - 删除子列表重复项,包括反向

javascript - 我如何将所选项目从 React Native Paper Menu 传递到

ruby-on-rails - 如何在 Ruby on Rails 6 中将 rich_text_a

angular - 不同的路线,相同的组件 - 为什么组件不在路线更改时重新加载?

node.js - Yarn workspaces——包别名