欢迎来到赛兔子家园

docker使用不同操作系统

Centos7系统组成

我们一直使用vmware虚拟机安装系统,该系统是一个完整的系统文件,包括2部分:

  • linux内核,作用是提供操作系统的基本功能,和机器硬件进行交互、读取磁盘数据、管理网络。
  • centos7发行版,作用是提供软件功能,例如yum安装包管理。

因此linux内核&Centos分行版,就组成了一个系统,让用户使用。

需求:

是否可以灵活替代发行版本,让我们使用不同的系统?然后内核都共用宿主机的内核,上层的发行版,自由替换。

查看系统两大部分:

查看当前宿主机发行版:cat  /etc/redhat-release

查看内核版本:uname -r

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)
[root@localhost ~]# uname -r
3.10.0-1160.el7.x86_64
[root@localhost ~]#   

说明:docker镜像都使用的同一套内核。

使用docker来切换不同的发行版

利用docker获取不同的发行版镜像,内核使用的都是宿主机的内核。

示例1:获取CentOS(8.3.201)发行版镜像且进入该容器中

先查看宿主机发行版本:

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

命令:docker pull ubuntu

[root@localhost ~]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
35807b77a593: Pull complete 
Digest: sha256:9d6a8699fb5c9c39cf08a0871bd6219f0400981c570894cd8cbea30d3424a31f
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

docker images 查看本地镜像

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
redis        latest    02c7f2054405   45 hours ago   105MB
nginx        latest    822b7ec2aaf2   2 days ago     133MB
ubuntu       latest    fb52e22af1b0   5 days ago     72.8MB
nginx        <none>    08b152afcfae   6 weeks ago    133MB
centos       latest    300e315adb2f   9 months ago   209MB

运行容器且进入容器内

参数解释

-i  交互式命令操作

-t  开启一个终端

base 进入容器后执行的命令,base解析器用来解析我们输入的linux命令

运行id=300e315adb2f的centos且进入容器内

[root@localhost ~]# docker run  -it 300e315adb2f bash

宿主机发行版:CentOS Linux release 7.9.2009 (Core)

容器内centos发行版:CentOS Linux release 8.3.201

不想使用centos了,想使用ubuntu怎么办?

先使用exit退出当前容器空间

 查看已经返回到宿主机

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

示例1:获取Ubuntu发行版镜像且进入该镜像中

查看本地镜像库中是否已经安装ubuntu镜像,命令:docker images

拉取安装ubuntu镜像命令:docker pull ubuntu

进入ubuntu镜像空间:docker run -it ubuntu bash

查看是否已经进入ubuntu容器中。通过查看发行版本信息:

查看命令:cat /etc/lsb-release 

root@97213a4c348b:/# cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.3 LTS"
root@97213a4c348b:/# 

如图说明已经就人ubuntu容器中

总结
  1. 一个完整的系统,是由Linux的内核&发行版,才组成了一个可以使用的完整的系统。
  2. 利用docker容器,可以获取不同的发行版镜像,然后基于该镜像运行出各种容器去使用。

 

posted on 2021-09-04 19:11  赛兔子  阅读(2061)  评论(0编辑  收藏  举报

导航