0

I am starting a command which goes into background on its own. On the terminal it appears that the command shows some output on the screen and exited.

On my host i can check that the command is still running by finding it in $ ps -aux

So the docker thinks the command is done and it exits.

Based on that background running command i want to run another command using --exec.

So how to achieve this

Santhosh
  • 9,965
  • 20
  • 103
  • 243
  • 1
    The best answer is **not** to self-daemonize. Most software can have that "functionality" turned off. If you want two different programs to interoperate, ideally, they should be in separate containers, with service links between them. – Charles Duffy Mar 04 '19 at 20:34
  • See [Access service in docker container from another docker container](https://stackoverflow.com/questions/35867600/access-service-running-in-docker-container-from-inside-another-docker-container) – Charles Duffy Mar 04 '19 at 20:37
  • Ok, unless that feature of turning of deamonizing exists in software it will not be possible to take advantage of such command in docker – Santhosh Mar 04 '19 at 20:37
  • "Not possible" isn't quite true -- there are ugly hacks you can use, but it should be established that they're actually *needed* first. For example, you can create the self-daemonizing program in a context where it holds a file descriptor which you try to lock from the parent process. Anyhow, if you told us more about what the command in question *was*, we could look for an existing way to prevent its self-daemonization. – Charles Duffy Mar 04 '19 at 20:39
  • But my container exits immediately. So there is chance of doinga anything further – Santhosh Mar 04 '19 at 20:39
  • You can find a long list of examples of starting programs in ways that stop them from self-daemonizing at http://smarden.org/runit/runscripts.html (those are "run scripts" for the Runit process supervision system; runit requires that software not daemonize itself in order to be correctly monitored). – Charles Duffy Mar 04 '19 at 20:39

1 Answers1

2

A docker container lives as long as the process that you have specified it to run, has not exited.

Docker containers do not make use of daemons and services - you are supposed to run your process in the foreground of the container. This is the recommended usage of containers - although you can force it to do otherwise if you want to.

Something that has helped me a lot conceptually, is to think more of a docker container as a "process isolation" mechanism, and less of it as a box of software that you can start and stop.

You may find this guide useful if you want to start multiple processes in the container: https://docs.docker.com/config/containers/multi-service_container/

A little trick is to add an indefinitly running command to the end of your docker ENTRYPOINT or CMD. One commenly used is tail -f /dev/null, like this:

systemctl start myservice && tail -f /dev/null

I cannot say I can recommend this, but it will quite likely do what you want it to.

I will include a minimal example here, of how this can be used. Here's a Dockerfile where the ENTRYPOINT is specified to start a service (running in the background), and then tailing the null device, /dev/null:

FROM ubuntu
RUN apt-get update
RUN apt-get install -y apache2
ENTRYPOINT service apache2 start && tail -f /dev/null

Build it with:

docker build -t servicetest:01 .

Start it with:

docker run -p 8080:80 servicetest:01

And visit http://localhost:8080 to see it working

Andreas Lorenzen
  • 3,810
  • 1
  • 24
  • 26