还没开启你的Docker之旅?给我一首歌的时间,带你入门(Mac版)

每一个Linux工程师都是哲学家。

下载Docker-ToolBox

在正式开始之前,请移步到官网下载Docker-ToolBox,然后往下看。

题外话:相信很多朋友在网上会看到Boot2Docker这个工具,也会好奇这东西和官网的Docker-ToolBox有什么联系。B2D是DT出现之前的替代品,以前官网推荐使用的docker安装工具,不过放心,已经在使用B2D的朋友,不要担心,官方有相关手段让你过度到Docker-ToolBox。

讲解指令

假设你的Docker-ToolBox还没下载好。
提前告诉你,安装好Docker-ToolBox后,你就使用三个指令:docker-machine、docker、docker-compose。

  • docker-machine主要负责管理虚拟机,使用docker-machine -h获知详情。
  • docker主要负责管理容器,使用docker -h获知详情。
  • docker-compose用法不详,先留个悬念,这篇博客暂时涉及不到。

常用指令

  1. docker-machine ip ,获取VM的ip地址
  2. docker port ,获取容器端口
  3. docker ps,列出仍在运行的容器

安装

假设你的Docker-ToolBox已经下载好。
双击下载包,一路点击“继续”,直到倒数第二步:

倒数第二步

这里让你选择开始你的docker,左边的是使用终端(Terminal)的方式,右边是通过GUI方式。像我们这样的土鳖,肯定用Terminal啦。
当然你也可以跳过这一步,“继续”下去,最后完成安装。然后在你的Launchpad里选择开始docker的方式。如下:
安装结果

这里我们选择Terminal的方式,点击相应图标后,终端会自动帮你安装一个叫“default”的虚拟机。过程如下:

alpha@Alpha-MacBook-Pro:~$ bash --login '/Applications/Docker/Docker Quickstart Terminal.app/Contents/Resources/Scripts/start.sh'
Machine default already exists in VirtualBox.
Setting environment variables for machine default...


                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/


docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com

到这里你可以运行:docker run hello-world
系统会检测本地是否有”hello-world”这个image,如果没有会从远程仓库拉取。如下:

alpha@Alpha-MacBook-Pro:~$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world

b901d36b6f2f: Pull complete
0a6ba66e537a: Pull complete
Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7
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.
 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 Hub account:
 https://hub.docker.com

For more examples and ideas, visit:
 https://docs.docker.com/userguide/

不出意外的话,恭喜你安装好了。

安装好Docker-ToolBox,直接使用shell也是可以的,效果和上面一样。(选读)

  1. 创建“default”虚拟机:docker-machine create --driver virtualbox default
  2. 显示搭建”default”虚拟机的需要的指令:docker-machine env default
  3. 连接至”default”虚拟机:eval "$(docker-machine env default)"
  4. 验证是否成功:docker run hello-world

小试牛刀——nginx容器

官网上提到,当你启动一个容器的时候,虚拟机会自动关联到你的用户目录:/Users/username

  1. 创建网站根目录:
  • cd $HOME
  • mkdir site
  • cd site
  1. 编写主页:echo "hello docker." > index.html
  2. 开启nginx容器:docker run -d -P -v $HOME/site:/usr/share/nginx/html --name mysite nginx
  3. 查看虚拟机IP:docker-machine ip default
  4. 查看容器port:docker port mystic
  5. 使用浏览器访问得到的ip和port。格式:ip:port.

收笔

好了,《情歌王》已经唱完了。
假设你已经读到这里,那么现在你已经入门docker了,水平和我相当。想要更强?
不定时更新docker系列学习笔记,都会收录进我管理的专题——《程序员周边》,非常希望你能关注。

你可能感兴趣的:(还没开启你的Docker之旅?给我一首歌的时间,带你入门(Mac版))