371

I normally prefer to manage my apps on my OSX with brew

I am able to install docker, docker-compose and docker-machine

docker --version
Docker version 17.05.0-ce, build 89658be
docker-compose --version
docker-compose version 1.13.0, build unknown
docker-machine --version
docker-machine version 0.11.0, build 5b27455

I did not download and run 'Docker for Mac' app.

However when I try to run

> docker run -d -p 80:80 --name webserver nginx
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

I have already checked the sock file

ls -lah /var/run/docker.sock /var/tmp/docker.sock
ls: /var/run/docker.sock: No such file or directory
ls: /var/tmp/docker.sock: No such file or directory

I have also tried this proposed solution: Mac OS X sudo docker Cannot connect to the Docker daemon. Is the docker daemon running on this host?

But I got this error message:

$ eval $(docker-machine env default)
Host does not exist: "default"

Is it possible to get a docker service to run by command line tools?

Community
  • 1
  • 1
Anthony Kong
  • 33,453
  • 37
  • 154
  • 277
  • 5
    Launch the Docker application from Launchpad, will be showing a dialog asking that for run the App, is necessary granted some privileged for this App, after this, the Docker is back to the game. – Rafael Kerr Jan 22 '18 at 00:19
  • In my case there was another user loggedin thats why it was failing. Logout other user and worked then – Raza Ahmed Jun 14 '19 at 17:33

21 Answers21

778

On a supported Mac, run:

brew install --cask docker

Then launch the Docker app. Click next. It will ask for privileged access. Confirm. A whale icon should appear in the top bar. Click it and wait for "Docker is running" to appear.

You should be able to run docker commands now:

docker ps

Because docker is a system-level package, you cannot install it using brew install, and must use --cask instead.

Note: This solution only works for Macs whose CPUs support virtualization, which may not include old Macs.

Edit 2022 - It was brought up in the comments section that there has been a licensing change for Docker Desktop. Please consider alternatives below, and check the comments for details.

