2

I have this entrypoint in a Dockerfile:

ENTRYPOINT ["r2g", "run"]

and I run the resulting image with:

docker run --name "$container" "$tag"

most of the time, I want the container to exit when it's done - the r2g process is not a server, but a testing command line tool. So my question is - if I want to conditionally keep the container from exiting, is there a flag I can pass to docker run to keep the container alive? Can I add something to ENTRYPOINT to keep the container alive?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 2
    Container is nothing magical. It's just a process, like any other program. Ask yourself a question what would you do to prevent a process (your entrypoint) from terminating? – Mike Doe Jun 19 '18 at 07:17
  • pass a flag to the `docker run` executable to tell it to not exit? – Alexander Mills Jun 20 '18 at 05:42
  • This does not make any sense and is not possible. You can't tell a process to "not terminate". If it terminates it terminates. All Docker can do is to restart it. This is what `--restart-policy` is all about. – Mike Doe Jun 20 '18 at 07:31

1 Answers1

5

The only way to keep the docker container running is making it run a command that does not exit.

In your case, when you don't want the container to exit, you could run something like this:

docker run --name "$container" "$tag" sh -c "r2g run && sleep infinity"

This way, once the r2g command is finished, your container will wait indefinitely and keep running.

whites11
  • 12,008
  • 3
  • 36
  • 53
  • that should work, although I would be surprised if there is no argument to `docker run` that can keep it alive, something like `docker run --wait` or `docker run --forever` or whatever – Alexander Mills Jun 19 '18 at 21:50
  • No there is not. As emix said in a comment, docker is Simply a wrapper around an executable. There's no way to keep an executable running forever doing nothing if it is not designed to do that. – whites11 Jun 20 '18 at 04:32