The Most Useful Docker Commands for Mac You’ve Never Heard of, Because You Need To Make Them

michaelcharles

Michael Charles Aubrey

Posted on March 1, 2024

The Most Useful Docker Commands for Mac You’ve Never Heard of, Because You Need To Make Them

The commands are:

  • docker open - Open Docker Desktop (and therefore start the docker daemon) from your terminal.

  • docker close - Close Docker Desktop (and therefore stop the docker daemon) from your terminal.

  • docker flush - Stop and remove all running and stopped containers.

How Do We Get There?

With a simple function written for Zsh which is provided below.

docker() {
    case "$1" in
        "open")
            echo "Opening the Docker app..."
            open --background -a Docker
            ;;
        "close")
            echo "Stopping currently running Docker containers..."
            command docker ps -q | xargs -r -L1 command docker stop
            if [ $? -eq 0 ]; then
                echo "Closing the Docker app..."
                test -z "$(command docker ps -q 2>/dev/null)" && osascript -e 'quit app "Docker"'
            else
                echo "Failed to stop containers."
            fi
            ;;
        "flush")
            echo "Stopping and removing all containers."
            command docker container stop $(command docker ps -a -q) && command docker container rm $(command docker ps -a -q)
            if [ $? -eq 0 ]; then
                echo "Done."
            else
                echo "Failed to flush containers."
            fi
            ;;
        *)
            command docker "$@"
            ;;
    esac
}
Enter fullscreen mode Exit fullscreen mode

To make the commands detailed earlier accessible in your terminal, incorporate them into your Zsh configuration. This can be achieved by appending the script to your ~/.zshrc file. Alternatively, you can add it to another file that ~/.zshrc sources. Begin by opening the ~/.zshrc file located in your home directory with your preferred text editor. Then, simply copy and paste the script to the end of this file.

But Why Though?

I've saved this section for last because I assume that if you've found your way here, you likely have your own reasons for seeking out such functionality.

However, mine started back when I was working on a less powerful Macbook Air back in 2019 or so. I didn't want to run Docker Desktop in the background all of the time, but I wanted an easy way to start and stop the Docker daemon from the terminal.

Additionally, sometimes I wanted to bring down a project spun up by docker compose, but I didn't necessarily want to change directories to the project where the docker-compose.yml file was.

While these might seem like trivial issues, I've been pleasantly surprised by the significant utility these commands have provided over the years. I wanted to share them in the hope they might be useful to others.

💖 💪 🙅 🚩
michaelcharles
Michael Charles Aubrey

Posted on March 1, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related