1. 程式人生 > 其它 >Ubuntu 17.04 x64 安裝 Docker CE

Ubuntu 17.04 x64 安裝 Docker CE

Docker 是個劃時代的開源專案,它徹底釋放了計算虛擬化的威力,極大提高了應用的執行效率,降低了雲端計算資源供應的成本!使用 Docker,可以讓應用的部署、測試和分發都變得前所未有的高效和輕鬆!

無論是應用開發者、運維人員、還是其他資訊科技從業人員,都有必要認識和掌握 Docker,節約有限的時間。

系統要求

要安裝Docker CE,您需要這些Ubuntu版本的64位版本:

  • Artful 17.10(Docker CE 17.11 Edge及更高版本)
  • ZESTY 17.04
  • Xenial 16.04(LTS)
  • Trusty 14.04(LTS)

Ubuntu x86_64,Linux armhf,s390x(IBM Z)和ppc64le(IBM Power)架構上支援Docker CE

解除安裝舊版本

老版本的Docker被稱為dockerdocker-engine。如果安裝了這些,請將其解除安裝:

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

使用儲存庫進行安裝

首次在新的主機上安裝Docker CE之前,需要設定Docker儲存庫。之後,您可以從儲存庫安裝和更新Docker

  • 設定儲存庫

1.更新軟體包

1.更新apt軟體包索引:

$ apt-get update

2.設定儲存庫

2.安裝軟體包以允許apt通過HTTPS使用儲存庫:

$ apt-get install 
    apt-transport-https 
    ca-certificates 
    curl 
    software-properties-common

3.新增GPG金鑰

3.新增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 -

4.設定映象源

4.新增 Docker 軟體源

鑑於國內網路問題,強烈建議使用國內源,官方源請在註釋中檢視。

然後,我們需要向 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

1.更新軟體包

1.更新apt軟體包索引。

$ apt-get update

2.安裝Docker CE

2.安裝最新版本的Docker CE,或者轉到下一步安裝特定版本。任何現有的Docker安裝都將被替換。

$ apt-get install docker-ce

3.列出版本

3.在生產系統上,您應該安裝特定版本的Docker CE,而不是始終使用最新版本。此輸出被截斷。列出可用的版本。

$ apt-cache madison docker-ce

docker-ce | 17.12.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu zesty/stable amd64 Packages
docker-ce | 17.09.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu zesty/stable amd64 Packages
docker-ce | 17.09.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu zesty/stable amd64 Packages
docker-ce | 17.06.2~ce-0~ubuntu | https://download.docker.com/linux/ubuntu zesty/stable amd64 Packages
docker-ce | 17.06.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu zesty/stable amd64 Packages
docker-ce | 17.06.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu zesty/stable amd64 Packages

列表的內容取決於啟用了哪個儲存庫。選擇一個特定的版本進行安裝。第二列是版本字串。第三列是儲存庫名稱,它指出了軟體包來自哪個儲存庫,並通過擴充套件其穩定性級別。要安裝特定版本,請將版本字串附加到包名稱,並用等號(=)將它們分開:

$ sudo apt-get install docker-ce=<VERSION>

4.執行映象

4.通過執行hello-world 映像驗證是否正確安裝了Docker CE

$ docker run hello-world

這個命令下載一個測試影象並在容器中執行。容器執行時,會列印一條資訊訊息並退出。

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete 
Digest: sha256:445b2fe9afea8b4aa0b2f27fe49dd6ad130dfe7a8fd0832be5de99625dad47cd
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/

以非root使用者身份管理Docker

預設情況下,docker 命令會使用 Unix socketDocker 引擎通訊。而只有 root 使用者和 docker 組的使用者才可以訪問 Docker 引擎的 Unix socket。出於安全考慮,一般 Linux 系統上不會直接使用 root 使用者。因此,更好地做法是將需要使用 docker 的使用者加入 docker 使用者組。

要建立docker組並新增您的使用者:

1.建立使用者組

1.建立docker組。

$ sudo groupadd docker

2.新增使用者組

2.將您的使用者新增到docker組中。

$ sudo usermod -aG docker $USER

3.登出系統

3.登出並重新登入,如果在虛擬機器上進行測試,則可能需要重新啟動虛擬機器才能使更改生效。

4.執行映象

4.驗證您可以不執行docker命令sudo

$ docker run hello-world

解除安裝Docker CE

1.解除安裝

1.解除安裝Docker CE軟體包:

$ sudo apt-get purge docker-ce

2.刪除

2.主機上的影象,容器,卷或自定義配置檔案不會自動刪除。刪除所有影象,容器和卷:

$ sudo rm -rf /var/lib/docker

利用 commit 理解映象構成

這條命令會用nginx 映象啟動一個容器,命名為 myweb,並且映射了 80 埠,這樣我們可以用瀏覽器去訪問這個 nginx 伺服器。

$ docker run --name myweb -d -p 80:80 nginx

直接訪問:http://localhost;如果使用的是 Docker Toolbox,或者是在虛擬機器、雲伺服器上安裝的 Docker,則需要將 localhost 換為虛擬機器地址或者實際雲伺服器地址。

參考:Docker 官網 Get Docker CE for Ubuntu

https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/#install-using-the-convenience-script

Contact