Docker每日一阅(1):在Win10平台顺利运行hello-world
文章目录
任务描述
- Docker在Win10里顺利运行起来
- 顺利运行
docker container run hello-world
- 理解其中的逻辑
- 理解image(镜像)和container(容器)的区别
- 理解
docker ps
的含义 - 多使用
help
指令自己寻找答案
过程
docker在Win10上的安装参考官网以及runoob docker教程。写的都非常详细。
此外,我很建议大家使用VSCode来进行接下来的操作。当然,你也可以用你喜欢的平台/IDE。
顺利运行hello-world
第一天,我们来验证一下Docker可以在Win10平台运行,我们打开VSCode,新建一个terminal。输入:
docker container run hello-world
下面是我的terminal打印出来的日志:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:97a379f4f88575512824f3b352bc03cd75e239179eea0fecc38e597b2209f49a
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 Desktop中你会看到新建的一个镜像:
理解其中的逻辑
需要注意这条简单的指令背后的逻辑:
我们在terminal中运行的Docker引擎试图找到一个名为hello world的image(镜像)。由于我们本地没有存储这个镜像(Unable to find image 'hello-world:latest' locally
),所以Docker引擎到Docker Hub去查找名为“hello world”的镜像。找到了之后,将其pull下来,然后在container(容器)中运行。hello world
的唯一功能是在terminal中打印Hello from Docker!
,然后容器停止运行。(大家要搞清楚镜像和容器的区别哦)
我们再读一遍上面一个章节的日志,
镜像和容器的区别
网上有很多解释,我觉得最通俗易懂的就是一句话:镜像你可以把它看成类,而容器可以看做是类的实例化对象。一个类可以有多个对象,同理,一个镜像可以有多个容器。更详细地描述,可以参考网上的解读。
理解docker ps
的含义
我们可以等几秒,然后尝试在terminal中输入
docker ps
发现啥都没有。再尝试输入
docker ps
发现有一个容器,状态显示的是Exited
:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
de7691d17fb5 hello-world "/hello" 35 minutes ago Exited (0) 34 minutes ago amazing_pare
因为docker ps
命令可以用来列出Docker容器的相关信息。 默认显示正在运行的容器。我们虽然run了hallo world,但这个程序打印完之后,这个容器也就停止了。所以docker ps
命令什么都没有显示。但docker ps -a
返回返回所有的容器,包括未运行的。所以这个时候就有一条记录了。还有一个需要注意的是,功能上,docker ps
和docker container ls
是一个东西。
使用help指令
如果我们对docker container run
指令的使用感兴趣,可以在terminal键入docker container run --help
来查看更多信息。