friederbluemle
  • 28,615
  • 14
  • 96
  • 99
  • 20
    This should be the accepted answer. Go to the launch pad after install and search docker – Joe Lloyd Aug 14 '17 at 08:24
  • 56
    I did a brew install docker and the app wasn't installed until I ran the command brew cask install docker. Thanks! – jbrahy Sep 12 '17 at 21:09
  • 3
    What happens if the command still doesn't work even after the cask install (and Docker whale shows it's running after password given)? – vol7ron Mar 19 '18 at 03:13
  • @vol7ron Please be more specific. What does not work? Are you seeing any error messages etc? – friederbluemle Mar 23 '18 at 07:19
  • I forget exactly. The desktop app was running, but not loading a container. From the command line nothing worked or knew to reference that app — I’ll try again in the future to get you more specific information. Perhaps the other brewed non-cask items (docker, docker-machine, etc) need to first be uninstalled? – vol7ron Mar 23 '18 at 12:03
  • 1
    It just worked for me, but I ran `brew uninstall docker` before reinstalling with `brew cask install docker`. Then everything worked like a charm. – Michael Delgado Apr 11 '18 at 17:11
  • 2
    This solution only works for Macs whose CPUs support virtualisation. It does *not* work on a 2009 Mac Pro, for example. Could you please edit your answer to include this important fact? – John Y May 06 '18 at 09:34
  • `Error: It seems there is already an App at '/Applications/Docker.app'.` – arntg Jun 06 '18 at 06:58
  • launching docker pap did it for me – Bowofola Oct 23 '18 at 15:49
  • 4
    This is great and all; but is there no way to do this without getting into the GUI? `$ open /Applications/Docker.app` returns `LSOpenURLsWithRole() failed with error -10810 for the file /Applications/Docker.app.` – dlamblin Mar 05 '19 at 13:45
  • 1
    Anyone coming across this, heads up! This will kill your minikube's network settings. So if you are using minikube make sure you know this before going all cask install crazy. Wasted an hour fixing this. – Ligemer Apr 17 '19 at 00:40
  • But why should this be the accepted answer? It's a bunch of GUI add-ons without actually solving the problem through troubleshooting. I think @kenorb's answer should be the accepted one. – SDG Jun 23 '19 at 07:44
  • 1
    There are definite tradeoffs to using Docker Desktop with this approach [as stated here](https://docs.docker.com/docker-for-mac/docker-toolbox/). I believe this answer should be updated to indicate this. I personally chose not to use Docker Desktop, but to each their own. – Chris H. Oct 03 '19 at 22:38
  • @dlamblin try running this command `open --hide --background -a Docker` – Andrew Jun 22 '21 at 02:00
  • 2
    Now that there is a licensing change on Docker Desktop, this is not a good answer. – dres Sep 01 '21 at 12:22
  • @dres - Can you elaborate? How does the answer need to be updated? – friederbluemle Sep 01 '21 at 16:12
  • 3
    @friederbluemle https://docs.docker.com/desktop/mac/release-notes/#new references a new licensing model which requires organizations over a certain size to pay to use the GUI on Mac and Windows. The answer should really be more like https://stackoverflow.com/a/49719638/442837. I also wrote about problems on bigsur here https://mindhaus.medium.com/ditching-docker-for-desktop-on-macos-bf087d5f60f9 – dres Sep 02 '21 at 17:41
  • perfect instructions – eliahonader May 09 '22 at 01:36
  • This is the correct answer, especially for users do not want to tie themselves with Docker Desktop or wanted to use Docker together with Rancher Desktop – paranzana May 11 '22 at 11:10
  • does not seem to work anymore, I get `Error: It seems there is already a Binary at '/usr/local/share/zsh/site-functions/_docker'.` – user5359531 May 17 '22 at 14:46
213

On macOS the docker binary is only a client and you cannot use it to run the docker daemon, because Docker daemon uses Linux-specific kernel features, therefore you can’t run Docker natively in OS X. So you have to install docker-machine in order to create VM and attach to it.

Install docker-machine on macOS

If you don't have docker-machine command yet, install it by using one of the following methods:

  • Using Brew command: brew install docker-machine docker.

  • Manually from Releases page:

    $ curl -L https://github.com/docker/machine/releases/download/v0.16.1/docker-machine-`uname -s`-`uname -m` >/usr/local/bin/docker-machine
    $ chmod +x /usr/local/bin/docker-machine
    

See: Get started with Docker for Mac.

Install Virtualbox

docker-machine relies on VirtualBox being installed and will fail if this isn't the case. If you already have VirtualBox, you can skip this step.

  • Using Homebrew: brew install --cask virtualbox
  • Manually using the releases on Virtualbox.org

You will need to actively accept loading the Virtualbox kernel extension in the OS X Security panel and then proceed to restart the machine for the next commands not to fail with Error: VBoxNetAdpCtl: Error while adding new interface

Configure docker-machine on macOS

Create a default machine (if you don't have one, see: docker-machine ls):

docker-machine create --driver virtualbox default

Then set-up the environment for the Docker client:

eval "$(docker-machine env default)"

Then double-check by listing containers:

docker ps

See: Get started with Docker Machine and a local VM.


Related:

Kevin Burke
  • 55,512
  • 71
  • 177
  • 289
kenorb
  • 137,499
  • 74
  • 643
  • 694
  • 16
    this should be the accepted answer. It clearly outlines a) why it doesn't work, b) instructions to get it working without downloading the UI on OSX and c) reference links for further reading. – user2490003 Jun 20 '19 at 05:51
  • 1
    when you get all above staff done, you'd better add `eval "$(docker-machine env default)"` to your .*rc file of your shell(e.g. .zshrc, .bashrc) – xiaojueguan Nov 17 '20 at 04:40
  • Nice answer, while I'm a newbie, what's the purpose of running `eval "$(docker-machine env default)", and should I add this to my .zshrc file? – VimNing Jun 11 '21 at 23:52
  • @xiaojueguan: Why? – VimNing Jun 12 '21 at 00:02
  • I had to upvote this answer. Docker Desktop is licensed and there was change of Policy. It remains free for small businesses (fewer than 250 employees AND less than $10 million in annual revenue), personal use, education, and non-commercial open source projects. – Pavol Travnik Sep 22 '21 at 10:09
  • 2
    If you are on MacOS and using Virtualbox >=6.1.28 and you are receiving an error along the lines of: `VBoxManage: error: Code E_ACCESSDENIED (0x80070005) - Access denied (extended info not available) VBoxManage: error: Context: "EnableStaticIPConfig(Bstr(pszIp).raw(), Bstr(pszNetmask).raw())" at line 242 of file VBoxManageHostonly.cpp` [Then check out this answer](https://stackoverflow.com/questions/69722254/vagrant-up-failing-for-virtualbox-provider-with-e-accessdenied-on-host-only-netw) – Anthony Fammartino Dec 01 '21 at 21:18
  • 5
    Docker machine is now deprecated, it should be outlined in the answer https://docs.docker.com/machine/ – lvndry Dec 16 '21 at 09:23
51

I had this same issue I solved it in the following steps:

docker-machine restart

Quit terminal (or iTerm2, etc, etc) and restart

eval $(docker-machine env default)

I also answered it here

Master p
  • 816
  • 9
  • 8
28

I have Mac OS and I open Launchpad and select docker application. from reset tab click on restart.

Ehsan Barkhordar
  • 2,934
  • 24
  • 28
18

Try this to create default.

docker-machine create default
  • 5
    this just gave me ... docker-machine create default Creating CA: /Users/stens/.docker/machine/certs/ca.pem Creating client certificate: /Users/stens/.docker/machine/certs/cert.pem Running pre-create checks... Error with pre-create check: "VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path" .... which seems wrong since VirtualBox no longer is necessary to run docker on osx – Scott Stensland Jun 06 '17 at 00:54
  • Appears you can use `xhyve` as well, see https://stackoverflow.com/a/42419979/32453 or just install virtualbox :) – rogerdpack Jul 19 '18 at 18:02
  • And.. so what ? – FrankyFred Oct 26 '18 at 17:03
  • 1
    @FrankyFred OK I added an answer to attempt to clarify. – rogerdpack Sep 21 '20 at 19:38
11

on OSX assure you have launched the Docker application before issuing

docker ps

or docker build ... etc ... yes it seems strange and somewhat misleading that issuing

docker --version

gives version even though the docker daemon is not running ... ditto for those other version cmds ... I just encountered exactly the same symptoms ... this behavior on OSX is different from on linux

Scott Stensland
  • 24,813
  • 12
  • 87
  • 96
7

I had docker up to date, docker said it was running, and the diagnosis was good. I needed to unset some legacy environment variable (thanks https://docs.docker.com/docker-for-mac/troubleshoot/#workarounds-for-common-problems )

unset DOCKER_HOST
unset DOCKER_CERT_PATH
unset DOCKER_TLS_VERIFY
Michael
  • 2,903
  • 1
  • 25
  • 64
7

Install minikube and come join the dark side.

https://kubernetes.io/docs/tasks/tools/install-minikube/

$ bash --version
GNU bash, version 5.0.3(1)-release (x86_64-apple-darwin18.2.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

eval $(minikube docker-env)

$ docker run --rm -v ${PWD}:/local swaggerapi/swagger-codegen-cli generate     -i /local/swagger.json     -l python     -o /local/test/
[main] INFO io.swagger.parser.Swagger20Parser - reading from /local/swagger.json
[main] WARN io.swagger.codegen.DefaultGenerator - 'host' not defined in the spec. Default to 'localhost'.

Works great on Macbook Pro 2018 with bash v5. No need to install all the other docker nonsense these days IMO. The VM and install script that comes with minikube can replace docker machine from my experience.

Ligemer
  • 1,662
  • 21
  • 19
  • As a follow up comment, I needed docker as well for another project w/o kube and the docker desktop works great. Just don't start it up always or else kube gets angry from this post. – Ligemer Apr 24 '19 at 19:41
  • minikube and `eval $(minikube docker-env)` does not resolve the issue for me. – Ian Smith Dec 10 '21 at 00:04
  • Is Docker Desktop installed? Try using Wireshark to view the packets and connections. The `env` command will show the environment variables. That should give some insights on the macos networking layers. With minikube, the Kaniko builder is likely a better solution come 2022. – Ligemer Jan 15 '22 at 23:00
6

This problem:

$ brew install docker docker-machine
$ docker ps

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

This apparently meant do the following:

$ docker-machine create default # default driver is apparently vbox:
Running pre-create checks...
Error with pre-create check: "VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path"
$  brew cask install virtualbox
…
$ docker-machine create default 
# works this time
$ docker ps
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
$ eval "$(docker-machine env default)"
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

It finally works.

You can use the “xhyve” driver if you don’t want to install virtual box. Also you can install the “docker app” (then run it) which apparently makes it so you don’t have to run some of the above. brew cask install docker then run the app, see the other answers. But apparently isn't necessary per se.

rogerdpack
  • 56,766
  • 33
  • 241
  • 361
5

I had the same problem. Docker running but couldn't access it through CLI.

For me the problem was solved by executing "Docker Quickstart Terminal.app". This is located in the "/Applications/Docker/" folder. As long as I work in this instance of the Terminal app Docker works perfectly. If a second window is needed I have to run the "Quickstart" app once more.

I have a Docker for Mac installation. Therefore I am not sure if my solution is valid for a Homebrew installation.

The "Docker Quickstart Terminal" app seems to be essentially some applescripts to launch the terminal app and a bash start script that initialise all the necessary environment variables.

Hope this helps someone else !

beo
  • 51
  • 1
  • 3
5

You should execute script for install docker and launch it from command line:

brew install --cask docker
sudo -H pip3 install --upgrade pip3
open -a Docker
docker-compose ... 

after that docker-compose should work

Boris Azanov
  • 4,224
  • 1
  • 13
  • 24
2

I was facing similar issue on my mac, and I found that docker wasn't running in my machine, I just went to applications and invoked whale and then it worked .

Rama Sharma
  • 98
  • 11
1

This did the trick for me:

open --background -a Docker

Takes some seconds until you can connect to docker.

I am on a Mac OS Big Sur.

MattSt
  • 748
  • 2
  • 10
  • 32
0

I first tried docker and docker-compose via homebrew, but it had the problem listed here. I had to install docker's official install from https://docs.docker.com/docker-for-mac/install/ and then everything worked as expected.

Thomas Kessler
  • 1,607
  • 3
  • 16
  • 25
0

i simply had to run spotlight search and execute the Docker application under /Applications folder which brew cask install created. Once this was run it asked to complete installation. I was then able to run docker ps

wired00
  • 13,134
  • 7
  • 66
  • 68
0

Tried the following and its working fine now

$ docker-machine create default
(default) Creating VirtualBox VM...
(default) Creating SSH key...
(default) Starting the VM...
(default) Check network to re-create if needed...
(default) Found a new host-only adapter: "vboxnet0"
(default) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!

Then

$ docker-machine env default

Finally

$ eval $(docker-machine env default)
$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
muTheTechie
  • 1,090
  • 12
  • 22
0

Docker was designed to run on Linux. It can, however, run in a VM on macOS. To do so is a bit of a kludge and that's perhaps why Docker Desktop exists in the first place. But that doesn't mean you can't enjoy using Docker from the comfort of the command line on a Mac.

Some options to consider:

  1. You could use lima and add an alias doocker='lima nerdctl' to your bash profile.
  2. You could install a Linux VM on your machine and use it instead (not recommended).
  3. Dual-boot Linux on Mac. Even machines with the T2 and M1 chips are supported now.
vhs
  • 7,906
  • 2
  • 61
  • 66
0

With the updated version of VirtualBox (6.1.32r149290), the command docker-machine create --driver virtualbox default doesn't work, there is an error with auto-generated IP for the Virtual VM, explanation is here https://stackoverflow.com/a/69745931/2917630 .

This command helped me to resolve the issue: docker-machine create --driver virtualbox --virtualbox-hostonly-cidr "192.168.59.3/24" default.

ultraon
  • 1,880
  • 2
  • 21
  • 26
0

Just opening Docker Desktop worked for me.

arush436
  • 1,490
  • 17
  • 18
-1

I run brew install docker-machine; then the problem is solved. The latest docker version does not have docker-machine included.

htlbydgod
  • 163
  • 1
  • 5
-2

installing docker-machine was enough for me to solve the problem.

ahmetbcakici
  • 303
  • 3
  • 16