一、思考❓
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
-
測試是否安裝成功
> 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.
...
-
-
在CMD中運行如下命令, 有顯示版本號或者顯示"Hello from Docker!", 則代表安裝成功
-
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 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/
-
-
在桌面查找Docker QuickStart圖標雙擊開啓Docker
-
如果出現User Account Control 提示, 點擊是
-
會出現一個bash終端
-
運行docker run hello-world, 如果出現"Hello from Docker.", 則代表運行成功
-
三、在Linux上安裝Docker
1.在Centos上安裝
-
linux內核版本依賴
-
uname -a | awk '{split($3, arr, "-"); print arr[1]}'
-
kernel version >= 3.8
-
可以使用如下命令查看
-
如果已安裝過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上安裝
uname -a | awk '{split($3, arr, "-"); print arr[1]}'
kernel version >= 3.8
可以使用如下命令查看
操作系統依賴, 如下版本都可以
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
-
linux內核版本依賴
使用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是第一步