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 ?

  • kafka-node LeaderNotAvailable errors on send
  • Why it'll abort when I add `RUN ln -sf /dev/stderr /var/log/mysql/error.log` to mysql dockerfile?
  • Restart Docker Container (Automatically) when Image changes in Portainer(?)
  • Does docker storage driver matter for applications that don't write a lot?
  • how to mount host directory to container VOLUME?
  • .pac files for ubuntu - command line
  • Handling ports in Shipyard Load Balancer with Docker
  • Docker php excel error (php://output) i/o stream
  • Docker: in memory file system
  • How do I run 2 environments of SkyDns/Skydock simultaneously?
  • Installing rpy2 with Docker is unable to find R path
  • How to start a docker container when windows boots
  • 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.