How to keep a service running on a Docker container
I am trying to run a simple docker container with my web application installed (Not using docker file).
During the testing I would always run a container using -t -i option and then start the tomcat service inside it by running a shell script.
How when I am moving to production I dont want to use the -t -i option any more and just need my Tomcat service to start and be the only primary service.
I trying pointing the entrypoint to the start up script for starting tomcat but the container terminates after that script finishes.
How do I run a container, start a service and keep that service as the single primary service of the container?
Note: I read some posts about supervisor but not sure if I would need to start building my image from scratch if I go that route? I would prefer not doing that.
3 Solutions collect form web for “How to keep a service running on a Docker container”
If you have a Dockerfile that uses an entrypoint pattern, it will look something like this:
FROM ubuntu ...Some configuration steps... add start.sh /start.sh ENTRYPOINT ["/start.sh"]
All you need to do is make sure your
start.sh script ‘hangs’ in some way. Some people like to tail the syslogs, but tailing any file that exists will work.
#!/bin/bash service Your_Service_Or_Whatever start tail -f /var/log/dmesg
Docker containers as default will run according to the configuration in the images
Dockerfile. If you usually run a container with the
-i flag, you leave STDIN open allowing you access to the containers entrypoint or it could be a bash shell. To achieve what you want, you can run the container in a detached state passing your commands into docker run directly.
docker run -d myapp /opt/catalina/bin/startup.sh
This will run the myapp container in a detached state and will run the command passed as the 3rd argument. If the command results in a long lived service, the container will stay active as long as the service is.
This is explained in detail in the docs.
A shorter version:
FROM ubuntu ...Some configuration steps... ENTRYPOINT ["/bin/sh", "-c", "while true; do sleep 1; done"]
tested with Docker version 1.12.1, build 23cf638
docker --version to find out your version