As a full stack developer on macOS, one of my most used tools is Docker Desktop. It drives the container-based workloads and microservices that power many modern cloud-native apps I build.

But working with Docker CLI can get frustrating fast if the docker binary is not available in the PATH environment variable, especially given how often developers switch terminals. This comprehensive guide will teach you how to seamlessly add Docker to the path in macOS and turbocharge your productivity as a developer.

Challenges When Docker is Not in Your Mac‘s System Path

According to Docker‘s 2021 survey, an average developer actively uses Docker CLI in 3-4 different terminal/shell windows daily while building cloud-native applications.

When the docker command is not present in your Mac‘s PATH, you‘ll encounter frustrating issues like:

  • "docker: command not found" errors whenever you try to build, run or access containers in a new shell or different folder location
  • Having to constantly specify the full binary path like /usr/local/bin/docker build which becomes tedious
  • Re-installing Docker in every terminal window before you can actually start developing

All this wastes precious minutes to hours of a developer‘s time even on a weekly basis.

Let‘s learn how to fix this with a simple PATH update once and for all.

Understanding the System PATH Variable in macOS

The PATH environment variable in macOS contains a list of directories separated by a colon : that the terminal searches through when you execute commands.

For example, when I run:

docker build

My zsh shell searches through every directory listed in PATH, executes the first docker binary it finds.

By adding /usr/local/bin path where docker is installed to PATH, we can execute Docker CLI from any location!

Now let‘s get into the implementation.

Step 1 – Identify the Docker Installation Path

First, we need to figure out where exactly Docker binary is installed.

Run the following which command to find the docker executable path:

which docker

On my Mac, I get /usr/local/bin/docker as the output. But your path may differ, so use the actual output from which docker.

Step 2 – Edit .zshrc or .bash_profile

Now we need to add the docker binary path from Step 1 into the correct shell startup script file.

macOS largely has two popular shells – zsh and bash, so you need to add it to ~/.zshrc or ~/.bash_profile respectively.

Let‘s check which shell you currently use:

echo $SHELL

I‘m getting /bin/zsh as output, so I will modify .zshrc:

nano ~/.zshrc

Add the following line at the end of .zshrc using my docker path:

export PATH="$PATH:/usr/local/bin" 

Note: If you use bash instead of zsh, modify the ~/.bash_profile file similarly.

Step 3 – Reload Shell Startup Script

For PATH changes to take effect, you need to reload the shell startup script – .zshrc or .bash_profile:

On zsh:

source ~/.zshrc

On bash:

source ~/.bash_profile

This will load the new $PATH variable with docker path included.

Step 4 – Verify Docker Works in PATH

Open a new terminal and run basic docker commands like:

docker --version
docker build .

It should now work without any command not found errors!

Docker is now available system-wide from any location 🎉. Our mission is accomplished.

Why Add Docker Path Instead of Using Docker Desktop?

You may be wondering – why go through all this work instead of using the convenient Docker Desktop app?

While Docker Desktop streamlines a lot of things, there are some limitations:

  • Desktop dependency hinders use in SSH sessions or CI/CD terminals
  • Resource heavy compared to native docker installs
  • Has background overheads which hurts performance

78% of developers still actively use docker command line since it offers the flexibility to build containers anywhere according to the Docker report.

So adding Docker CLI to PATH caters to this need without sacrificing Mac performance or flexibility in diverse contexts.

Conclusion

Not having docker available in the PATH variable can massively slow down developer workflow and productivity.

But with this short 4 step guide, you can permanently add docker to the PATH on macOS in just 5 minutes:

  1. Use which docker to identify docker binary path
  2. Add this path in .zshrc or .bash_profile
  3. Reload the file with source command
  4. Validate docker works system-wide!

Following industry best practices outlined here means you‘ll eliminate frustrating command not found errors.

So go forth and Dockerize all the things! No more obstacles or wasting precious minutes debugging path issues.

Similar Posts