初玩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,后续详细的演示这部分的具体应用。
感谢您的阅读,后续会持续更新!