DEV Community

Cover image for The Most Useful Docker Commands for Mac You’ve Never Heard of, Because You Need To Make Them
Michael Charles Aubrey
Michael Charles Aubrey

Posted on

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.

Top comments (4)

Collapse
 
moopet profile image
Ben Sinclair

I leave docker running all the time and leave my macbook with the lid closed. My workflow is otherwise pretty similar to yours.

I don't think there's any difference between running docker container stop and docker stop, and I can't think of a reason you'd need to attempt to stop containers which aren't running (what the -a gives you), so mine looks like this:

docker stop $(docker ps -q)

I call my version "undock", because I like making it obvious what're official docker commands and what're my additions - I have a very small brain and don't want to get used to expecting subcommands which don't exist on other environments!

Collapse
 
shricodev profile image
Shrijal Acharya

Why just Mac? Shouldn't it work with Linux or WSL? I believe it should.

Collapse
 
michaelcharles profile image
Michael Charles Aubrey

You could make such a script for Linux, but it would need to be altered. The script I posted here is based on opening the Docker Desktop app via the command line. On Linux, you'd probably just want to start or stop the daemon (systemctl start docker and systemctl stop docker). The flush command might work out of the box, especially if you're running Zsh on your Linux machine.

Collapse
 
dmuth profile image
Douglas Muth

This is neat to see. One reason why I got into Orbstack not too long ago was because it comes with a command-line app that lets you start and stop the app like what's shown here.