Docker - 在Ubuntu18.04中安装Docker CE

 


1 - 系统信息

anliven@anliven:~# sudo 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
anliven@anliven:~#
anliven@anliven:~# sudo 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
anliven@anliven:~#

2 - 安装Docker

用户权限设置

root@anliven:~# chmod u+w /etc/sudoers
root@anliven:~# vim /etc/sudoers
root@anliven:~# grep anliven /etc/sudoers
anliven   ALL=(ALL) NOPASSWD:ALL
root@anliven:~# chmod u-w /etc/sudoers

Ubuntu源

anliven@anliven:~# sudo 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
anliven@anliven:~#

安装与设置

# 卸载旧版本docker(全新安装时,无需执行该步骤)
sudo apt-get remove docker docker-engine docker.io containerd runc

# 更换国内ubuntu源(中国科技大学)
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list

# 更新 apt 包索引
sudo apt-get update

# 安装 apt 依赖包,用于通过HTTPS来获取仓库
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

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

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

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

# 安装 Docker-CE(默认安装最新版)
sudo apt install docker-ce

# 设置开机自启动并启动 Docker-CE
sudo systemctl enable docker
sudo systemctl start docker

# 添加当前用户到 docker 用户组,可以不用 sudo 运行 docker
sudo usermod -aG docker $USER && newgrp docker

查看版本

anliven@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
anliven@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 - 运行验证

anliven@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/

anliven@anliven:~#
anliven@anliven:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        4 months ago        13.3kB
anliven@anliven:~#
anliven@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
anliven@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地址

anliven@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
anliven@anliven:~#

方法3:在/etc/docker/daemon.json 文件中设置使用Docker国内镜像

7 - 参考信息

posted @   Anliven  阅读(747)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
历史上的今天:
2017-07-09 Python - 数据结构与算法(Data Structure and Algorithms)
点击右上角即可分享
微信分享提示