Ubuntu之Docker的安裝教程

一、你需要準(zhǔn)備

警告:切勿在沒(méi)有配置 Docker APT 源的情況下直接使用 apt 命令安裝 Docker

1.系統(tǒng)要求

Docker CE 支持以下版本的 Ubuntu 操作系統(tǒng):

  • Artful 17.10 (Docker CE 17.11 Edge +)
  • Xenial 16.04 (LTS)
  • Trusty 14.04 (LTS)

Docker CE 可以安裝在 64 位的 x86 平臺(tái)或 ARM 平臺(tái)上。Ubuntu 發(fā)行版中,LTS(Long-Term-Support)長(zhǎng)期支持版本,會(huì)獲得 5 年的升級(jí)維護(hù)支持,這樣的版本會(huì)更穩(wěn)定,因此在生產(chǎn)環(huán)境中推薦使用 LTS 版本,當(dāng)前最新的 LTS 版本為 Ubuntu 18.04。

2.卸載舊版本

舊版本的 Docker 稱為 docker 或者 docker-engine,使用以下命令卸載舊版本:

$ sudo apt-get remove docker \
               docker-engine \
               docker.io

二、你可以選擇以下其中任何一種安裝方式

1.使用 APT 安裝
- 安裝必要的一些系統(tǒng)工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
- 安裝 GPG 證書(shū)
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
- 寫(xiě)入軟件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
- 更新并安裝 Docker CE
sudo apt-get -y update
sudo apt-get -y install docker-ce

注:以上命令會(huì)添加穩(wěn)定版本的 Docker CE APT 鏡像源,如果需要最新或者測(cè)試版本的 Docker CE 請(qǐng)將 stable 改為 edge 或者 test。從 Docker 17.06 開(kāi)始,edge test 版本的 APT 鏡像源也會(huì)包含穩(wěn)定版本的 Docker。

2.使用腳本自動(dòng)安裝
- 在測(cè)試或開(kāi)發(fā)環(huán)境中 Docker 官方為了簡(jiǎn)化安裝流程,提供了一套便捷的安裝腳本,Ubuntu 系統(tǒng)上可以使用這套腳本安裝:
$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh --mirror Aliyun

三、你還需要做以下配置

  1. 啟動(dòng) Docker CE
$ sudo systemctl enable docker
$ sudo systemctl start docker
  1. 建立 docker 用戶組
$ sudo groupadd docker
- 將當(dāng)前用戶加入 docker 組:
$ sudo usermod -aG docker $USER

退出當(dāng)前終端并重新登錄,進(jìn)行如下測(cè)試。

  1. 測(cè)試 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/

四、你還可以設(shè)置鏡像加速

Ubuntu 16.04+、Debian 8+、CentOS 7
對(duì)于使用 systemd 的系統(tǒng),請(qǐng)?jiān)?/etc/docker/daemon.json 中寫(xiě)入如下內(nèi)容(如果文件不存在請(qǐng)新建該文件)
注意:一定要保證該文件符合 json 規(guī)范,否則 Docker 將不能啟動(dòng)。

{
  "registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn"
  ]
}
  1. 重新啟動(dòng)服務(wù)
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
  1. 檢查加速器是否生效

配置加速器之后,如果拉取鏡像仍然十分緩慢,請(qǐng)手動(dòng)檢查加速器配置是否生效,在命令行執(zhí)行 docker info,如果從結(jié)果中看到了如下內(nèi)容,說(shuō)明配置成功。

Registry Mirrors:
  https://registry.docker-cn.com/
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容