Solving permission denied while trying to connect to Docker daemon socket from container in Mac OS
Mac OS is my main development platform and a while back I found myself in a Docker-ception situation — a Docker container tried to create another Docker container on my Mac OS. Oh and behold the following error:
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.38/containers/json: dial unix /var/run/docker.sock: connect: permission denied
Well, error messages are part and parcel of programming so if the code runs on the first try, I’d leave it to your imagination on what that means.
So onto the usual googling, I mean, debugging process.
Turns out the yaml compose file for the service was written on Linux/Ubuntu. But on Mac OS, there’s a socket connection issue between Docker container and the host Docker daemon.
My workaround is to spin up a socat container — a unix tool that creates two bidirectional streams between two endpoints, and use it as a relay. The lines of code in bold below are what I’ve added to the compose file to resolve the problem.
- socat socat:
command: TCP4-LISTEN:2375,fork,reuseaddr UNIX-
<cont on prev line>CONNECT:/var/run/docker.sock
Hope this helps :)