How to retain docker alpine container after "exit" is used?

shubham_kamath picture shubham_kamath · Aug 11, 2017 · Viewed 15.8k times · Source

Like for example if I use command docker run -it alpine /bin/sh it starts a terminal after which I can install packages and all. Now when I use exit command it goes back to the terminal. (main one)

So how can I access the same container again? When I run that command again, I get a fresh alpine.

Please help

Answer

yamenk picture yamenk · Aug 11, 2017

The container lives as long as the specified run command process is still running. When you specify to run /bin/sh, once you exit, the sh process will die and so will you container.

If you want to keep your container running, you have to keep the process inside running. For your case (I am not sure what you want to acheive, I assume you are just testing), the following will keep it running

docker run -d --name alpine alpine tail -f /dev/null

Then you can sh into the container using

docker exec -it alpine sh