How to Fix Common Docker Errors on Mac and Linux (2024 Guide)

Troubleshooting Common Docker Errors

Last Updated On - August 2nd, 2024 Published On - Apr 13, 2024

Docker Setup Made Easy: Troubleshooting Common Docker Errors on Mac & Linux


Introduction

Welcome aboard! Setting up Docker can be a breeze, but sometimes you might encounter errors that slow you down. This blog post provides solutions to common Docker errors encountered on Mac and Linux systems. It covers issues like ‘zsh: command not found: docker mac’, ‘docker-credential-desktop’: executable file not found in $PATH’, and permission denied errors. The solutions involve checking the PATH environment variable and ensuring Docker and Docker Compose are installed correctly.


Taming the “docker: command not found” Error


Issue: zsh: command not found: docker

This error indicates your terminal can’t locate the docker command. Here’s how to fix it:

  1. Temporary Fix (Current Terminal Session):
    Open your terminal and run the following command specific to your Docker application:
    • For Visual Studio Code Docker Extension:
      export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
    • For Docker Desktop for Mac:
      export PATH="$PATH:/Applications/Docker.app/Contents/Resources/bin/"

  2. Permanent Fix (System-wide):
    • Using zsh:
      Edit your .zshrc file using a text editor like nano or vi. Add the following line:
      vi ~/.zshrc # or ~/.bashrc

      alias docker="/Applications/Docker.app/Contents/Resources/bin/docker"

      Save the changes and exit the editor using :wq command. In your terminal, run source ~/.zshrc or exec zsh to refresh your shell configuration.

      zsh profile

    • Using bash:
      Edit your .bashrc file and add the same line mentioned above for zsh. Save the changes and source the file using source ~/.bashrc.

  3. Verification:
    Run docker --version in your terminal. If successful, you’ll see the installed Docker version.
    docker --version



Conquering “docker compose: command not found” Errors


Issue: zsh: command not found: docker-compose

Issues: zsh: docker-compose: command not found

Issue: zsh: permission denied: docker-compose

Issue: sudo: docker-compose: command not found

This error signifies your system lacks docker-compose, a companion tool for managing multi-container applications. Here’s how to install it:

Pre-requisite (For Mac Only):

If you don’t have Homebrew, a package manager for macOS, install it using the following command in your terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Installation:

Run the following command in your terminal:

brew install docker-compose

Verification:

Type docker-compose --version in your terminal. If the installation was successful, you’ll see the installed docker-compose version.



Taming “Error response from daemon: Ports are not available”


This error pops up when a port you’re trying to map in your Docker container is already occupied by another application on your system. Here’s how to resolve it:

Identifying the Culprit:

Let us understand this with an example.

Suppose you have turned on mysql on your system using xampp manager. Now you’ve created a database in your docker container which is running on port 3306. You want to map your docker container database(using same port) to your system port no. 3306 which is already occupied by your xampp mysql. In this case you’ll get the following error.

When port 3306 occupied by MySQL process on system
Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:3306 -> 0.0.0.0.0: listen tcp 0.0.0.0:3306: bind: address aready in use

So to free any occupied port from your system, first check the occupied process by that port on system and get the process id(PID).

Use the following command to see which process is using the conflicting port (replace PORT with the actual port number):

#sudo lsof -i :PORT
sudo lsof -i :3306

This will display a list of processes using port 3306.

Freeing the Port:

Locate the process ID (PID) from the list. Then, use the following command to terminate the process (replace PID with the actual process ID):

#sudo kill PID
sudo kill 3217

This will free up the port, allowing your Docker container to use it.

kill process
Stop MySQL
Port 3306 is now free and not occupied by any process on the system


Executable file not found in $PATH


Issue: error getting credentials – err: exec: “docker-credential-desktop”: executable file not found in $PATH, out: “

In this case you have to edit docker’s config.json

execute nano ~/.docker/config.json on your terminal and edit config.json as suggested below

you have to change “credsStore” key to “credStore”

# Before
{
        "auths": {},
        "credsStore": "desktop",
        "currentContext": "desktop-linux"
}

#After
{
        "auths": {},
        "credStore": "desktop",
        "currentContext": "desktop-linux"
}

Press control + X then Y then enter

docker-compose up --build


Docker is not running


Issue: on executing ./vendor/bin/sail up in laravel, it is throwing error “Docker is not running” whereas docker is running on desktop as well as in terminal

Docker successfully running on system
Docker is not running error message when working with laravel sail

Use SAIL_SKIP_CHECKS=true ./vendor/bin/sail build command to create docker build via sail

Docker working with sail



FAQs


I’m on Mac and when I try to run docker commands, I get ‘zsh: command not found: docker mac’. How do I fix this?

This error indicates that your system can’t locate the docker command. The solution is to ensure Docker is installed and added to your PATH environment variable. Refer to the official Docker documentation or the above section for Mac installation instructions and PATH configuration steps.

I’m facing an error ‘docker-credential-desktop’: executable file not found in $PATH’. How can I resolve this?

This error suggests that your system cannot find the ‘docker-credential-desktop’ executable. The solution involves checking your PATH environment variable and ensuring it includes the directory where docker-credential-desktop is installed. You can find specific instructions on adding directories to your PATH in the official Docker documentation or this section.

How can I install Docker Compose?

The blog post recommends checking if Docker Compose is already installed. If not, it suggests steps to install on mac

I’m getting a ‘zsh: permission denied’ error when trying to run Docker commands. What should I do?

This error indicates you might not have the necessary permissions to run Docker commands. There are two possible solutions:
Run the command with sudo: Prefix your Docker command with sudo. However, be cautious using sudo as it grants elevated privileges. Only use it if absolutely necessary.
Switch to a user with Docker permissions: Some systems might require using a user with permissions to manage Docker. Check your Docker documentation for specific instructions on configuring user permissions.

I can’t find the ‘docker’ or ‘docker-compose’ commands even though I’m sure they are installed. What’s wrong?

Even after installation, your system might not immediately recognize the commands. This can happen if the installation directories are not included in your PATH environment variable. The PATH environment variable tells your system where to look for executable files.
Check your PATH: You can check your current PATH by running echo $PATH in your terminal.
Modify your PATH: The blog post likely references instructions on adding directories to your PATH. Follow those steps to ensure the Docker installation directories are included.


Closing Note


Now you possess the knowledge to tackle these common Docker setup hurdles! If you encounter any other issues, feel free to contact me or consult the official Docker documentation for further assistance.