Docker之获取镜像(一)

1961 篇文章 51 订阅
1221 篇文章 21 订阅

在这里插入图片描述

初玩Docker

docker是开源的应用容器引擎,基于主流的开发语言Go语言而开发,它是基于Apache2.0协议开发。docker的好处在于它可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化,容器完全是使用沙箱隔离的机制,这样保障了不会对宿主机有任何的破坏。毫不夸张的说,正因为有了docker,也就形成了它很完善的生态体系,也可以让微服务架构级的产品全面落地,形成了“软件即服务”的理念,真正意义上解决了Saas化以及Paas化平台级产品的部署难的困境。程序员都是谦虚的,学习什么,都习惯了向世界问好,学习docker也是如此。在搭建docker环境的基础上,执行如下指令,就会输出“Hello from Docker!”

docker run --rm hello-world

执行后,见如下的输出信息:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:9ade9cc2e26189a19c2e8854b9c8f1e14829b51c55a630ee675a5a9540ef6ccf
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 run imageName,在该指令中,其实内部发生了两个过程,第一个是先从docker hub获取hello-world的镜像,然后再执行运行它的输出。

获取镜像步骤

获取镜像的方式可以总结为如下几点,具体可以总结如下:

1、获取该软件的docker镜像,直接可以进行搜索,比如docker pull nginx

2、运行该容器,运行成功后可以启动一个容器,可以nginx服务就运行在一个容器里面了

3、停止容器,删除该镜像

docker容器式的环境更可以看成是一个沙盒的环境。判断docker是否启动,就是在控制台里面输入docker version。使用docker ps是查看是否有容器启动,但是查看本地的docker镜像文件的命令是docker images or docker image ls 执行命令后,具体看如下的信息:

[root@wuyaShare ~]# docker images 
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    feb5d9fea6a5   11 days ago   13.3kB

[root@wuyaShare ~]# docker image ls 
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    feb5d9fea6a5   11 days ago   13.3kB

[root@wuyaShare ~]# docker image ls | grep hello
hello-world   latest    feb5d9fea6a5   11 days ago   13.3kB

获取centos镜像

下面以获取centos的镜像为案例,来演示docker的基本应用,获取镜像的命令为:docker pull imageName,详细见如下:

