一、docker介紹及安裝部署


一、docker簡介

1、Docker 是一個開源的應用容器引擎,基於 Go 語言 並遵從 Apache2.0 協議開源。
2、Docker 可以讓開發者打包他們的應用以及依賴包到一個輕量級、可移植的容器中,然后發布到任何流行的 Linux 機器上,也可以實現虛擬化。
3、容器是完全使用沙箱機制,相互之間不會有任何接口(類似 iPhone 的 app),更重要的是容器性能開銷極低。
4、Docker 從 17.03 版本之后分為 CE(Community Edition: 社區版) 和 EE(Enterprise Edition: 企業版),我們用社區版就可以了。
5、Docker是通過內核虛擬化技術(namespaces及cgroups)來提供容器的資源隔離與資源限制。由於Docker通過操作系統層的虛擬化實現隔離(對操作系統的內核有要求),所以Docker容器在運行時,不需要類似虛擬機(VM)額外的操作系統開銷,從而比kvm虛擬機更輕量。也可以把docker理解為一種簡單的打包技術

docker目標

docker的主要目標是"Build,Ship and Run any App,Angwhere",構建,運輸,處處運行
構建:制作docker鏡像,打包容器的所有系統目錄文件
運輸:下載docker鏡像
運行:基於docker鏡像提供的rootfs,啟動容器
總結:只要能運行docker容器,那么docker鏡像中已經安裝好的軟件也可以運行,所以說docker是一種件的打包技術。

應用場景

1、Web 應用的自動化打包和發布。
2、自動化測試和持續集成、發布。
3、在服務型環境中部署和調整數據庫或其他的后台應用。
4、從頭編譯或者擴展現有的 OpenShift 或 Cloud Foundry 平台來搭建自己的 PaaS 環境。

docker優勢

 
1:解決了操作系統和軟件運行環境的依賴例如:nginx和git需要安裝的openssl版本不同,在同一台設備上安裝會造成軟件沖突
2:對於開發人員來說,再也不用擔心不會部署開發環境
3:開發環境,測試環境和生產環境高度一致。
4:讓用戶體驗產品新特性的又一種思路。
5:容器不需要進行硬件虛擬以及運行完整操作系統等額外開銷,Docker 對系統資源的利用率更高。無論是應用執行速度、內存損耗或者文件存儲速度,都要比傳統虛擬機技術更高效。因此,相比虛擬機技術,一個相同配置的主機,往往可以運行更多數量的應用。
6:傳統的虛擬機技術啟動應用服務往往需要數分鍾,而 Docker 容器應用,由於直接運行於宿主內核,無需啟動完整的操作系統,因此可以做到秒級、甚至毫秒級的啟動時間。大大的節約了開發、測試、部署的時間。
7:由於 Docker 確保了執行環境的一致性,使得應用的遷移更加容易。Docker 可以在很多平台上運行,無論是物理機、虛擬機、公有雲、私有雲,甚至是筆記本,其運行結果是一致的。因此用戶可以很輕易的將在一個平台上運行的應用,遷移到另一個平台上,而不用擔心運行環境的變化導致應用無法正常運行的情況。
8:Docker 使用的分層存儲以及鏡像的技術,使得應用重復部分的復用更為容易,也使得應用的維護更新更加簡單,基於基礎鏡像進一步擴展鏡像也變得非常簡單。此外,Docker 團隊同各個開源項目團隊一起維護了一大批高質量的 官方鏡像,既可以直接在生產環境使用,又可以作為基礎進一步定制,大大的降低了應用服務的鏡像制作成本。

Docker與虛擬機的區別

二、docker的架構

1、docker三個基本概念:

•    鏡像(Image):Docker 鏡像(Image),就相當於是一個 root 文件系統。比如官方鏡像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系統的 root 文件系統。
•    容器(Container):鏡像(Image)和容器(Container)的關系,就像是面向對象程序設計中的類和實例一樣,鏡像是靜態的定義,容器是鏡像運行時的實體。容器可以被創建、啟動、停止、刪除、暫停等。
•    倉庫(Repository):倉庫可看着一個代碼控制中心,用來保存鏡像。

Docker 使用客戶端-服務器 (C/S) 架構模式,使用遠程API來管理和創建Docker容器。

Docker 容器通過 Docker 鏡像來創建。

2、docker的鏡像分層

一個完整的Docker鏡像可以支撐一個Docker容器的運行,在Docker容器運行過程中主要提供文件系統數據支撐。

Docker鏡像作為docker中最基本的概念,有以下幾個特性:

    鏡像分層,每個鏡像都由一個或多個鏡像層組成;
    可通過在某個鏡像加上一定的鏡像層得到新鏡像(此過程可通過編寫dockerfile或基於容器Commit實現);
    每個鏡像層擁有唯一鏡像ID;
    鏡像在存儲和使用時共享相同的鏡像層(根據ID),所以在pull鏡像時,已有的鏡像層會自動跳過下載;
    每個鏡像層都是只讀,即使啟動成容器,也無法對其真正的修改,修改只會作用於最上層的容器層;

Docker容器,可以理解為一個或多個運行進程,而這些運行進程將占有相應的內存,相應的CPU計算資源,相應的虛擬網絡設備以及相應的文件系統資源。而Docker容器所占用的文件系統資源,則通過Docker鏡像的鏡像層文件來提供。

