本文已迁移到我的新博客地址,欢迎访问:
blog.favorstack.io
Docker安装完毕,我们就可以试着来运行一些命令了,看看docker可以干什么。
(一) 创建一个容器
首先,让我们运行一个最简单的容器,hello-world。如果安装没有问题,并运行正确的话,应该会出现下面的结果:
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c04b14da8d14: Pull complete
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
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/engine/userguide/
可以看到,上述结果给了我们很多信息,首先,打印出了一条信息:Hello from Docker!
接着简单描述了一下产生该条信息所经历的一系列步骤:
1 Docker客户端连接到守护进程
2 Docker守护进程从Docker Hub拉取了hello-world镜像
3 Docker守护进程从该镜像创建了一个容器,来执行了命令并输出了我们当前看的这条消息
4 Docker守护进程将容器中的标准输出转发到你的终端。
需要补充一点的是,docker在运行一个容器时,首先会检查本地是否存在指定的镜像,如果有,则直接运行,如果没有,才会去Docker Hub上拉取。这也就是命令结果第一行为什么会显示Unable to find image 'hello-world:latest' locally
的原因。
接下来,我们可以看一下目前所有的容器:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a058b3cde45 hello-world "/hello" 3 m