Docker学习笔记_03 Docker的部署安装(Ubuntu)

为什么生产环境中Docker要部署在Ubuntu上

原来为了兼容企业级应用,之前学习Docker选用了Centos7做为部署安装Docker的系统平台,但在阅读官方文档后发现,Centos7下默认的Docker存储驱动在生产环境中部署是会有一些问题。

详细可参考这篇文章:http://blog.csdn.net/csdn_duomaomao/article/details/78499639

在Ubuntu上安装Docker

查看系统版本

uname -a
# 18.04
Linux ubuntu 4.15.0-34-generic #37-Ubuntu SMP Mon Aug 27 15:21:48 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

cat /proc/version
# 18.04
Linux version 4.15.0-34-generic (buildd@lgw01-amd64-047) (gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)) #37-Ubuntu SMP Mon Aug 27 15:21:48 UTC 2018

lsb_release -a
#--------------------------------------
# 18.04
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:        18.04
Codename:       bionic
#--------------------------------------

移除旧的Docker版本

sudo apt-get remove docker docker-engine docker.io

安装必要的一些系统工具

# Update the apt package index
sudo apt-get update
sudo apt-get -y upgrade
sudo apt autoremove

# Install packages to allow apt to use a repository over HTTPS
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common

安装GPG证书并使用阿里源

# Add Docker’s official GPG key
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

sudo apt-key fingerprint 0EBFCD88
#--------------------------------------------------------------
pub   4096R/0EBFCD88 2017-02-22
      Key fingerprint = 9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid                  Docker Release (CE deb) <docker@docker.com>
sub   4096R/F273FCD8 2017-02-22
#--------------------------------------------------------------

sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update

安装DOCKER CE

为了之后配置使用rancher和kubernetes,这里安装指定版本的docker(17.03.2-ce)
版本的选择参考:https://rancher.com/docs/rancher/v1.6/en/hosts/#supported-docker-versions

# 查找可用Docker-CE的版本
sudo apt-cache madison docker-ce
#---------------------------------------------------------------------------------------------------------
 docker-ce | 18.06.1~ce~3-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 18.06.0~ce~3-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 18.03.1~ce~3-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic/stable amd64 Packages
#---------------------------------------------------------------------------------------------------------

sudo apt install docker-ce

# 安装指定版本的Docker-CE: (VERSION 例如上面的 18.06.1~ce~3-0~ubuntu)
sudo apt -y install docker-ce=18.06.1~ce~3-0~ubuntu

# The Docker daemon starts automatically.

查看docker版本及运行信息

sudo docker version
#-----------------------------------
Client:
 Version:           18.06.1-ce
 API version:       1.38
 Go version:        go1.10.3
 Git commit:        e68fc7a
 Built:             Tue Aug 21 17:24:51 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.06.1-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.3
  Git commit:       e68fc7a
  Built:            Tue Aug 21 17:23:15 2018
  OS/Arch:          linux/amd64
  Experimental:     false
#-----------------------------------

sudo docker info
#-----------------------------------
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 18.06.1-ce
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 468a545b9edcd5932818eb9de8e72413e616e86e
runc version: 69663f0bd4b60df09991c08812a60108003fa340
init version: fec3683
Security Options:
 apparmor
 seccomp
  Profile: default
Kernel Version: 4.15.0-36-generic
Operating System: Ubuntu 18.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 16
Total Memory: 31.41GiB
Name: ubuntu
ID: AQXX:XUK3:VP7F:RXXQ:4DSS:PYYC:QPK5:OOZH:I75I:CZ2H:IFSI:GFLM
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

WARNING: No swap limit support
#-----------------------------------
# WARNING: No swap limit support 这个警告不影响正常使用

设置不更新指定版本的docker-ce

sudo echo "docker-ce hold" | sudo dpkg --set-selections

# 查询当前系统内所有软件包的状态,命令为:
sudo dpkg --get-selections | more

# 查询当前系统被锁定不更新的软件包状态(hold),命令为
sudo dpkg --get-selections | grep hold

# 取消不更新
sudo echo "docker-ce install" | sudo dpkg --set-selections

# 查看
sudo dpkg --get-selections | grep docker
#----------------------------------------------------------------------
docker-ce                                       install
#----------------------------------------------------------------------

卸载Docker CE

sudo apt-get purge docker-ce
sudo rm -rf /var/lib/docker

使用阿里镜像加速器

使用阿里云专属加速器加快获取Docker官方镜像,否则在国内速度会慢到你无法忍受哒。步骤如下:

  1. 免费注册一个阿里云账号 www.aliyun.com
  2. 进入加速器页面 https://cr.console.aliyun.com/#/accelerator
  3. 选择镜像加速器