3、鏡像與容器的聯系

當啟動一個新的容器時,Docker會加載只讀鏡像,並在其之上添加一個讀寫層,即容器層。

docker 容器=鏡像+可讀層

 

4、鏡像存儲核心技術:聯合文件系統

鏡像的高效存儲:引入聯合文件系統,將鏡像多層文件聯合掛載到容器文件系統。

 

5、鏡像存儲核心技術:寫時復制(COW)

引入寫時復制(copy-on-write),需要修改文件操作時,會先從鏡像里把要寫的文件復制到自己的文件系統(容器的讀寫層)中進行修改。源鏡像層中的文件不會發生變化。

 

三、docker的安裝部署

centos系統

1、安裝環境

[root@inode3 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) 
[root@inode3 ~]# uname -r 3.10.0-862.el7.x86_64

2、centos7使用yum安裝(推薦的方式)

#下載docker-ce的yum源
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
#把docker-ce源的地址修改為清華源的地址
sed -i 's#download.docker.com#mirrors.tuna.tsinghua.edu.cn/docker-ce#g' /etc/yum.repos.d/docker-ce.repo
#更新docker-ce.repo
yum makecache fast
#安裝docker-ce
yum install -y docker-ce

下載安裝指定版本

#查看docker-ce的版本
[root@inode3 ~]# yum list docker-ce.x86_64 --showduplicates | sort -r  
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
Installed Packages
docker-ce.x86_64            3:18.09.2-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.1-3.el7                    docker-ce-stable 
docker-ce.x86_64            3:18.09.0-3.el7                    docker-ce-stable 
docker-ce.x86_64            18.06.3.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.2.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.1.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.0.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            18.03.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.3.ce-1.el7                   docker-ce-stable 
docker-ce.x86_64            17.03.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.0.ce-1.el7.centos            docker-ce-stable 
Available Packages

......

安裝指定版本

yum -y install docker-ce-17.03.2.ce

安裝報錯(虛擬機中可能會遇到,如果沒有報錯請忽略)

Error: Package: docker-ce-17.03.2.ce-1.el7.centos.x86_64 (docker-ce-stable)           Requires: docker-ce-selinux >= 17.03.2.ce-1.el7.centos           
Available: docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch (docker-ce-stable)               docker-ce-selinux = 17.03.0.ce-1.el7.centos           
Available: docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch (docker-ce-stable)               docker-ce-selinux = 17.03.1.ce-1.el7.centos           
Available: docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch (docker-ce-stable)               docker-ce-selinux = 17.03.2.ce-1.el7.centos           
Available: docker-ce-selinux-17.03.3.ce-1.el7.noarch (docker-ce-stable)                   docker-ce-selinux = 17.03.3.ce-1.el7

報錯原因: docker-ce-selinux 版本過低

解決辦法:https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/Packages/網站下載對應版本的docker-ce-selinux,安裝即可

yum -y install https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.3.ce-1.el7.noarch.rpm

重新安裝docker-ce-17.03.2.ce成功

這次安裝文檔版本 docker-18.03.1-ce

(1)下載安裝

mkdir /data
cd /data/
wget -c https://download.docker.com/linux/static/stable/x86_64/docker-18.03.1-ce.tgz
tar -xvf docker-18.03.1-ce.tgz 

(2)配置啟動腳本

vim /usr/lib/systemd/system/docker.service

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process

[Install]
WantedBy=multi-user.target

(3)配置生效環境變量,方便使用docker命令

cat > /etc/profile.d/docker.sh <<EOF
export PATH=/data/docker:$PATH
EOF
source /etc/profile.d/docker.sh

(4)配置docker命令補齊腳本

wget -O /usr/share/bash-completion/completions/docker https://raw.githubusercontent.com/alonghub/Docker/master/Resource/docker 

4、Ubuntu 14.04 16.04 (使用apt-get進行安裝)

(1)安裝最新版本

# step 1: 安裝必要的一些系統工具

sudo apt-get updatesudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common

# step 2: 安裝GPG證書

curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

# Step 3: 寫入軟件源信息

sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

# Step 4: 更新並安裝 Docker-CE

sudo apt-get -y updatesudo apt-get -y install docker-ce 

(2)安裝指定版本的Docker-CE:

# Step 1: 查找Docker-CE的版本:
# apt-cache madison docker-ce
#   docker-ce | 17.03.1~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
#   docker-ce | 17.03.0~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages

# Step 2: 安裝指定版本的Docker-CE: (VERSION 例如上面的 17.03.1~ce-0~ubuntu-xenial)
# sudo apt-get -y install docker-ce=[VERSION]

5、啟動docker

 
[root@inode1 ~]# systemctl start docker
[root@inode1 ~]# ps -ef |grep docker
root      46215      1  2 20:26 ?        00:00:00 /usr/bin/dockerdroot      46218  46215  0 20:26 ?        00:00:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runcroot      46349  37852  0 20:26 pts/0    00:00:00 grep --color=auto docker

四、docker命令無法tab鍵補全

#1、安裝bash-complete
yum install -y bash-completion
#2、刷新文件
source /usr/share/bash-completion/completions/docker
source /usr/share/bash-completion/bash_completion

 

 


免責聲明!

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



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