0

I am passing the service script to ENTRYPOINT. The service is started but exited. I have to start a process per container using service script from ENTRYPOINT or CMD. This way, I can reload the configuration inside the container using service script. I tried with CMD statement as well, but it starting the service but immediately exists the container.

ENTRYPOINT ["/etc/init.d/elasticsearch", "start"]

/etc/init.d/elasticsearch script has below code to start the service as daemon.

cd $ES_HOME
echo -n $"Starting $prog: "
daemon --user elasticsearch --pidfile $pidfile $exec -p $pidfile -d
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval

Is it not possible to start the service using startup script and keep the container running?

commands used to create and run the containers.

docker build -f Dockerfile -t="elk/elasticsearch" .
docker run -d elk/elasticsearch
docker run -it elk/elasticsearch bash
intechops6
  • 1,007
  • 4
  • 22
  • 43

2 Answers2

1

In general you do not use service scripts with Docker. Also in general, you never restart the service inside a container; instead, you stop the existing container, delete it, and start a new one.

The standard pattern is to launch whatever service it is you are trying to run, directly, as a foreground process. (No /etc/init.d, service, or systemctl anything.) You can extract the relevant command from the init script you show. I would replace your ENTRYPOINT command with

CMD ["elasticsearch"]

(but also double-check the Elasticsearch documentation just in case there are some other command-line options that matter).

The second part of this is to make sure database data is stored outside the container. Usually you use the docker run -v option to mount some alternate storage into the container. For example:

docker run \
  --name elasticsearch \
  -p 9200:9200 \
  -v ./elasticsearch:/var/data/elasticsearch \
  imagename

Once you’ve done this, you are free to stop, delete, and recreate the container, which is the right way to restart the service. (You need to do this if the underlying image ever changes; this happens if there is a bug fix or security issue in the image software or in the underlying Linux distribution.)

docker stop elasticsearch
docker rm elasticsearch
docker run -- name elasticsearch ...

You can write a simple shell script to hold the docker run command, or use an orchestration tool like Docker Compose that lets you declare the container parameters.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Perfect answer. I was wondering why service script is not used in Dockerfile. Thanks @David maze. – intechops6 Jul 10 '19 at 18:59
  • The container has exited immediately. In the logs of the container, it says the node is not a swarm manager and then swarm init and swarm join to connect to this node. This time I used CMD ["nginx"] to start the nginx server. Why do I need to run this in swarm cluster? – intechops6 Jul 10 '19 at 21:44
1

The sysv initscripts are of type "forking" speaking in terms of a service manager. So it will detach from the start script. The container then needs some init process on pid 1 that controls the background process(es).

If you do not want to extract the relevant command from the initscript then you could still use the docker-systemctl-replacement to do both things for you. If it is run as CMD then it will start enabled service scripts just as you are used from a normal machine.

Guido U. Draheim
  • 3,038
  • 1
  • 20
  • 19
  • Running many services in a container may not be a best practice for many reasons, there are situations where running multiple applications in a container does dirty and quick job if written correctly. Nice to know there is a possibility using docker-systemctl-replacement. – intechops6 Jul 11 '19 at 14:43