史上最全Docker环境安装指南-让安装docker简单到爆

一、思考❓❔

1.什么是Docker?

  • 装应用的容器
  • 开发、测试、运维都偏爱的容器化技术
  • 轻量级
  • 扩展性
  • 一次构建、多次分享、随处运行

2.安装Docker难不难?

  • So easy!
  • 此文看过之后,读者一定会有一个docker环境


二、Windows上安装Docker🔨🔨

1.在Windows 10上安装Docker

  • 系统需求
操作系统 内存 其他
Windows 10 64 bit (家庭版不行) 4GB以上 开启Hyper-V(大多数电脑默认会开启)

  • 下载安装包

    • https://download.docker.com/win/stable/Docker%20for%20Windows%20Installer.exe
  • 按照安装向导next即可

  • 运行

    • 不会自动启动

    • 在开始菜单搜索Docker, 打开即可

    • 细节配置, 可参考这个链接(https://docs.docker.com/docker-for-windows/#docker-settings-dialog


  • 测试是否安装成功

    • 在CMD中运行如下命令, 有显示版本号或者显示"Hello from Docker!", 则代表安装成功
    > docker --version
    
    Docker version 19.03.1
    
    > docker run hello-world
    docker : Unable to find image 'hello-world:latest' locally
    ...
    
    latest:
    Pulling from library/hello-world
    ca4f61b1923c:
    Pulling fs layer
    ca4f61b1923c:
    Download complete
    ca4f61b1923c:
    Pull complete
    Digest: sha256:97ce6fa4b6cdc0790cda65fe7290b74cfebd9fa0c9b8c38e979330d547d22ce1
    Status: Downloaded newer image for hello-world:latest
    
    Hello from Docker!
    This message shows that your installation appears to be working correctly.
    ...
    


2.在Windows 老版本安装Docker

  • 在老版本Windows上安装Docker需要满足如下条件
操作系统 内存 其他
Windows 7 , 8, 8.1 4GB以上 开启Hyper-V(大多数电脑默认会开启)

  • 下载Docker ToolBox安装包
    • https://github.com/docker/toolbox/releases/download/v19.03.1/DockerToolbox-19.03.1.exe

  • 安装

    • 检测版本

      • 对于Windows 8 或者 8.1, 打开任务管理器 -> 性能 -> 虚拟化已启用

      • 对于Windows 7可以使用这个工具去检测是否开启虚拟化https://www.microsoft.com/en-us/download/details.aspx?id=592
    • 双击exe安装包, 按照安装向导next即可


  • 测试是否安装成功

    • 在桌面查找Docker QuickStart图标双击开启Docker

    • 如果出现User Account Control 提示, 点击是

    • 会出现一个bash终端

    • 运行docker run hello-world, 如果出现"Hello from Docker.", 则代表运行成功

     $ docker run hello-world
     Unable to find image 'hello-world:latest' locally
     Pulling repository hello-world
     91c95931e552: Download complete
     a8219747be10: Download complete
     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 Engine CLI client contacted the Docker Engine daemon.
      2. The Docker Engine daemon pulled the "hello-world" image from the Docker Hub.
         (Assuming it was not already locally available.)
      3. The Docker Engine daemon created a new container from that image which runs the
         executable that produces the output you are currently reading.
      4. The Docker Engine daemon streamed that output to the Docker Engine CLI client, which sent it
         to your terminal.
    
     To try something more ambitious, you can run an Ubuntu container with:
      $ docker run -it ubuntu bash
    
     For more examples and ideas, visit:
      https://docs.docker.com/userguide/
    


三、在Linux上安装Docker💪🏽💪🏽

1.在Centos上安装

  • linux内核版本依赖
    • kernel version >= 3.8
    • 可以使用如下命令查看
      • uname -a | awk '{split($3, arr, "-"); print arr[1]}'

  • 如果已安装过Docker, 需要移除老版本的Docker

    sudo yum remove docker \
                      docker-client \
                      docker-client-latest \
                      docker-common \
                      docker-latest \
                      docker-latest-logrotate \
                      docker-logrotate \
                      docker-engine
    

  • 添加Docker repository yum源

    # 国内源, 速度更快, 推荐
    sudo yum-config-manager \
        --add-repo \
        https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
    
    
    # 官方源, 服务器在国外, 安装速度慢
    # $ sudo yum-config-manager \
    #     --add-repo \
    #     https://download.docker.com/linux/centos/docker-ce.repo
    

  • 开始安装Docker Engine

    sudo yum makecache fast
    sudo yum install docker-ce docker-ce-cli containerd.io
    

  • 开启Docker

    sudo systemctl enable docker
    sudo systemctl start docker
    

  • 验证是否安装成功

     sudo docker run hello-world
    
    • 如果出现"Hello from Docker.", 则代表运行成功

  • 如果在每次运行docker命令是, 在前面不添加sudo, 可以执行如下命令:

    sudo usermod -aG docker $USER
    

  • 如果嫌上面安装步骤麻烦, 可以运行如下脚本来安装

    • 不能在生产系统中使用
    curl -fsSL https://get.docker.com -o get-docker.sh
    sudo sh get-docker.sh --mirror Aliyun
    
    sudo systemctl enable docker
    sudo systemctl start docker
    
    sudo groupadd docker
    sudo usermod -aG docker $USER
    

2.在Ubuntu上安装

  • linux内核版本依赖

    • kernel version >= 3.8
    • 可以使用如下命令查看
      • uname -a | awk '{split($3, arr, "-"); print arr[1]}'
  • 操作系统依赖, 如下版本都可以

    Disco 19.04
    Cosmic 18.10
    Bionic 18.04 (LTS)
    Xenial 16.04 (LTS)
    

  • 如果已安装过Docker, 需要移除老版本的Docker

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

  • 使用Docker repository 来安装

    # 更新apt包索引
    sudo apt-get update
    
    # 为支持https
    sudo apt-get install \
        apt-transport-https \
        ca-certificates \
        curl \
        gnupg-agent \
        software-properties-common
        
    # 添加Docker GPG秘钥
    # 国内源
    curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
    # 或者国外源
    # curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    
    # 添加安装源
    # 推荐国内源
    sudo add-apt-repository \
        "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
        $(lsb_release -cs) \
        stable"
    # 或者国外源
    # sudo add-apt-repository \
    #   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
    #   $(lsb_release -cs) \
    #   stable"
    

  • 安装Docker

    # 更新apt包索引
    sudo apt-get update
    
    # 安装docker
    sudo apt-get install docker-ce docker-ce-cli containerd.io
    

  • 开启Docker

    sudo systemctl enable docker
    sudo systemctl start docker
    

  • 验证是否安装成功

     sudo docker run hello-world
    
    • 如果出现"Hello from Docker.", 则代表运行成功

  • 如果在每次运行docker命令是, 在前面不添加sudo, 可以执行如下命令:

    sudo usermod -aG docker $USER
    


四、使用shell脚本安装Docker🐙🐙

1.在Ubuntu和Centos上安装

install_docker_.sh

#!/bin/bash
#Author: 柠檬班可优
#Date: 2019-06-06
#install docker in ubuntu and centos


function install_docker_in_ubuntu
{
    sudo  apt-get update -y
    # install some tools
    sudo apt-get install \
        apt-transport-https \
        ca-certificates \
        curl \
        gnupg-agent \
        software-properties-common \
        net-tools \
        wget -y

    # install docker
    curl -fsSL get.docker.com -o get-docker.sh
    sh get-docker.sh

    # start docker service
    sudo groupadd docker &> /dev/null
    sudo gpasswd -a "${USER}" docker
    sudo systemctl start docker

    rm -rf get-docker.sh
}


function install_docker_in_centos
{
    # install some tools
    sudo yum install -y git vim gcc glibc-static telnet bridge-utils

    # install docker
    curl -fsSL get.docker.com -o get-docker.sh
    sh get-docker.sh

    # start docker service
    sudo groupadd docker &> /dev/null
    sudo gpasswd -a "${USER}" docker
    sudo systemctl start docker

    rm -rf get-docker.sh

}


SYSTEM_NAME="$(awk -F= '/^NAME/{print $2}' /etc/os-release)"
if [[ "${SYSTEM_NAME,,}" =~ "ubuntu"  ]] ; then
    echo "Your system is ubuntu."
    echo "Installing Docker in ubuntu..."
    install_docker_in_ubuntu
elif [[ "${SYSTEM_NAME,,}" =~ "centos" ]] ; then
    echo "Your system is centos."
    echo "Installing Docker in centos..."
    install_docker_in_centos
else
    echo "This script can only run in ubuntu and centos system."
    exit 1
fi

2.运行脚本

  • bash install_docker_.sh


五、在线练习docker🙊🙊

  • katacoda网站:https://www.katacoda.com/
    • 每10分钟一个小练习
  • Play with Docker网站:https://labs.play-with-docker.com/
    • 注册docker账号并登陆
    • 每次能使用4个小时,之后会被清空


六、总结💡💡

  • 本文介绍了安装docker的多种方式,相信总有一种适合你的
  • 学习热门的Docker容器化技术,安装docker是第一步

posted @ 2019-09-17 09:35  可优  阅读(6867)  评论(0编辑  收藏  举报