Docker基礎學習筆記一:Docker概述和安裝
- 容器的起源
- 什么是容器
- Docker是什么
- Docker容器為什么這么火
- Docker能做什么?
- Docker的安裝(centos8中)
- 卸載
容器的起源
假設你們公司正在秘密研發下一個“今日頭條”APP,我們姑且稱為明日頭條,程式員自己從頭到尾搭建了一套環境開始寫代碼,寫完代碼后程式員要把代碼交給測驗同學測驗,這時測驗同學開始從頭到尾搭建這套環境,測驗程序中出現問題程式員也不用擔心,大可以一臉無辜的撒嬌,“明明在人家的環境上可以運行的”,
測驗同學測完后終于可以上線了,這時運維同學又要重新從頭到尾搭建這套環境,費了九牛二虎之力搭建好環境開始上線,糟糕,上線系統就崩潰了,這時心理素質好的程式員又可以施展演技了,“明明在人家的環境上可以運行的”,
從整個程序可以看到,不但我們重復搭建了三套環境還要迫使程式員轉行演員浪費表演才華,典型的浪費時間和效率,聰明的程式員是永遠不會滿足現狀的,因此又到了程式員改變世界的時候了,容器技術應運而生,
什么是容器
容器技術起源于Linux開源平臺,并且經歷了比較長時間的發展階段,其中最重要的成果是Linux容器(Linux Containers,LXC)技術,
IBM DeveloperWorks網站關于容器技術的描述是這樣的:“容器有效地將由單個作業系統管理的資源劃分到孤立的組中,以更好地在孤立的組之間平衡有沖突的資源使用需求,與虛擬化相比,這樣既不需要指令級模擬,也不需要即時編譯,容器可以在核心CPU本地運行指令,而不需要任何專門的解釋機制,此外,也避免了準虛擬化(paravirtualization)和系統呼叫替換中的復雜性,”
直白的翻譯過來就是這樣的:如果認為虛擬機是模擬運行的一整套作業系統(包括內核、應用運行態環境和其他系統環境)和跑在上面的應用,那么容器就是獨立運行的一個(或一組)應用,以及它們必需的運行環境,
容器中運行的就是一個或者多個應用程式,以及應用運行所需要的環境,容器直接運行在作業系統內核之上的用戶空間,容器技術可以讓多個獨立的用戶空間運行在同一臺宿主機上,容器既可以運行在物理機也可以運行在虛擬機上,當然也可以運行在公有云主機上,
Docker是什么
Docker 是一個開源的應用容器引擎,讓開發者可以打包他們的應用以及依賴包到一個可移植的鏡像中,然后發布到任何流行的 Linux或Windows 機器上,也可以實作虛擬化,容器是完全使用沙箱機制,相互之間不會有任何介面,
Docker基于Go語言開發的!開源專案!
一個完整的Docker有以下幾個部分組成:
- DockerClient客戶端
- Docker Daemon守護行程
- Docker Image鏡像
- DockerContainer容器
Docker容器為什么這么火
在容器技術出來之前,我們都是使用虛擬機技術!
虛擬機:在window中裝一個VMware,通過這個軟體我們可以虛擬出來一臺或者多臺電腦!笨重!
虛擬機也屬于虛擬化技術,Docker容器技術,也是一種虛擬化技術!
Docker容器技術:可以獨立運行的一個(或一組)應用,以及它們必需的運行環境
Docker能做什么?
比較Docker和虛擬機技術的不同:
- 傳統虛擬機,虛擬出一條硬體,運行一個完整的作業系統,然后在這個系統上安裝和運行軟體
- 容器內的應用直接運行在宿主機的內容,容器是沒有自己的內核的,也沒有虛擬我們的硬體,所以就輕便了
- 每個容器間是互相隔離,每個容器內都有一個屬于自己的檔案系統,互不影響
Docker的安裝(centos8中)
docker官網:https://www.docker.com/
官方安裝檔案url:https://docs.docker.com/engine/install/centos/
注意:此操作都是在root用戶下操作,如果權限不夠在centos操作命令時輸入sudo;
環境準備:
Linux要求內核3.0以上
[root@localhost ~]uname -r #查看內核版本
4.18.0-305.19.1.el8_4.x86_64
[root@localhost ~]cat /etc/os-release #查看系統版本
NAME="CentOS Linux"
VERSION="8"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
安裝
#使用yum下載需要的安裝包
[root@localhost ~]yum install -y yum-utils
Last metadata expiration check: 11:30:39 ago on Sun 26 Sep 2021 02:28:03 AM CST.
Package yum-utils-4.0.18-4.el8.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!
#設定鏡像的倉庫,官方檔案默認的是國外的,這里用的是國內的
[root@localhost ~]yum-config-manager \
> --add-repo \
> https://download.docker.com/linux/centos/docker-ce.repo
Adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
#更新系統,為了下載最新的docker
[root@localhost ~] yum -y update
Docker CE Stable - x86_64 7.4 kB/s | 15 kB 00:02
Dependencies resolved.
Nothing to do.
Complete!
#安裝docker相關的 docker-ce 社區版 而ee是企業版 containerd.io是容器,因為我已經安裝過了
[root@localhost ~]yum install docker-ce docker-ce-cli containerd.io
Last metadata expiration check: 0:01:37 ago on Sun 26 Sep 2021 02:13:27 PM CST.
Package docker-ce-3:20.10.8-3.el8.x86_64 is already installed.
Package docker-ce-cli-1:20.10.8-3.el8.x86_64 is already installed.
Package containerd.io-1.4.9-3.1.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
#啟動docker
[root@localhost ~]# systemctl start docker
# 使用docker version查看是否安裝成功
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 20.10.8
API version: 1.41
Go version: go1.16.6
Git commit: 3967b7d
Built: Fri Jul 30 19:53:39 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.8
API version: 1.41 (minimum version 1.12)
Go version: go1.16.6
Git commit: 75249d8
Built: Fri Jul 30 19:52:00 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.9
GitCommit: e25210fe30a0a703442421b0f60afac609f950a3
runc:
Version: 1.0.1
GitCommit: v1.0.1-0-g4144b63
docker-init:
Version: 0.19.0
GitCommit: de40ad0
測驗hello-word
[root@localhost ~] docker run hello-world
Unable to find image 'hello-world:latest' locally #沒有這個鏡像,需要從外網下載
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:393b81f0ea5a98a7335d7ad44be96fe76ca8eb2eaa76950eb8c989ebf2b78ec0
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/ #下載的真實地址
#查看一下下載的這個hello-world鏡像
[root@localhost ~]docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 2 days ago 13.3kB
卸載
#1. 卸載依賴
yum remove docker-ce docker-ce-cli containerd.io
#2. 洗掉資源
rm -rf /var/lib/docker
# /var/lib/docker 是docker的默認作業路徑!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/303143.html
標籤:其他
