ubuntu下安裝docker方法大全
系統要求
Docker CE 支持以下版本的 Ubuntu 操作系統:
Artful 17.10 (Docker CE 17.11 Edge)
Zesty 17.04
Xenial 16.04 (LTS)
Trusty 14.04 (LTS)
Docker CE 可以安裝在 64 位的 x86 平臺或 ARM 平臺上。Ubuntu 發行版中,LTS(Long-Term-Support)長期支持版本,會獲得 5 年的升級維護支持,這樣的版本會更穩定,因此在生產環境中推薦使用 LTS 版本,當前最新的 LTS 版本為 Ubuntu 16.04。
卸載舊版本
舊版本的 Docker 稱為 docker 或者 docker-engine,使用以下命令卸載舊版本:
$ sudo apt-get remove docker \
docker-engine \
docker.io
Ubuntu 14.04 可選內核模塊
從 Ubuntu 14.04 開始,一部分內核模塊移到了可選內核模塊包 (linux-image-extra-*) ,以減少內核軟件包的體積。正常安裝的系統應該會包含可選內核模塊包,而一些裁剪後的系統可能會將其精簡掉。AUFS 內核驅動屬於可選內核模塊的一部分,作為推薦的 Docker 存儲層驅動,一般建議安裝可選內核模塊包以使用 AUFS。
如果系統沒有安裝可選內核模塊的話,可以執行下面的命令來安裝可選內核模塊包:
$ sudo apt-get update
$ sudo apt-get install \
linux-image-extra-$(uname -r) \
linux-image-extra-virtual
使用 APT 安裝
由於 apt 源使用 HTTPS 以確保軟件下載過程中不被篡改。因此,我們首先需要添加使用 HTTPS 傳輸的軟件包以及 CA 證書。
$ sudo apt-get update
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
鑒於國內網絡問題,強烈建議使用國內源,官方源請在註釋中查看。
為了確認所下載軟件包的合法性,需要添加軟件源的 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 -
然後,我們需要向 source.list 中添加 Docker 軟件源
$ 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 CE APT 鏡像源,如果需要最新或者測試版本的 Docker CE 請將 stable 改為 edge 或者 test。從 Docker 17.06 開始,edge test 版本的 APT 鏡像源也會包含穩定版本的 Docker。
安裝 Docker CE
更新 apt 軟件包緩存,並安裝 docker-ce:
$ sudo apt-get update
$ sudo apt-get install docker-ce
使用腳本自動安裝
在測試或開發環境中 Docker 官方為了簡化安裝流程,提供了一套便捷的安裝腳本,Ubuntu 系統上可以使用這套腳本安裝:
$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh --mirror Aliyun
執行這個命令後,腳本就會自動的將一切準備工作做好,並且把 Docker CE 的 Edge 版本安裝在系統中。
啟動 Docker CE
$ sudo systemctl enable docker
$ sudo systemctl start docker
Ubuntu 14.04 請使用以下命令啟動:
$ sudo service docker start
建立 docker 用戶組
默認情況下,docker 命令會使用 Unix socket 與 Docker 引擎通訊。而只有 root 用戶和 docker 組的用戶才可以訪問 Docker 引擎的 Unix socket。出於安全考慮,一般 Linux 系統上不會直接使用 root 用戶。因此,更好地做法是將需要使用 docker 的用戶加入 docker 用戶組。
建立 docker 組:
$ sudo groupadd docker
將當前用戶加入 docker 組:
$ sudo usermod -aG docker $USER
退出當前終端並重新登錄,進行如下測試。
測試 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:
- The Docker client contacted the Docker daemon.
- The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64) - The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading. - 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/
若能正常輸出以上信息,則說明安裝成功。
ubuntu下安裝docker方法大全