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

use*_*023 2 dns named docker dockerfile docker-compose

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

Dockerfile:

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"]
Run Code Online (Sandbox Code Playgroud)

启动绑定.sh:

#! /bin/bash

/usr/sbin/named.init start
Run Code Online (Sandbox Code Playgroud)

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

aak*_*ore 7

您可以使用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"]
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

最终输出: 同一个 docker 容器中的 Redis 和 Gunicorn 服务

您可以阅读我的完整文章,链接如下: 完整文章链接