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

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?

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

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?

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

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?

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

Copy that, thanks very much.