TheRemoteCoder
TheRemoteCoder

Reputation: 182

How to remove CLI Docker message 'Cannot connect to the Docker daemon ...' on MacOS?

Problem description: Each time I start any CLI on my Mac, a message appears multiple times in it: "Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?"

The message appears only if the Docker application is not running. The Docker software basically works on my machine, so there is no need to fix it: When I start the Docker application manually, the message is not shown; as expected.


Desired solution: I just want to get rid of the message. Is there any way to fix or suppress it; like remove it from some autostart file or folder?

Why? - I prefer to start Docker manually when I need it; in order to save system resources and reduce battery usage.


My setup:

Tested CLIs:

Previous research:


Docker issue in CLI

Upvotes: 0

Views: 721

Answers (3)

Touwer
Touwer

Reputation: 381

I had it too. It was due to a command in my .bash_profile:

alias killimages="docker kill $(docker ps -q)"

at the start Bash seems to run docker if something like this is in an alias. Didn't know that.

Upvotes: 0

user2014334
user2014334

Reputation: 122

I had the same issue. I think, this is a very generic problem and surprisingly hardly anyone of the posts like this on stack-overflow has answered the issue. Below is my finding :

  1. As a first step, you can try uninstalling/installing the docker setup.
  2. But the main twist is to find out the exact issue. For me, it looks like one of my container was crashing the docker engine because of the growing memory footprint.
  3. So, to mitigate that, I went to docker engine settings -> Resources -> Increased Memory and Image Size footprint

Boom. Finally my problem Solved :-)

Upvotes: 1

TheRemoteCoder
TheRemoteCoder

Reputation: 182

Problem

I found a solution and that there are different possible issues.

The file .bash_aliases in the User folder contains aliases for Docker commands. These are read and run (?) each time the CLI starts.

Errors appear in these cases:

  1. Docker is not installed (obviously)
  2. Docker is not started at all
  3. Docker is starting (but not finished yet)
  4. Docker is shutting down (but not finished yet)

This results in these messages:

  1. "Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?"
    • Happens when Docker is not started at all.
  2. "Error response from daemon: Bad response from Docker engine"
    • Happens when Docker is either starting or shutting down.

Solution

Wrapping the aliases with a silenced check did the trick for me on MacOS. This will remove any CLI error message from aliases being not available.

docker_code=$(docker ps &>/dev/null)
docker_status=$?
# echo "Docker status: $docker_status"

if [ "$docker_status" == "0" ]; then
    # OK: Docker has started and is fully running
    alias dcu="docker-compose up"
    alias dcd="docker-compose down"
    alias dcb="docker-compose build"
fi
# else = FAIL: Docker is not available, not running or shutting down

Practical use

This might not make sense for everyone, as aliases are sometimes not available.

Solutions

  • Add a nice info message to inform that they have been skipped (instead of muting the error).
  • Create an alias that allows you to reload the available commands on-the-fly without having to close the CLI:
    # Reload Bash config
    alias rcfg="source ~/.bashrc"
    

Resources & Credits

The above solution is based on these articles:

Upvotes: 2

Related Questions