user2536023
user2536023

Reputation: 165

How to start two services in one docker container

I need to start two services/commands in docker, from google I got that I can use ENTRYPOINT and CMD to pass different commands. but when I start the container only ENTRYPOINT script runs and CMD seems not running. since I am a new docker can you help me on how to run two commands.

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

startbind.sh:

#! /bin/bash

/usr/sbin/named.init start

Thanks & Regards, Mohamed Naveen

Upvotes: 2

Views: 10609

Answers (4)

Ron
Ron

Reputation: 6591

Within startbind.sh you can do:

#! /bin/bash

#start second service here, and push it to background:
/usr/sbin/secondservice.init start &

#then run the last commands:
/usr/sbin/named.init start

Your /usr/sbin/named.init start (the last command on the entry point) command must NOT go into the background; you need to keep it in the foreground.

If this last command is not kept in foreground, Docker will exit.

Upvotes: 0

Alexz
Alexz

Reputation: 741

Options to run more than one service within the container described really well in this official docker article: multi-service_container. I'd recommend reviewing why you need two services in one container(shared data volume, init, etc) cause by properly separating the services you'll have ready to scale architecture, more useful logs, easier lifecycle/resource management, and easier testing.

Upvotes: 2

aakash_rathore
aakash_rathore

Reputation: 71

You can use supervisor tools for managing multiple services inside a single docker container.

Check out the below example(running Redis and Django server using single CMD):

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 file

## 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

Final output: Redis and Gunicorn service in same docker container

You can read my complete article on this, the link is given below: Link for complete article

Upvotes: 7

Gabi Martinez
Gabi Martinez

Reputation: 1

You can add to startbind.sh the two service start. You can use RUN command also. RUN execute commands in docker container. If dont work, you can ask me to keep helping you.

Upvotes: -1

Related Questions