[root@wuyaShare ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

#查看已获取到的centos的镜像
[root@wuyaShare ~]# docker images 
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       latest    5d0da3dc9764   3 weeks ago   231MB

在如上中可以看到,已经获取了centos的镜像信息,下面详细的说明运行镜像的过程。

运行centos镜像

获取镜像后,就可以运行镜像,每个镜像运行后,都会在容器中生成一条记录,这样确实很不友好的,比如运行了N次,就有N条这样的记录,那么可以加–rm的命令,下面针对镜像运行的命令作一个汇总。

  • -it :开启交互式命令

  • –rm:容器退出时候删除容器的记录,也就是docker ps -a 会查询不到该容器的记录,前提是退出

  • –name:给容器起一个名字

  • -p:映射端口,具体使用案例如:-p 宿主机端口:容器端口

  • -P:映射端口,但是映射的是宿主机任意的端口,也就是未被占用的端口

了解了如上的命令后,下面详细的演示下centos镜像的执行,详细见如下执行的命令行的信息:

[root@wuyaShare ~]# docker run -it --rm centos bash 
[root@5f39d61c6948 /]# echo "Hello Docker!"
Hello Docker!
[root@5f39d61c6948 /]# date
Fri Oct  8 14:56:54 UTC 2021
[root@5f39d61c6948 /]#

也可以查看运行后的容器的记录信息,其实就会发现运行centos的容器ID的信息与如上Linux里面的容器ID是一致的,具体如下所示:

[root@wuyaShare ~]# docker ps -a | grep centos
5f39d61c6948   centos    "bash"    2 minutes ago   Up 2 minutes             boring_dubinsky

在进入的centos里面查询vim没有,下来使用yum的命令来进行安装,详细如下:

[root@wuyaShare ~]# docker run -it --rm centos bash 
[root@5f39d61c6948 /]# echo "Hello Docker!"
Hello Docker!
[root@5f39d61c6948 /]# date
Fri Oct  8 14:56:54 UTC 2021
[root@5f39d61c6948 /]# vim 
bash: vim: command not found
[root@5f39d61c6948 /]# yum install -y vim 
Failed to set locale, defaulting to C.UTF-8
CentOS Linux 8 - AppStream                                           8.0 MB/s | 9.3 MB     00:01
CentOS Linux 8 - BaseOS                                               10 MB/s | 7.5 MB     00:00
CentOS Linux 8 - Extras                                               12 kB/s |  10 kB     00:00
Dependencies resolved.
=====================================================================================================
Package                   Architecture      Version                      Repository            Size
=====================================================================================================
Installing:
vim-enhanced              x86_64            2:8.0.1763-15.el8            appstream            1.4 M
Installing dependencies:
gpm-libs                  x86_64            1.20.7-17.el8                appstream             39 k
vim-common                x86_64            2:8.0.1763-15.el8            appstream            6.3 M
vim-filesystem            noarch            2:8.0.1763-15.el8            appstream             48 k
which                     x86_64            2.21-12.el8                  baseos                49 k

Transaction Summary
=====================================================================================================
Install  5 Packages

Total download size: 7.8 M
Installed size: 30 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm                             288 kB/s |  39 kB     00:00
(2/5): vim-filesystem-8.0.1763-15.el8.noarch.rpm                     1.1 MB/s |  48 kB     00:00
(3/5): vim-enhanced-8.0.1763-15.el8.x86_64.rpm                       6.6 MB/s | 1.4 MB     00:00
(4/5): which-2.21-12.el8.x86_64.rpm                                  1.2 MB/s |  49 kB     00:00
(5/5): vim-common-8.0.1763-15.el8.x86_64.rpm                          17 MB/s | 6.3 MB     00:00
-----------------------------------------------------------------------------------------------------
Total                                                                1.4 MB/s | 7.8 MB     00:05
warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - AppStream                                           1.6 MB/s | 1.6 kB     00:00
Importing GPG key 0x8483C65D:
Userid     : "CentOS (CentOS Official Signing Key) <security@centos.org>"
Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing        :                                                                             1/1
Installing       : which-2.21-12.el8.x86_64                                                    1/5
Installing       : vim-filesystem-2:8.0.1763-15.el8.noarch                                     2/5
Installing       : vim-common-2:8.0.1763-15.el8.x86_64                                         3/5
Installing       : gpm-libs-1.20.7-17.el8.x86_64                                               4/5
Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64                                               4/5
Installing       : vim-enhanced-2:8.0.1763-15.el8.x86_64                                       5/5
Running scriptlet: vim-enhanced-2:8.0.1763-15.el8.x86_64                                       5/5
Running scriptlet: vim-common-2:8.0.1763-15.el8.x86_64                                         5/5
Verifying        : gpm-libs-1.20.7-17.el8.x86_64                                               1/5
Verifying        : vim-common-2:8.0.1763-15.el8.x86_64                                         2/5
Verifying        : vim-enhanced-2:8.0.1763-15.el8.x86_64                                       3/5
Verifying        : vim-filesystem-2:8.0.1763-15.el8.noarch                                     4/5
Verifying        : which-2.21-12.el8.x86_64                                                    5/5
Installed:
gpm-libs-1.20.7-17.el8.x86_64                    vim-common-2:8.0.1763-15.el8.x86_64
vim-enhanced-2:8.0.1763-15.el8.x86_64            vim-filesystem-2:8.0.1763-15.el8.noarch
which-2.21-12.el8.x86_64

Complete!
[root@5f39d61c6948 /]#

安装vim成功后,然后输入exit退出,再次进入,发现还是没有vim,这是因为针对之前的容器并没有进行commit,后续详细的演示这部分的具体应用。

感谢您的阅读,后续会持续更新!

在这里插入图片描述

最后: 可以在公众号:伤心的辣条 ! 免费领取一份216页软件测试工程师面试宝典文档资料。以及相对应的视频学习教程免费分享!,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等。

如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一键三连哦!喜欢软件测试的小伙伴们,可以加入我们的测试技术交流扣扣群:914172719(里面有各种软件测试资源和技术讨论)


好文推荐

转行面试,跳槽面试,软件测试人员都必须知道的这几种面试技巧!

面试经:一线城市搬砖!又面软件测试岗,5000就知足了…

面试官:工作三年,还来面初级测试?恐怕你的软件测试工程师的头衔要加双引号…

什么样的人适合从事软件测试工作?

那个准点下班的人,比我先升职了…

测试岗反复跳槽,跳着跳着就跳没了…

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值