Docker Community Forums

Share and learn in the Docker community.

Is container still active if the cmd (by container start/run) exit by cmd (by exec) is till alive?


(Klaus1982) #1

I’d like to confirm the expected behavior of the following case:
Step 1: start/run container with "/bin/sleep 100"
Step 2: exec “/bin/sleep 2000”

So, will the command “docker run” for #1 will exit after 100s? will the container still active after 100s?


(Ranjandas) #2

Hi,

If you are running /bin/sleep 100 as CMD in Dockerfile build and run the image, that image will run for 100 sec and will exit.

Also if you are running a container using like docker run -it --rm imagename /bin/sleep 100 it will also exit after 100 sec.

Regards


(Klaus1982) #3

For #2, i’m going to use “docker exec” to start a new command in the existing container? Will container exit although “docker exec” command is still running?


(Ranjandas) #4

If you are using docker exec to run sleep as a new command in a running container even if the sleep command exits the container wont exit. The containers lifetime is determined by the command which was used to start the container(Process ID 1 in the container) and not on any other process that runs/exits in between.

Regards


(Klaus1982) #5

Copy that; so if the new command by “docker exec”'s running time is longer, the container will also exit when started command finish. right?


(Ranjandas) #6

Yes thats right. It all depends on the command that started the container.


(Klaus1982) #7

Copy that, thanks very much.