一, 思考
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 for Windows Installer.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 是第一步
来源: https://www.cnblogs.com/keyou1/p/11511067.html