Search code examples
dockerdockerfiledocker-run

Prevent container from exiting, conditionally


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?


Solution

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