CentOS 7 安裝 Docker CE

翻譯自 Get Docker CE for CentOS | Docker Docs,以 Docker 官方文檔 為準(zhǔn)
點(diǎn)擊查看我的博客原文

安裝前的準(zhǔn)備

系統(tǒng)要求

  • 官方推薦使用 CentOS 7維護(hù)版本,已經(jīng)歸檔的版本不受支持或未經(jīng)測(cè)試
  • 需要啟用centos-extrasrepository。在 CentOS 7 中這個(gè)倉(cāng)庫(kù)是默認(rèn)啟用的,如果之前有將其禁用,則需要重新啟用
  • 推薦使用overlay2作為 Docker 的存儲(chǔ)驅(qū)動(dòng)

卸載舊版本

舊版本的 Docker 在 CentOS 中的包名為dockerdocker-engine。如果之前安裝了 Docker 的舊版本,需要先卸載舊版 Docker 及相關(guān)依賴(lài)

> sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

yum提示卸載成功沒(méi)有找到相關(guān)包,即可進(jìn)行下一步操作。

注意/var/lib/docker/目錄下的內(nèi)容,包括鏡像、容器、卷組、網(wǎng)絡(luò)等文件將被保留。Docker CE 的新包名為docker-ce。

安裝 Docker CE

有以下三種方法安裝 Docker CE,可根據(jù)實(shí)際需要選擇:

  1. 建立 Docker 倉(cāng)庫(kù):安裝過(guò)程及后續(xù)的更新方便,Docker 官方推薦
  2. 下載 RPM 包手動(dòng)安裝:手動(dòng)管理更新。適合離線環(huán)境。
  3. 通過(guò)安裝腳本自動(dòng)安裝:適合測(cè)試及開(kāi)發(fā)環(huán)境。

方法 1:建立 Docker 倉(cāng)庫(kù)

首次安裝 Docker CE 前需要建立 Docker repository,之后可通過(guò)倉(cāng)庫(kù)安裝并更新 Docker。

建立倉(cāng)庫(kù)

1.安裝所需軟件包yum-utils提供了yum-config-manager工具,存儲(chǔ)驅(qū)動(dòng)devicemapper則依賴(lài)于device-mapper-persistent-datalvm2

> sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

2.使用以下命令建立stable版本的 repository

> sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

3.可選啟用edgetest倉(cāng)庫(kù)。這些倉(cāng)庫(kù)包含在docker.repo文件中,但默認(rèn)是禁用的??梢詫⑺鼈兣cstable倉(cāng)庫(kù)共同啟用。

> sudo yum-config-manager --enable docker-ce-edge
> sudo yum-config-manager --enable docker-ce-test

使用帶--disable參數(shù)的yum-config-manager命令即可禁用edgetest倉(cāng)庫(kù),使用--enable參數(shù)則會(huì)重新啟用。例如下面的命令將禁用edge倉(cāng)庫(kù):

> sudo yum-config-manager --disable docker-ce-edge

Docker17.06版本開(kāi)始,stable倉(cāng)庫(kù)的 releases 也會(huì)推送至edgetest倉(cāng)庫(kù)中。
點(diǎn)擊此處查看 Docker 官方關(guān)于stableedge的說(shuō)明。

安裝 Docker CE

1.使用以下命令安裝最新版 Docker CE

> sudo yum install docker-ce

如果提示是否接受 GPG 密鑰,則需驗(yàn)證密鑰指紋是否符合下面的內(nèi)容,若符合即可點(diǎn)擊 accept 繼續(xù)安裝:

060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35

如果啟用了多個(gè) Docker 倉(cāng)庫(kù),并且在yum installyum update命令中沒(méi)有指明版本,則會(huì)安裝所有倉(cāng)庫(kù)中版本號(hào)最新的 Docker。

2.要安裝指定版本的 Docker CE,則需要從倉(cāng)庫(kù)中列出所有可用的版本,再根據(jù)需要選擇安裝:

> yum list docker-ce --showduplicates | sort -r

docker-ce.x86_64            18.09.0.ce-1.el7.centos             docker-ce-stable

此時(shí)安裝包名的格式為docker-ce-<VERSION STRING>。例如安裝18.03.0版本的 Docker CE:

> sudo yum install docker-ce-18.03.0.ce

此時(shí) Docker 應(yīng)該已經(jīng)安裝完成,但還沒(méi)有啟動(dòng)。新的用戶組docker也已創(chuàng)建,目前為空。

3.啟動(dòng) Docker

> sudo systemctl start docker

4.運(yùn)行hello-world鏡像以驗(yàn)證 Docker 是否正確安裝

> sudo docker run hello-world

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/

升級(jí) Docker CE

如需升級(jí) Docker CE,則可根據(jù)上述安裝教程,選擇安裝最新版docker-ce,即可完成升級(jí)。

方法 2:下載 RPM 包手動(dòng)安裝

安裝 Docker CE

如果無(wú)法使用 Docker 倉(cāng)庫(kù),可以下載.rpm安裝包手動(dòng)安裝 Docker CE。

1.前往https://download.docker.com/linux/centos/7/x86_64/stable/Packages/,下載對(duì)應(yīng)版本的 RPM 安裝包

2.使用yum命令安裝 RPM 包

> sudo yum install /path/to/package.rpm

3.啟動(dòng) Docker

> sudo systemctl start docker

4.運(yùn)行hello-world鏡像以驗(yàn)證 Docker 是否正確安裝

> sudo docker run hello-world

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/

升級(jí) Docker CE

如需升級(jí),則可下載新版本的 RPM 安裝包,使用yum upgrade命令升級(jí):

> sudo yum -y upgrade /path/to/package.rpm

方法 3:通過(guò)安裝腳本自動(dòng)安裝

通過(guò) Docker 提供的一鍵安裝腳本可以在開(kāi)發(fā)環(huán)境中快速安裝 Docker CE,且無(wú)需交互。get.docker.comtest.docker.com 分別對(duì)應(yīng)edgetest版本,腳本源碼存放在 docker-install 倉(cāng)庫(kù) 中。

Docker 官方不推薦在生產(chǎn)環(huán)境中使用安裝腳本

下面的示例將使用 get.docker.com 提供的腳本安裝 Docker CE 的最新發(fā)布版本。如果要安裝最新測(cè)試版本,只需將腳本替換為 test.docker.com,并將下面示例命令中的get替換為test

> curl -fsSL https://get.docker.com -o get-docker.sh
> sudo sh get-docker.sh

<output truncated>

如果需要讓非root用戶使用 Docker,則使用以下命令將用戶添加至docker用戶組

> sudo usermod -aG docker your-user

注銷(xiāo)并重新登錄,即可生效。之后啟動(dòng) Docker

> sudo systemctl start docker

運(yùn)行hello-world鏡像以驗(yàn)證 Docker 是否正確安裝

> sudo docker run hello-world

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/

卸載 Docker CE

1. 卸載 Docker 安裝包

> sudo yum remote docker-ce

2. 刪除相關(guān)文件

主機(jī)上的鏡像、容器、卷組以及自定義的配置文件需要手動(dòng)刪除

> sudo rm -rf /var/lib/docker

參考資料

  1. Get Docker CE for CentOS | Docker Docs
  2. Post-installation steps for Linux | Docker Docs
  3. Docker Docs
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容