按图中进行相关配置

# 下面的xxxxx要替换成你的专属加速器的地址哦
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://xxxxxxx.mirror.aliyuncs.com"]
}
EOF

# 重启docker
sudo /etc/init.d/docker restart

sudo systemctl restart docker

更改ubuntu源为阿里源

sudo vi /etc/apt/sources.list

# 输入ggdG删除所有内容,添加以下内容 ubuntu 18.04(bionic) 配置如下
#----------------------------------------------------------------------
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
#----------------------------------------------------------------------

# 更新获取阿里云软件源 提供的软件列表
sudo apt-get update

# 更新软件
sudo apt-get -y upgrade

# 公司局域网域名解析有问题,手动配置hosts
sudo vi /etc/hosts
#----------------------------------------------------------------------
221.206.129.236  mirrors.aliyun.com
91.189.88.162  archive.ubuntu.com
91.189.92.150  archive.canonical.com
#----------------------------------------------------------------------

#变更DNS 16.04
vi /etc/network/interfaces
修改
dns-nameservers 10.240.4.159

# 18.04配置/etc/network/interfaces已无用
vi /etc/netplan/50-cloud-init.yaml
#----------------------------------------------------------------------
network:
    ethernets:
        ens160:
            addresses:
            - 10.240.4.185/24
            dhcp4: false
            gateway4: 10.240.4.254
            nameservers:
                addresses: [10.240.4.159,10.240.4.160]
                search: []
    version: 2
#----------------------------------------------------------------------

netplan apply 

netstat -nltp | grep systemd-resolv
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      1017/systemd-resolv 

systemctl list-unit-files | grep resolv
dbus-org.freedesktop.resolve1.service  enabled        
systemd-resolved.service               enabled  

systemctl restart systemd-resolved.service
systemd-resolve --status

Ubunut 18.04 DNS问题

# Rancher的DNS会从宿主机/etc/resolv.conf复制, Ubunut 18.04中直接修改以下两个地方在安装rancher agent后会报错

vi /etc/systemd/resolved.conf
#----------------------------------------------------------------------
[Resolve]
DNS=10.240.4.159 10.240.4.160
LLMNR=no
#----------------------------------------------------------------------

# DNS=设置的是域名解析服务器的IP地址,这里分别设为10.240.4.159和10.240.4.160
# LLMNR=设置的是禁止运行LLMNR(Link-Local Multicast Name Resolution),否则systemd-resolve会监听5535端口。

systemctl stop systemd-resolved
systemctl disable systemd-resolved

systemctl list-unit-files | grep resolv
#----------------------------------------------------------------------
systemd-resolved.service               disabled  
#----------------------------------------------------------------------

rm /etc/resolv.conf
vi /etc/resolv.conf
#----------------------------------------------------------------------
nameserver 10.240.4.159
nameserver 10.240.4.160
#----------------------------------------------------------------------


hostnamectl
#----------------------------------------------------------------------
   Static hostname: ubuntu
         Icon name: computer-vm
           Chassis: vm
        Machine ID: ba3546d2eb174ef4b6d4d71ebb1fa616
           Boot ID: 99539141d36149ccb20bd164d184b211
    Virtualization: vmware
  Operating System: Ubuntu 18.04.1 LTS
            Kernel: Linux 4.15.0-36-generic
      Architecture: x86-64
#----------------------------------------------------------------------
#
vi /etc/cloud/cloud.cfg
 #找到preserve_hostname: false修改为preserve_hostname: ture12

hostnamectl set-hostname docker185
vi /etc/hosts
#----------------------------------------------------------------------
127.0.0.1       localhost.localdomain   localhost
10.240.4.185    docker185
#----------------------------------------------------------------------

# 或者
vi /etc/hostname
#----------------------------------------------------------------------
docker185
#----------------------------------------------------------------------

vi /etc/cloud/cloud.cfg
#----------------------------------------------------------------------
preserve_hostname: true
#----------------------------------------------------------------------

reboot
文章目录
  1. 1. 为什么生产环境中Docker要部署在Ubuntu上
  2. 2. 在Ubuntu上安装Docker
    1. 2.1. 查看系统版本
    2. 2.2. 移除旧的Docker版本
    3. 2.3. 安装必要的一些系统工具
    4. 2.4. 安装GPG证书并使用阿里源
    5. 2.5. 安装DOCKER CE
    6. 2.6. 查看docker版本及运行信息
    7. 2.7. 设置不更新指定版本的docker-ce
  3. 3. 卸载Docker CE
  4. 4. 使用阿里镜像加速器
  5. 5. 更改ubuntu源为阿里源
  6. 6. Ubunut 18.04 DNS问题