1. 程式人生 > 實用技巧 >Install Docker Engine on CentOS 在CentOS 7 上安裝Docker

Install Docker Engine on CentOS 在CentOS 7 上安裝Docker

Install Docker Engine on CentOS

OS Requirements 系統要求

To install Docker Engine,you need a maintained version of CentOS 7. Archives versions arent supported or testes.

要安裝Docker引擎,需要CentOS 7的維護版本。不支援或測試存檔版本。

The centos-extras repository must be enabled. This repository is enabled by default.but if you hava disabled it ,you need to re-enabled

it

必須啟用centos-extras儲存庫。預設情況下該儲存庫是啟用的,但是如果您已經禁用了它,則需要重新啟用它。

The overlay2 storage driver is recommended.

建議使用overlay2儲存驅動程式。

Uninstall old versions 解除安裝舊版本

Older versions of Docker were called docker or docker-engine. If these are installed, uninstall them, along with associated dependencies.

Docker的舊版本被稱為Docker

Docker -engine。如果安裝了這些,請解除安裝它們以及相關的依賴項。

$ sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

It’s OK if yum reports that none of these packages are installed.

如果yum報告沒有安裝這些包,就說明可以了

The contents of /var/lib/docker/, including images, containers, volumes, and networks, are preserved. The Docker Engine package is now called docker-ce.

儲存/var/lib/docker/的內容,包括影象、容器、卷和網路。Docker Engine包現在稱為Docker -ce。

Installation methods

You can install Docker Engine in different ways, depending on your needs:

你可以根據需要,使用不同的方式安裝

  • Most users set up Docker’s repositories and install from them, for ease of installation and upgrade tasks. This is the recommended approach.
  • 大多數使用者設定了Docker的儲存庫並從它們進行安裝,以方便安裝和升級任務。這是推薦的方法。線上安裝
  • Some users download the RPM package and install it manually and manage upgrades completely manually. This is useful in situations such as installing Docker on air-gapped systems with no access to the internet.
  • 一些使用者下載RPM包並手動安裝,並完全手動管理升級。這在一些情況下非常有用,比如在沒有訪問internet的被隔離的系統上安裝Docker。離線安裝
  • In testing and development environments, some users choose to use automated convenience scripts to install Docker.
  • 在測試和開發環境中,一些使用者選擇使用自動化的方便指令碼來安裝Docker。

Install using the repository 使用儲存庫安裝

Before you install Docker Engine for the first time on a new host machine, you need to set up the Docker repository. Afterward, you can install and update Docker from the repository.

首次安裝Dcoker Engine之前,需要設定Docker repository,然後從rspository安裝和更新Docker

SET UP THE REPOSITORY 設定儲存庫

Install the yum-utils package (which provides the yum-config-manager utility) and set up the stable repository.

安裝yum-utils包(它提供了yum-config-manager 程式)並設定穩定的儲存庫

$ sudo yum install -y yum-utils

$ sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
阿里源
$ sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

Optional: Enable the nightly or test repositories.

可選:開啟每日儲存庫或者測試庫

These repositories are included in the docker.repo file above but are disabled by default. You can enable them alongside the stable repository. The following command enables the nightly repository.

這些儲存庫包含在docker中的repo檔案,但預設情況下是禁用的。您可以在穩定儲存庫的同時啟用它們。下面的命令啟用了每日儲存庫。

$ sudo yum-config-manager --enable docker-ce-nightly

To enable the test channel, run the following command:

要啟用測試通道,請執行以下命令:

$ sudo yum-config-manager --enable docker-ce-test

You can disable the nightly or test repository by running the yum-config-manager command with the --disable flag. To re-enable it, use the --enable flag. The following command disables the nightly repository.

您可以通過執行帶有 -disable標誌的yum-config-manager命令來禁用每日儲存庫或測試儲存庫。要重新啟用它,使用·--enable標誌。下面的命令禁用夜間儲存庫。

$ sudo yum-config-manager --disable docker-ce-nightly

INSTALL DOCKER ENGINE

  1. Install the latest version of Docker Engine and containerd, or go to the next step to install a specific version:

​ 安裝Docker Engine和containerd的最新版本,或者進入下一步安裝特定版本:

$ sudo yum install docker-ce docker-ce-cli containerd.io

​ If prompted to accept the GPG key, verify that the fingerprint matches 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35, and if so, accept it.

​ 如果提示接受GPG金鑰,請驗證指紋是否與060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35匹配,如果匹配,請接受。

​ To install a specific version of Docker Engine, list the available versions in the repo, then select and install:

​ 要安裝特定版本的Docker引擎,請在repo中列出可用的版本,然後選擇並安裝:

a.List and sort the versions available in your repo. This example sorts results by version number, highest to lowest, and is truncated:

​ a.列出並排序可用的版本。這個示例按照版本號對結果進行排序。從最高到最低並截斷

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

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.1.ce-3.el7                    docker-ce-stable
docker-ce.x86_64  18.06.0.ce-3.el7                    docker-ce-stable

​ The list returned depends on which repositories are enabled, and is specific to your version of CentOS (indicated by the .el7 suffix in this example).

​ 返回的列表取決於啟用了哪些儲存庫,並且特定於您的CentOs版本

b. Install a specific version by its fully qualified package name, which is the package name (docker-ce) plus the version string (2nd column) starting at the first colon (:), up to the first hyphen, separated by a hyphen (-). For example,

​ b.通過完全限定的包名安裝一個特定版本,包名是包名(docker-ce)加上版本字串(第二列),從第一個冒號(:)開始,直到第一個連字元,用連字元(-)分隔。例如,docker-ce-18.09.1

$ sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
eg: yum install docker-ce-18.09.9 docker-ce-cli-18.09.9 containerd.io

​ Docker is installed but not started. The docker group is created, but no users are added to the group.

​ Docker已安裝,但尚未啟動。docker組被建立,但是沒有使用者被新增到組中

  1. Start Docker.

    $ sudo systemctl start docker
    
  2. 新增國內的映象源,否則後續無法驗證

    sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json <<-'EOF'
    {
      "registry-mirrors": ["https://vfdfgdg.mirror.aliyuncs.com"]
    }
    EOF
    sudo systemctl daemon-reload
    sudo systemctl restart docker
    
  3. Verify that Docker Engine is installed correctly by running the hello-world image.

    $ sudo docker run hello-world
    

This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.

這個命令下載一個測試映像並在容器中執行它。當容器執行時,它列印一條訊息並退出。

Docker Engine is installed and running. You need to use sudo to run Docker commands. Continue to Linux postinstall to allow non-privileged users to run Docker commands and for other optional configuration steps.