简体   繁体   中英

Docker can't connect to docker daemon macOS

I was trying to run docker run hello-world to test if my docker installation works. I used then the command brew services start docker-machine to start docker machine via Homebrew. However, I get this error:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

And every time I want to connect to the docker machine I have to do the steps stated in this post: Docker can't connect to docker daemon to be able to use my docker-machine:

docker-machine start # Start virtual machine for docker
docker-machine env  # It's helps to get environment variables
eval "$(docker-machine env default)" # Set environment variables

I don't understand why I have to do these steps every time I want to run docker on my virtual machine.

Is there a specific reason you are using docker-machine instead of the Docker Desktop app? With the latter one, many things like spinning up a virtual machine, setting config and mounting volumes happen in the background.

For details check https://www.docker.com/products/docker-desktop (make sure to uninstall the tooling you currently use)

Answering your specific question on docker-machine env :

The docker CLI tool interacts with the daemon process via unix or tcp sockets. The default setting of the cli is to look for a unix socket at the path you mentioned ( unix:///var/run/docker.sock ).

Because the docker daemon cannot run natively on MacOS, you need to use some linux virtual machine to be able to use docker on a Mac. Docker-machine is the old way of doing this, which is basically a tool that spins up a new virtual machine that gets some (rather) random ip address. If your MacOS-local CLI now wants to connect to this machine, it needs to use the tcp socket on this specific IP address. The docker-machine env command basically creates some environment variables for your docker CLI to overwrite the default setting of unix:///var/run/docker.sock . Besides the TCP socket URL, it also configures some certificates to be used to authenticate against the docker socket of the VM. All those env variables configure the docker CLI tool.

The eval command automatically applies those environment varibale exports to the currently active shell. Those values are ephemeral and do not persist over restarts or when opening up a new shell.

You could possibly put those values into your shell configuration so that they are applied automatically (eg in .bashrc or .zshrc ). But as the IP address might not be stable, you may run into issues where your shell points to an old configuration. That's why you might execute this command each and every time to have the current setup.

For details on docker-machine env see the docs at https://docs.docker.com/machine/reference/env/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM