按照程序世界的惯例,我们应该以HelloWorld的程序为起点开始介绍。那么接下来我们就看看Docker的HelloWorld是如何运行的。
安装 Docker CE
由于我的系统是OSX,个人推荐使用homebrew安装docker,该方式方便减少交互,只需在Terminal敲入以下代码:
brew cask install docker
由于众所周知原因,下载过程比较慢,需要耐心等待。安装结果如下所示:
Updating Homebrew...
==> Satisfying dependencies
==> Downloading https://download.docker.com/mac/stable/23011/Docker.dmg
######################################################################## 100.0%
==> Verifying checksum for Cask docker
==> Installing Cask docker
==> Moving App 'Docker.app' to '/Applications/Docker.app'.
? docker was successfully installed!
预备工作:image仓库的镜像地址
示例程序 HelloWorld 的image文件需要从仓库下载,但国内访问 Docker的官方仓库很慢,还经常断线,所以需要把仓库地址改为国内的镜像站。这里推荐使用官方镜像registry.docker-cn.com。添加方法如下图所示:
应用新的配置后,可以正常获取镜像文件了。
运行 HelloWorld 镜像
接下来是激动人心的时刻,在终端输入以下代码:
docker run hello-world
终端会输出以下信息:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:97ce6fa4b6cdc0790cda65fe7290b74cfebd9fa0c9b8c38e979330d547d22ce1
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://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
那么恭喜,Docker 的 HelloWorld 的入门示例程序已经运行成功了,觉得是不是 so easy 。下篇文章我会写些概念的东西,如 什么是容器 、为什么用容器、容器是怎么工作的等一些基础概念,这些内容是我从网上摘抄过来的,用以理解。