I recently took on some new clients who were already using Docker, and I found it so useful that I now use it in nearly every project. As with any piece of software, there were a few commands that I found myself running regularly, so I’ve created some helper scripts to make them easier to remember.
These scripts are all intended to be run on a local development machine, not on a production system.
Stops all containers on the system. Useful if you can’t start a new container because an existing container is using the same port.
docker stop $(docker ps -aq)
Removes all stopped containers on the system.
docker rm -v $(docker ps -aq -f status=exited)
Stops all containers, removes all containers, and prunes all volumes. This should give you a clean build of everything across your system.
docker stop $(docker ps -aq) docker rm -v $(docker ps -aq -f status=exited) docker volume prune
Shutdown all containers in the current Docker Compose project, remove all volumes, and remove any ‘orphaned’ containers (not defined in the Compose file). This is useful if you are testing a new feature branch and want to reset everything so you can start a clean build with
docker-compose up --build. I run this command several times a day.
docker-compose down --volumes --remove-orphans
In related Docker news, I’m working on a book and associated training course aimed specifically at fellow PHP developers. I’m hoping to release a draft of both resources later this year.