Docker每日一阅(1):在Win10平台顺利运行hello-world

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 psdocker container ls是一个东西。

使用help指令

如果我们对docker container run指令的使用感兴趣,可以在terminal键入docker container run --help来查看更多信息。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

破浪会有时

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值