Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep a service running on a Docker container

Tags:

docker

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.

Any suggestions?

like image 294
Sneh Shah Avatar asked Mar 21 '16 09:03

Sneh Shah


People also ask

How do I keep a docker Service running?

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.

How do you make a container run forever?

The easiest way to keep the container running is to change its entry point to a command that will continue running forever. Let's use tail -f /dev/null . Rechecking the running container via docker ps -a we can see that our container has been up and running for several seconds and hasn't stopped yet.

How do I run a docker container without stopping?

Detaching Without StoppingPress Ctrl-P, followed by Ctrl-Q, to detach from your connection. You'll be dropped back into your shell but the previously attached process will remain alive, keeping your container running. You can check this by using docker ps to get a list of running containers.

How do you stop a container from exiting?

If there's no terminal attached, then your shell process will exit, and so the container will exit. You can stop this by adding --interactive --tty (or just -it ) to your docker run ... command, which will let you type commands into the shell.


2 Answers

If you have a Dockerfile that uses an entrypoint pattern, it will look something like this:

(Dockerfile)

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.

(start.sh)

#!/bin/bash

service Your_Service_Or_Whatever start
tail -f /var/log/dmesg
like image 131
Ninjaxor Avatar answered Nov 08 '22 05:11

Ninjaxor


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

Use docker --version to find out your version

like image 33
kraeki Avatar answered Nov 08 '22 05:11

kraeki