在 Linux 上部署 Docker

參考

Docker 官網用戶指南:https://docs.docker.com/engine/installation/linux/linux

環境依賴

  • Linux:64 位 Kernel 3.10 及以上(RHEL7 以上版本)docker

  • Windows:64 位ubuntu

在 Ubuntu 上安裝 Docker

Docker 支持如下的 Ubuntu 版本windows

  • Ubuntu Xenial 16.04 (LTS)centos

  • Ubuntu Wily 15.10緩存

  • Ubuntu Trusty 14.04 (LTS)bash

  • Ubuntu Precise 12.04 (LTS)服務器

先決條件

Docker 須要在 64 位版本的 Ubuntu 上安裝。此外,你還須要保證你的 Ubuntu 內核的最小版本不低於 3.10,其中 3.10 小版本和更新維護版也是可使用的。app

在低於 3.10 版本的內核上運行 Docker 會丟失一部分功能。在這些舊的版本上運行 Docker 會出現一些 BUG,這些 BUG 在必定的條件裏會致使數據的丟失,或者報一些嚴重的錯誤。curl

打開控制檯使用 uname -r 命令來查看你當前的內核版本。

$ uname -r
3.11.0-15-generic

Docker 要求 Ubuntu 系統的內核版本高於 3.10,查看本頁面的前提條件來驗證你的 Ubuntu 版本是否支持 Docker 。

更新 APT 源

經過下邊的操做來升級你的內核和安裝額外的包

1. 登陸到主機,切換到 root 用戶,取得權限。

2. 打開 Ubuntu 命令行控制檯。

3. 升級你的包管理器,確保 apt 可以使用 htpps,安裝 CA 證書

$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates

4. 添加 Docker 公鑰。

配置 Apt 來使用新倉庫的第一步是想 Apt 緩存中添加該庫的公鑰。使用 apt-key 命令:

$ sudo apt-key adv \
               --keyserver hkp://ha.pool.sks-keyservers.net:80 \
               --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

以上的 apt-key 命令向密鑰服務器 hkp://ha.pool.sks-keyservers.net 請求一個特定的密鑰( 58118E89F3A912897C070ADBF76221572C52609D )。公鑰將會被用來驗證重新倉庫下載的全部包。

5. 依據你的 Ubuntu 發行版本,在下表中找到對應條目。這個將決定 APT 將搜索的 Docker 包。若是可能,請運行一個長期支持 (LTS) 版的 Ubuntu。

Ubuntu version Repository
Precise 12.04 (LTS) deb https://apt.dockerproject.org... ubuntu-precise main
Trusty 14.04 (LTS) deb https://apt.dockerproject.org... ubuntu-trusty main
Wily 15.10 deb https://apt.dockerproject.org... ubuntu-wily main
Xenial 16.04 (LTS) deb https://apt.dockerproject.org... ubuntu-xenial main

6. 指定 Docker 倉庫的位置,根據第 5 步肯定的發行版本,替換下面命令行中的 <REPO> 參數便可。

$ echo "<REPO>" | sudo tee /etc/apt/sources.list.d/docker.list

## 例如:
$ echo "deb https://apt.dockerproject.org/repo ubuntu-xenial" | sudo tee /etc/apt/sources.list.d/docker.list

引入 Docker 的公鑰,咱們能夠配置 Apt 使用 Docker 的倉庫服務器。新建文件:/etc/apt/sources.list.d/docker.list,在裏面添加一個條目:deb https://apt.dockerproject.org/repo ubuntu-xenial main

7. 更新 APT 軟件包索引

$ sudo apt-get update

8. 驗證 apt 拉取正確的 repo

$ apt-cache policy docker-engine

  docker-engine:
    Installed: 1.12.2-0~trusty
    Candidate: 1.12.2-0~trusty
    Version table:
   *** 1.12.2-0~trusty 0
          500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
          100 /var/lib/dpkg/status
       1.12.1-0~trusty 0
          500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages
       1.12.0-0~trusty 0
          500 https://apt.dockerproject.org/repo/ ubuntu-trusty/main amd64 Packages

Ubuntu Xenial 16.04 (LTS), Wily 15.10, Trusty 14.04 (LTS) 先決條件

對於支持 aufs 存儲驅動的 Ubuntu Trusty, Wily, and Xenial 安裝 linux-image-extra-* kernel

1. 打開 Ubuntu 命令行控制檯

2. 更新 apt 包緩存

$ sudo apt-get update

這會觸發 apt 從新讀取配置文件,刷新倉庫列表,包含進咱們添加的那個倉庫。該命令也會查詢這些倉庫來緩存可用的包列表。

