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:
- Docker Desktop: 2.0.0.3 (31259) - Stable
- Docker Engine: 18.09.2
- OS: MacOS Mojave, 10.14.3
Tested CLIs:
- MacOS Terminal 2.9.1
- iTerm2 3.2.7
- Hyper 2.1.2
- (PhpStorm IDE 2018.3.4)
Previous research:
- I found out that the file 'docker.sock' exists in the path "Macintosh HD/private/var/run/docker.sock". I'm not sure if this was a good idea, but deleting it didn't help. After this Docker still works if started manually.
- All other solutions I found so far are for different problems - more complex setups like nested containers; Docker itself being broken or additional software having issues. These don't relate to my problem.
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:
- Docker is not installed (obviously)
- Docker is not started at all
- Docker is starting (but not finished yet)
- Docker is shutting down (but not finished yet)
This results in these messages:
- "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.
- "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:
- Valkyrie Studios - medium.com
- freeCodeCamp - medium.com
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 :
- As a first step, you can try uninstalling/installing the docker setup.
- 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.
- So, to mitigate that, I went to docker engine settings -> Resources -> Increased Memory and Image Size footprint
Boom. Finally my problem Solved :-)