Docker 基础教程:Mac 下如何正确安装 docker + docker-compose + docker-machine? 0 个改进

开始之前你需要: Wiki:Homebrew:Mac 下安装 Homebrew

本文演示 Mac 下如何通过 Homebrew 来安装 Docker。开始之前你需要 Homebrew,如果你还未安装,请前往 Wiki:Homebrew:Mac 下安装 Homebrew

第一步、安装 Docker

$ brew cask install docker

第二步、启动 Docker

以上会在系统里安装一个 Docker 应用。同时按住  + 空格 键,打开 Spotlight 程序,输入 Docker

第一次打开 Docker 会弹框需要网络权限,选择 OK,接下来输入密码并再次点击 OK

启动后,状态栏里会出现 Docker 小鲸鱼的 icon,此时会在  /usr/local/bin 下生成以下软连接:

  • docker
  • docker-compose
  • docker-credential-desktop
  • docker-credential-osxkeychain
  • docker-machine

执行:

$ ls /usr/local/bin/docker*

输出:


/usr/local/bin/docker                        /usr/local/bin/docker-credential-osxkeychain
/usr/local/bin/docker-compose                /usr/local/bin/docker-machine
/usr/local/bin/docker-credential-desktop

第三步、确认开启

点击状态栏里的小鲸鱼按钮,确认是绿色的 Docker is running.

如何在 Mac 下安装 docker + docker-compose + docker-machine?

第四步、检查是否安装成功

命令:

$ docker run hello-world

输出:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:d1668a9a1f5b42ed3f46b70b9cb7c88fd8bdc8a2d73509bb0041cf436018fbf5
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

查看 Docker 版本:

$ docker version

输出:

Client: Docker Engine - Community
 Version:           19.03.5
 API version:       1.40
 Go version:        go1.12.12
 Git commit:        633a0ea
 Built:             Wed Nov 13 07:22:34 2019
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.5
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.12
  Git commit:       633a0ea
  Built:            Wed Nov 13 07:29:19 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

第五步、安装 VirtualBox

我们会使用 docker-machine 来创建 VirtualBox 虚拟机,如果你还未安装的话,执行以下:

$ brew cask install virtualbox

执行 docker-machine 创建虚拟机:

$ docker-machine create manager

未安装会 VirtualBox 出现:

Running pre-create checks...
Error with pre-create check: "VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path"

安装了 VirtualBox 的情况会出现:

Creating CA: /Users/summer/.docker/machine/certs/ca.pem
Creating client certificate: /Users/summer/.docker/machine/certs/cert.pem
Running pre-create checks...
(manager) Image cache directory does not exist, creating it at /Users/summer/.docker/machine/cache...
(manager) No default Boot2Docker ISO found locally, downloading the latest release...
(manager) Latest release for github.com/boot2docker/boot2docker is v19.03.5
(manager) Downloading /Users/summer/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v19.03.5/boot2docker.iso...
(manager) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(manager) Copying /Users/summer/.docker/machine/cache/boot2docker.iso to /Users/summer/.docker/machine/machines/manager/boot2docker.iso...
(manager) Creating VirtualBox VM...
(manager) Creating SSH key...
(manager) Starting the VM...
(manager) Check network to re-create if needed...
(manager) Found a new host-only adapter: "vboxnet1"
(manager) 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!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env manager

使用以下命令查看创建了的虚拟机:

$ docker-machine ls

输出:

NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER     ERRORS
manager   -        virtualbox   Running   tcp://192.168.99.100:2376           v19.03.5

删除虚拟机:

$ docker-machine rm manager
About to remove manager
WARNING: This action will delete both local reference and remote instance.
Are you sure? (y/n): y
Successfully removed manager

参考

本文为 Wiki 文章,邀您参与纠错、纰漏和优化
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!