環境准備
操作系統需求
為兼容企業級應用,學習選用Centos7做為部署安裝Docker的系統平台
# 通過以下命令可查看系統版本和內核版本等信息 cat /etc/redhat-release #----------------------------------- CentOS Linux release 7.4.1708 (Core) #----------------------------------- uname -a #--------------------------------------------------------------------------------------------------------- Linux docker01 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux #--------------------------------------------------------------------------------------------------------- cat /etc/os-release #------------------------------------------ NAME="CentOS Linux" VERSION="7 (Core)" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="7" PRETTY_NAME="CentOS Linux 7 (Core)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:centos:centos:7" HOME_URL="https://www.centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-7" CENTOS_MANTISBT_PROJECT_VERSION="7" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="7" #------------------------------------------
更換默認的yum源
Centos默認的yun源在國外,速度很慢有時間也無法訪問
yum repolist #------------------------------------------------------------------------------------------------------------------------------ Loaded plugins: fastestmirror Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error" Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=extras&infra=stock error was 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error" Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=updates&infra=stock error was 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error" repo id repo name status base/7/x86_64 CentOS-7 - Base 0 extras/7/x86_64 CentOS-7 - Extras 0 updates/7/x86_64 CentOS-7 - Updates 0 repolist: 0 #------------------------------------------------------------------------------------------------------------------------------ # 公司內服務器域名解析總有問題,時好時不好,很煩,這里直接用hosts做解析 vi /etc/hosts #------------------------------------- 221.206.129.236 mirrors.aliyun.com #------------------------------------- # 更換成aliyun yum源 cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo # 編譯CentOS-Base.repo,把帶mirrors.aliyuncs.com的行都刪除 vi /etc/yum.repos.d/CentOS-Base.repo # 運行以下命令生成緩存 yum clean all yum makecache # 查看已啟用的repo,確保centos-extras repository是啟用了,安裝docker時需要 yum repolist #-------------------------------------------------------------------------- Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com repo id repo name status base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 9,591 extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 284 updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 1,540 repolist: 11,415 #--------------------------------------------------------------------------
更新系統(可選)
yum update
刪除docker舊版本
# 有舊版本的docker話,可以用下面命令刪除 yum remove docker docker-common docker-selinux docker-engine
安裝 Docker CE
yum install -y yum-utils device-mapper-persistent-data lvm2 # yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum install -y docker-ce #-------------------------------------------------------------------------- ....... Installed: docker-ce.x86_64 0:17.09.1.ce-1.el7.centos Dependency Installed: audit-libs-python.x86_64 0:2.7.6-3.el7 checkpolicy.x86_64 0:2.5-4.el7 container-selinux.noarch 2:2.28-1.git85ce147.el7 libcgroup.x86_64 0:0.41-13.el7 libseccomp.x86_64 0:2.3.1-3.el7 libsemanage-python.x86_64 0:2.5-8.el7 policycoreutils-python.x86_64 0:2.5-17.1.el7 python-IPy.noarch 0:0.75-6.el7 setools-libs.x86_64 0:3.3.8-1.1.el7 ...... #--------------------------------------------------------------------------
若需要安裝指定的版本時,可參照以下命令
# 根據需要選擇是否開啟edge和test repositories yum-config-manager --enable docker-ce-edge yum-config-manager --enable docker-ce-test ## 禁用命令 yum-config-manager --disable docker-ce-edge ## 安裝指定的版本 yum list docker-ce --showduplicates | sort -r #-------------------------------------------------------------------------- Loading mirror speeds from cached hostfile Loaded plugins: fastestmirror Installed Packages docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.1.ce-1.el7.centos @docker-ce-stable docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable Available Packages #-------------------------------------------------------------------------- yum install docker-ce-17.06.1.ce
啟動docker
systemctl start docker # 查看docker的版本信息 docker version #-------------------------------------------------------------------------- Client: Version: 17.09.1-ce # 客戶端版本 API version: 1.32 Go version: go1.8.3 Git commit: 19e2cf6 Built: Thu Dec 7 22:23:40 2017 OS/Arch: linux/amd64 Server: Version: 17.09.1-ce # 服務端版本 API version: 1.32 (minimum version 1.12) Go version: go1.8.3 Git commit: 19e2cf6 Built: Thu Dec 7 22:25:03 2017 OS/Arch: linux/amd64 Experimental: false #-------------------------------------------------------------------------- # 查看網絡信息 ip addr #-------------------------------------------------------------------------- 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 link/ether 00:50:56:ab:4c:50 brd ff:ff:ff:ff:ff:ff inet 10.240.4.185/24 brd 10.240.4.255 scope global ens160 valid_lft forever preferred_lft forever inet6 fe80::250:56ff:feab:4c50/64 scope link valid_lft forever preferred_lft forever 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN # docker0 虛擬網橋 link/ether 02:42:72:ac:05:bf brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:72ff:feac:5bf/64 scope link valid_lft forever preferred_lft forever #-------------------------------------------------------------------------- systemctl list-unit-files | grep docker #-------------------------------------------------------------------------- docker.service disabled #-------------------------------------------------------------------------- # 設置成自啟服務 systemctl enable docker.service # 查看狀態 systemctl status docker #-------------------------------------------------------------------------- ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2017-12-12 17:24:31 CST; 13min ago Docs: https://docs.docker.com Main PID: 23479 (dockerd) CGroup: /system.slice/docker.service ├─23479 /usr/bin/dockerd └─23490 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libco... Dec 12 17:24:29 docker01 dockerd[23479]: time="2017-12-12T17:24:29.594209004+08:00" level=info msg="libcontainerd: new containerd process, pid: 23490" Dec 12 17:24:30 docker01 dockerd[23479]: time="2017-12-12T17:24:30.596093094+08:00" level=warning msg="failed to rename /var/lib/docker/tmp for background deletio...chronously" Dec 12 17:24:30 docker01 dockerd[23479]: time="2017-12-12T17:24:30.654014669+08:00" level=info msg="Graph migration to content-addressability took 0.00 seconds" Dec 12 17:24:30 docker01 dockerd[23479]: time="2017-12-12T17:24:30.654714697+08:00" level=info msg="Loading containers: start." Dec 12 17:24:30 docker01 dockerd[23479]: time="2017-12-12T17:24:30.852920366+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17...IP address" Dec 12 17:24:30 docker01 dockerd[23479]: time="2017-12-12T17:24:30.996504508+08:00" level=info msg="Loading containers: done." Dec 12 17:24:31 docker01 dockerd[23479]: time="2017-12-12T17:24:31.004149257+08:00" level=info msg="Docker daemon" commit=19e2cf6 graphdriver(s)=overlay version=17.09.1-ce Dec 12 17:24:31 docker01 dockerd[23479]: time="2017-12-12T17:24:31.004282017+08:00" level=info msg="Daemon has completed initialization" Dec 12 17:24:31 docker01 dockerd[23479]: time="2017-12-12T17:24:31.015479108+08:00" level=info msg="API listen on /var/run/docker.sock" Dec 12 17:24:31 docker01 systemd[1]: Started Docker Application Container Engine. Hint: Some lines were ellipsized, use -l to show in full. #--------------------------------------------------------------------------
運行hello-world image驗證docker安裝是否成功
docker run hello-world #-------------------------------------------------------------------------- Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world ca4f61b1923c: Pull complete Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c 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://cloud.docker.com/ For more examples and ideas, visit: https://docs.docker.com/engine/userguide/ #--------------------------------------------------------------------------
升級和卸載docker
# 升級 yum -y upgrade docker-ce # 卸載 yum remove docker-ce # 刪除Images, containers, volumes, or customized configuration files rm -rf /var/lib/docker
使用阿里鏡像加速器
使用阿里雲專屬加速器加快獲取Docker官方鏡像,否則在國內速度會慢到你無法忍受噠。步驟如下:
- 免費注冊一個阿里雲賬號 www.aliyun.com
- 進入加速器頁面 https://cr.console.aliyun.com/#/accelerator
- 選擇
鏡像加速器
按圖中進行相關配置
# 下面的xxxxx要替換成你的專屬加速器的地址哦 tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://xxxxxxx.mirror.aliyuncs.com"] } EOF systemctl daemon-reload systemctl restart docker