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 ?

  • Docker oracle database fails when starting after commiting
  • strange docker error when stop and starting a container
  • Case sensitivity issue in Spring application
  • How to do a custom hostname (URL) to site in docker container in Ubuntu 16.04?
  • Validate Connection fail to publish asp.net website to docker on ubuntu
  • Why does `docker images` also show the base image?
  • cURL to DropBox using Symfony 3
  • How can i persist my logs/data to local filesystem in docker
  • Private Network broken in Vagrant with Docker
  • How to list the content of a named volume in docker 1.9+?
  • Issue with Docker Toolbox
  • Volumes and docker-compose
  • 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.