0

I'm creating a docker service that executes a command to start a program, but the problem is the service keeps terminating, because once the command to start completes, docker thinks it's done and terminates and starts it again. I want to change this behaviour so that the docker service runs, completes its command, then stays running forever after its command is completed.

How do I do this? I know you can use the -td flag to do this with a plain old "docker run" but how do I do this with docker stack using a compose file?

EDIT: I'm attempting to append a "sleep infinity" command to the end of the command like this:

FROM ubuntu:16.04
ADD <redacted> /opt/<redacted>
CMD "/opt/<redacted>/start.sh; sleep infinity"

Running with docker stack deploy results in:

/bin/sh: 1: /opt/<redacted>/start.sh; sleep infinity: not found
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
user1072692
  • 687
  • 1
  • 8
  • 19
  • 1
    What should the container be doing when your start command is done? Is it just taking up disk space, or do you expect it to be providing a service of some sort? – BMitch Jul 21 '17 at 20:32
  • The container is providing a service.. basically, there's a script that starts the service, but the problem is the script that starts the service exists with code 0 once the service is started because all the script does is bootstrap it. So docker thinks its done and shuts it down. – user1072692 Jul 21 '17 at 20:43

2 Answers2

1

The preferred solution, and the one you'll see used by most images in the docker hub, is to redesign your startup script to run your application in the foreground. Steps to do this vary by application, so check the hub to see how others have done it before you.

As a fall back, you can append a command like sleep infinity or tail -f /dev/null, which will block indefinitely, to the end of your startup script. This fall back is very much a hack and should be avoided.

Edit: to run this outside of your script, you can change your command to CMD /opt/xyz/start.sh && sleep infinity (remove your double quotes). Docker should turn this into /bin/sh -c "/opt/xyz/start.sh && sleep infinity". Note that these are adding a hack on top of a hack and I continue to recommend that you avoid this solution.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • Can't rewrite the script. I have to use what I have. I tried the append sleep solution but that's not working.. it's complaining that "/bin/sh: sleep not found" which is very weird because if I run the container manually and attach it I can run sleep just fine. – user1072692 Jul 21 '17 at 20:53
  • Please include a [MCVE](http://stackoverflow.com/help/mcve) in your question so that I can provide further assistance. – BMitch Jul 21 '17 at 20:54
0
CMD "/opt/<redacted>/start.sh; sleep infinity"

This is telling Docker to find and run this executable file:

/opt/<redacted>/start.sh; sleep infinity

But that path does not exist as a file, so it tells you it can't find that.

You need to remove the quote marks, they are confusing things.

CMD /opt/<redacted>/start.sh; sleep infinity

Now it will try to locate and run the file /opt/<redacted>/start.sh, followed by running the sleep command with the argument infinity.

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112