3. 安裝 linux-image-extra-* 包

在安裝 Docker Engine 以前,咱們須要安裝一個先決軟件包( prerequisite package )。linux-image-extra 包是一個內核相關的包,Ubuntu 系統須要它來支持 aufs 存儲設備驅動。Docker 會使用該驅動來加載卷。

$ sudo apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual

在 apt-get 命令中,$(uname -r) 將返回正在運行的內核的版本。任何對於該系統的內核更新應當包括安裝 linux-image-extra,它的版本須要與新內核版本相一致。若是該包沒有正確更新的話,Docker 加載卷的功能可能受到影響。

4. 安裝 Docker

轉到下一節 安裝 Docker Engine

Ubuntu Precise 12.04 (LTS) 先決條件

對於 Ubuntu Precise 發行版本, Docker 要求 Kernel 版本至少 3.13。若是你的 Kernel 版本老於 3.13,那你必須先升級內核。

1. 打開 Ubuntu 命令行控制檯

2. 更新 apt 包緩存

$ sudo apt-get update

3. 安裝包

$ sudo apt-get install linux-image-generic-lts-trusty

4. 重啓宿主機

$ sudo reboot

4. 安裝 Docker

轉到下一節 安裝 Docker Engine

安裝最新版本的 Docker Engine

1. 以 sudo 的用戶權限登陸 Ubuntu。

2. 更新 APT 包索引

$ sudo apt-get update

3. 安裝 Docker

$ sudo apt-get install docker-engine

4. 啓動 Docker 守護進程

$ sudo service docker start

5. 驗證 Docker 是否安裝成功

$ sudo docker run hello-world

升級

$ sudo apt-get update
$ sudo apt-get upgrade docker-engine

卸載

1. 卸載 Docker 包

$ sudo apt-get purge docker-engine

2. 卸載 docker 依賴包

$ sudo apt-get autoremove --purge docker-engine

3. 刪除相關文件

$ rm -rf /var/lib/docker

在 RedHat 上安裝 Docker

先決條件

Docker 須要在 64 位且內核版本不低於 3.10 的 Linux 上運行。

打開控制檯使用 uname -r 命令來查看你當前的內核版本。

$ uname -r
3.10.0-229.el7.x86_64

安裝 Docker Engine

以下兩種安裝方式:

  1. Yum 安裝

  2. Script 安裝

Yum 安裝

1. 以 sudo 或 root 權限登陸宿主機

2. yum 更新。

$ sudo yum update

3. 添加 yum 源

$ sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF

4. 安裝。

$ sudo yum install docker-engine

5. 啓用服務。

$ sudo systemctl enable docker.service

6. 如今 Docker 已經安裝好了,我來啓動 Docker 守護進程。

$ sudo systemctl start docker
Redirecting to /bin/systemctl start  docker.service

ps: sudo systemctl start docker.service

7. 驗證 Docker 是否安裝成功

$ sudo docker run --rm hello-world

 Unable to find image 'hello-world:latest' locally
 latest: Pulling from library/hello-world
 c04b14da8d14: Pull complete
 Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
 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.
  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 Hub account:
  https://hub.docker.com

 For more examples and ideas, visit:
  https://docs.docker.com/engine/userguide/

腳本安裝

1. 以 sudo 或 root 權限登陸宿主機

2. yum 更新。

$ sudo yum update

3. 運行 Docker 安裝腳本。

$ curl -fsSL https://get.docker.com/ | sh

4. 而後啓用 Docker 服務

$ sudo systemctl enable docker.service

5. 運行 Docker 線程

$ sudo systemctl start docker

6. 驗證 Docker 是否安裝成功

$ sudo docker run hello-world

卸載

1. 列出已安裝的 Docker 包

$ yum list installed | grep docker

2. 刪除包

$ sudo yum -y remove docker-engine.x86_64

3. 刪除鏡像,容器,數據卷

$ rm -rf /var/lib/docker

4. 刪除用戶數據

Windows

Dokcer for Windows

  1. 64 位 windows10 pro。

  2. 開啓 Hyper-V(控制面板 程序 程序和功能 啓用或關閉 Windows 功能 Hyper-V)。

  3. 官網下載並安裝 Docker for Windows(https://docs.docker.com/docker-for-windows/)。

Docker Toolbox

Windows10 pro 以前的 windows 系統。

官網下載並安裝 Docker Toolbox(https://www.docker.com/products/docker-toolbox),包括了 docker 可視化軟件Docker Kitematic

相關文章
相關標籤/搜索