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 - 参考信息

  • https://www.cnblogs.com/anliven/p/6804501.html
  • https://www.runoob.com/docker/ubuntu-docker-install.html

你可能感兴趣的:(Docker - 在Ubuntu18.04中安装Docker CE)