Docker - 在Ubuntu18.04中安裝Docker CE

1 - 系統信息

root@anliven:~# uname -a
Linux anliven 5.3.0-45-generic #37~18.04.1-Ubuntu SMP Fri Mar 27 15:58:10 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
root@anliven:~#
root@anliven:~# cat /proc/version
Linux version 5.3.0-45-generic (buildd@lcy01-amd64-027) (gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)) #37~18.04.1-Ubuntu SMP Fri Mar 27 15:58:10 UTC 2020
root@anliven:~#

2 - 安裝Docker

Ubuntu源

root@anliven:~# cat  /etc/apt/sources.list
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
root@anliven:~#

安裝與設置

# 卸載舊版本docker(全新安裝時,無需執行該步驟)
apt-get remove docker docker-engine docker.io

# 更換國內ubuntu源(中國科技大學)
cp /etc/apt/sources.list /etc/apt/sources.list.bak
sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list

# 更新 apt 包索引
apt-get update

# 安裝 apt 依賴包,用於通過HTTPS來獲取倉庫
apt-get install apt-transport-https ca-certificates curl software-properties-common

# 添加 Docker 的官方 GPG 密鑰
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# 設置Docker穩定版倉庫
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
add-apt-repository "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

# 再次更新 apt 包索引
apt-get update

# 安裝 Docker-CE(默認安裝最新版)
apt install docker-ce

# 設置開機自啓動並啓動 Docker-CE
systemctl enable docker
systemctl start docker

# 添加當前用戶到 docker 用戶組,可以不用 sudo 運行 docker(可選)
groupadd docker
usermod -aG docker $USER

查看版本

root@anliven:~# docker version
Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b7f0
 Built:             Wed Mar 11 01:25:46 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b7f0
  Built:            Wed Mar 11 01:24:19 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
root@anliven:~#

3 - 設置鏡像加速

參考信息: https://www.cnblogs.com/anliven/p/6218741.html

  1. 安裝/升級Docker客戶端
    推薦安裝1.10.0以上版本的Docker客戶端,參考文檔: https://yq.aliyun.com/articles/110806
  2. 配置鏡像加速器
    針對Docker客戶端版本大於 1.10.0 的用戶
    可以通過修改daemon配置文件/etc/docker/daemon.json來使用加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://t5t8q6wn.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

4 - 運行驗證

root@anliven:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:6a65f928fb91fcfbc963f7aa6d57c8eeb426ad9a20c7ee045538ef34847f44f1
Status: Downloaded newer image for hello-world:latest

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/

root@anliven:~#
root@anliven:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        4 months ago        13.3kB
root@anliven:~#
root@anliven:~# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
0f1ea64ef13e        hello-world         "/hello"            2 minutes ago       Exited (0) 2 minutes ago                       mystifying_darwin
root@anliven:~#

5 - docker-compose

https://docs.docker.com/compose/install/#install-compose

# 下載docker-compose
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# 授權
$ sudo chmod +x /usr/local/bin/docker-compose

# 版本信息顯示正常說明安裝成功
$ docker-compose --version

6 - 問題處理

1 - apt命令失敗

處理步驟

ps -A | grep apt
kill -9
rm -f /var/lib/dpkg/lock
rm -f /var/cache/apt/archives/lock
rm -f /var/lib/apt/lists/lock

2 - 執行Docker命令報錯

docker search 和 docker pull 命令時 分別提示如下錯誤

Error response from daemon: Get https://index.docker.io/v1/search?q=helloworld&n=25: dial tcp: lookup index.docker.io: no such host
Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

方法1:
通過dig @114.114.114.114 registry-1.docker.io命令找到可用IP
修改/etc/hosts強制docker.io相關的域名解析到可用IP

方法2:修改DNS Server地址

root@anliven:~# cat /etc/resolv.conf |grep nameserver
nameserver 114.114.114.114
nameserver 115.115.115.115
nameserver 223.5.5.5
nameserver 119.29.29.29
root@anliven:~#

方法3:在/etc/docker/daemon.json 文件中設置使用Docker國內鏡像

7 - 參考信息

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章