0

'docker exec' can only used on running container, but what is the meaning of running container? Is that means the container should be computing something? or is the issue about the [command] which I define for the container? Why my TensorFlow container always be stopped status?

After I used 'docker run' to build a tensorflow container, the container stopped automatically. I need to restart it and then execute command on it. Why the container cannot be always in running since I build it?

docker run -it --runtime=nvidia tensorflow/tensorflow:latest-gpu-py3

It will then pops up a bash which I can use to control the container. But after I exit, the container stopped itself. Which means, I can only use docker ps -a to see my container but docker pscan not. I have to restart the container if I want to use my container again.

UPDATE1: If I want create a container like VM, I cannot use docker run with a temporal [command] like python ... The container will lose control permanently after the command finished. docker restart cannot start the container again. Hence, docker exec cannot apply on it. Instead, using bash or nothing as the [command] can create a container which can be restart, therefore, can be applied withdocker exec.

UPDATE2: docker run -d -it can create a running container (but the bash shell won't pops up, neither even with bash). Directly using docker exec -it container_name bash can take the control of the running container again, without docker restart. In this time, exiting bash shell will not stop the container.

chongkai Lu
  • 452
  • 3
  • 9
  • add `tail -f /dev/null` to the end of your `docker run` command and the container will stay up after executing whatever it needs to. – Nordle Apr 04 '19 at 09:19
  • Treating containers like a VM will give you trouble: https://stackoverflow.com/q/16047306/596285 – BMitch Apr 05 '19 at 08:45
  • You really have a misconception of what Docker is for and what the command you are running are doing, as @BMitch points it out. `docker run` and `docker exec` are two things that comes **in pair**, if you want a "listening server-like" docker container you would 1/ `docker run --name=some_name_here -d myimage` 2/ if you need, attach to it via `docker exec -ti some_name_here bash` – β.εηοιτ.βε Apr 05 '19 at 09:25
  • 1
    @b.enoit.be docker made a change a few years back to allow `-d` with `-it`. It means to allocate a tty and setup stdin for the container, but run the container detached. Any process trying to read from that stdin will hang, rather than getting an EOF, and you can later attach to the container and give it input. – BMitch Apr 05 '19 at 12:00
  • @BMitch good to know, thanks – β.εηοιτ.βε Apr 05 '19 at 13:58

2 Answers2

0

The difference between docker ps -a and docker ps is exactly what you are looking for:

From the documentation:

--all , -a      Show all containers (default shows just running) 

So

  • docker ps gives you only running container
  • docker ps -a also shows you the stopped ones

So probably, if you expect you container to be long running, (like it would for a web server), then indeed your container command could have an issue and is not keeping your container alive.

Also mind that, if you run your container with the options -ti, like you did, you get an interactive tty attached to it.

--tty , -t              Allocate a pseudo-TTY
--interactive , -i      Keep STDIN open even if not attached

That basically means that, as soon as you exit that interactive context, your container will shut down.

Running it in a detached mode, with the options -d, is maybe what you are looking for

docker run -d --runtime=nvidia tensorflow/tensorflow:latest-gpu-py3

Related documentation:

--detach , -d       Run container in background and print container ID

https://docs.docker.com/engine/reference/commandline/run/

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • --detach is a solution! But I am still confused about the definition of "running". what's the main difference between a running and stopped container? How dose Docker judge this? – chongkai Lu Apr 05 '19 at 02:21
  • Well it is exactly like a computer. It might be either shut down or running. The basic idea of a container is that is runs one only command, if that command stops, the container stops. – β.εηοιτ.βε Apr 05 '19 at 09:18
0

A container is running when there is an active process running inside it. When you are running this tensorflow container, it will exit due to there being no running process

If you were to run

docker run -it --runtime=nvidia tensorflow/tensorflow:latest-gpu-py3 bash

or

docker run -it --runtime=nvidia tensorflow/tensorflow:latest-gpu-py3 python <python script name>

then the container would run the bash/python script as a process and therefore remain up whilst that process is running

View running processes with:

docker ps

See all containers (including stopped/exited tasks) with:

docker ps -a
devingops
  • 196
  • 4
  • 2
    The container won't remain up by doing this, it will either put you into a bash environment which will kill the container when you exit, or run the python script and exit once it's finished. – Nordle Apr 04 '19 at 09:18
  • I don't think active process is the evidence. I can use 'docker run -d' to create a running new container without giving it any command or process. Besides, if you give a python file to the 'docker run' when creating new container, you can never use the container again after it finished the python file. – chongkai Lu Apr 05 '19 at 03:03
  • -d simply detaches you from the container. Take a read of: https://medium.com/devopsion/life-and-death-of-a-container-146dfc62f808 for a better understanding. – devingops Apr 05 '19 at 17:30