2

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

Alexz
  • 741
  • 1
  • 8
  • 20
user2536023
  • 165
  • 1
  • 4
  • 14
  • The standard answer to this is "in two separate containers". Is there a reason they _must_ be in the same container? (The two containers could run the same image with different commands.) – David Maze May 01 '20 at 00:33

4 Answers4

7

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

  • It seems you copied and linked an article you just wrote on your personal website. You have to disclose your affiliation to this website otherwise you will see your answer flagged as spam. – Mickael B. May 02 '20 at 03:55
  • The link no longer works due to a DNS problem – moojen Jan 14 '22 at 13:49
2

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.

Alexz
  • 741
  • 1
  • 8
  • 20
0

Within startbind.sh you can do:

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

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

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

Ron
  • 5,900
  • 2
  • 20
  • 30
  • Hi Ron, But the container get exited immediately – user2536023 Apr 30 '20 at 22:24
  • your `/usr/sbin/named.init start` command must NOT go into background, you need to keep it on the foreground. You should have a flag for that in the init script – Ron Apr 30 '20 at 22:27
-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.

  • 2
    `RUN` runs a command during the image build phase. It can't start a background process; anything that's left running at the end of a single `RUN` command will get stopped, and a built image never includes running (background) processes. – David Maze May 01 '20 at 00:34