CentOS7安裝Docker遇到的問題筆記

朱季謙發表於2021-09-26

筆記/朱季謙

以下是筆者本人學習搭建docker過程當中記錄的一些實踐筆記,過程當中也遇到了一些坑,但都解決了,就此記錄,留作以後再次搭建時可以直接參考。

一、首先,先檢查CentOS版本,保證在CentOS7版本以上,系統核心在3.10以上——

[root@192 opt]# uname -r
3.10.0-693.el7.x86_64

二、解除安裝舊的docker版本

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

三、安裝需要的依賴包

sudo yum install -y yum-utils

四、設定國內映象的倉庫

sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

五、更新yum軟體包索引

yum makecache fast 

六、安裝docker相關 docker-ce 社群 docker ee企業版

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

七、啟動docker

systemctl start docker

這時,出現了一個很奇怪的異常:

Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.

不慌,我們先檢查一下daemon.json檔案格式:

vim /etc/docker/daemon.json

發現這個daemon.json檔案裡是這樣的,也不知道為啥直接更新下載後,它會缺少了部分字元......

image

需要修改成這樣,就可以了

{"registry-mirrors": ["http://9600955f.m.daocloud.io"],
 "insecure-registries": []
}

接下來,可以正常啟動docker了——

[root@192 opt]# systemctl daemon-reload
[root@192 opt]# systemctl start docker

如果不報錯,輸入systemctl status docker.service,若顯示以下資訊則證明啟動安裝並啟動成功:
image

安裝成功後,使用docker version,一般會出現以下資訊:

[root@192 opt]# 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:55:49 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:54:13 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-world時,發現出現一個超時問題:

[root@192 opt]# docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get "https://registry-1.docker.io/v2/library/hello-world/manifests/sha256:393b81f0ea5a98a7335d7ad44be96fe76ca8eb2eaa76950eb8c989ebf2b78ec0": net/http: TLS handshake timeout.
See 'docker run --help'.

這時,需要把daemon.json檔案裡的資訊改一下,改成國內阿里雲映象配置,可以提高拉取速度,避免超時問題,如下:

{"registry-mirrors": ["https://6kx4zyno.mirror.aliyuncs.com"],
 "insecure-registries": []
}

接著,重啟systemctl restart docker,即可。

[root@192 opt]# sudo systemctl restart docker

再試著執行docker run hello-world,這次就正常了:

[root@192 opt]# 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.

檢視一下是否已經成功下載hello-world映象,可以看到,hello-world映象已經被拉到了docker當中:

root@192 opt]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   40 hours ago    13.3kB
nginx         latest    f35646e83998   11 months ago   133MB
ubuntu        latest    549b9b86cb8d   21 months ago   64.2MB

相關文章