Search code examples
dockerdocker-composedocker-stack

Docker Swarm - Get Service to Stay Running Forever


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

Solution

  • 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.