docker介紹及安裝部署
阿新 • • 發佈:2021-06-11
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 環境。
1:解決了作業系統和軟體執行環境的依賴例如:nginx和git需要安裝的openssl版本不同,在同一臺裝置上安裝會造成軟體衝突 2:對於開發人員來說,再也不用擔心不會部署開發環境 3:開發環境,測試環境和生產環境高度一致。 4:讓使用者體驗產品新特性的又一種思路。 5:容器不需要進行硬體虛擬以及執行完整作業系統等額外開銷,Docker 對系統資源的利用率更高。無論是應用執行速度、記憶體損耗或者檔案儲存速度,都要比傳統虛擬機器技術更高效。因此,相比虛擬機器技術,一個相同配置的主機,往往可以執行更多數量的應用。 6:傳統的虛擬機器技術啟動應用服務往往需要數分鐘,而 Docker 容器應用,由於直接運行於宿主核心,無需啟動完整的作業系統,因此可以做到秒級、甚至毫秒級的啟動時間。大大的節約了開發、測試、部署的時間。 7:由於 Docker 確保了執行環境的一致性,使得應用的遷移更加容易。Docker 可以在很多平臺上執行,無論是物理機、虛擬機器、公有云、私有云,甚至是筆記本,其執行結果是一致的。因此使用者可以很輕易的將在一個平臺上執行的應用,遷移到另一個平臺上,而不用擔心執行環境的變化導致應用無法正常執行的情況。 8:Docker 使用的分層儲存以及映象的技術,使得應用重複部分的複用更為容易,也使得應用的維護更新更加簡單,基於基礎映象進一步擴充套件映象也變得非常簡單。此外,Docker 團隊同各個開源專案團隊一起維護了一大批高質量的 官方映象,既可以直接在生產環境使用,又可以作為基礎進一步定製,大大的降低了應用服務的映象製作成本。
Docker與虛擬機器的區別
1、docker三個基本概念:
• 映象(Image):Docker 映象(Image),就相當於是一個 root 檔案系統。比如官方映象 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系統的 root 檔案系統。 • 容器(Container):映象(Image)和容器(Container)的關係,就像是面向物件程式設計中的類和例項一樣,映象是靜態的定義,容器是映象執行時的實體。容器可以被建立、啟動、停止、刪除、暫停等。 • 倉庫(Repository):倉庫可看著一個程式碼控制中心,用來儲存映象。
Docker 容器通過 Docker 映象來建立。
2、docker的映象分層
一個完整的Docker映象可以支撐一個Docker容器的執行,在Docker容器執行過程中主要提供檔案系統資料支撐。
Docker映象作為
映象分層,每個映象都由一個或多個映象層組成; 可通過在某個映象加上一定的映象層得到新映象(此過程可通過編寫dockerfile或基於容器Commit實現); 每個映象層擁有唯一映象ID; 映象在儲存和使用時共享相同的映象層(根據ID),所以在pull映象時,已有的映象層會自動跳過下載; 每個映象層都是隻讀,即使啟動成容器,也無法對其真正的修改,修改只會作用於最上層的容器層;
3、映象與容器的聯絡
當啟動一個新的容器時,Docker會載入只讀映象,並在其之上新增一個讀寫層,即容器層。
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
#下載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
報錯原因:
解決辦法:在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)配置啟動指令碼
[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
wget -O /usr/share/bash-completion/completions/docker https://raw.githubusercontent.com/alonghub/Docker/master/Resource/docker
(1)安裝最新版本
#step 1: 安裝必要的一些系統工具
sudo apt-get updatesudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
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]
[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
#1、安裝bash-complete yum install -y bash-completion #2、重新整理檔案 source /usr/share/bash-completion/completions/docker source /usr/share/bash-completion/bash_completion