Last Updated: October 29, 2016
·
6.915K
· skillslane

Run commands in a stopped container

docker exec only works with currently running containers.

If you want to take the resulting image, and run another command, you will need to commit that and start another container.:

docker run --name mycont4 ubuntu echo one</pre>
one
$ docker commit mycont4 myimage5
015b3a3d3844b0c3638ab0e07eabd6b2ccdd1768667bc8635051488c6dcec087
$ docker run --name mycont5 myimage5 echo two
two
Since the echo example might be skewing a bit, docker exec may still work for you. For example:

$ docker run --name mycont4 ubuntu sleep 10 # let this exit
$ docker start mycont4 $ this starts sleep 10 back up in the background
mycont4
$ docker exec mycont4 ps faux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 8 0.0 0.1 15568 2112 ? R 08:11 0:00 ps faux
root 1 0.7 0.0 4348 648 ? Ss 08:11 0:00 sleep 10
Since sleep doesn't exit immediately, the container is running still when you run docker exec.

Hopefully this is helpful.