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容器,可以獲取不同的發行版鏡像,然后基於該鏡像運行出各種容器去使用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM