exit and delete all but the latest docker container

I currently have a lot of docker containers running that are no longer used.
So many actually that it would take a while to exit and quit them all manually.

I was wondering if there was a way to exit and delete all of them except for the latest one ?

  • RUN command not called in Dockerfile
  • Docker not working after running MapReduce - AWS
  • How to configure Docker in windows to listen to tcp?
  • Run asp.net 4.5 in Docker
  • Symfony app in Docker doesn't respond in URL call
  • What's the meaning of docker hub's url design?
  • Why does `apt install postgresql` not work in a docker container?
  • Setting up multiple Docker containers and MongoDB to run in CircleCI
  • Cache PIP packages in dockerfile
  • Docker has two bridge networks named 'bridge'. How to remove?
  • Trouble connecting to Docker registry stored on Nexus 3 Preview on Azure VM
  • How to write my own applications on Docker?
  • One Solution collect form web for “exit and delete all but the latest docker container”

    A quick approach to do this is the following:

    First, stop all containers with this command:

    docker stop $(docker ps -aq)

    Then, start the container that you want to keep using:

    docker start container_name

    And finally, use docker container prune, which removes all stopped containers.

    docker container prune

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.