天天看點

最簡單的教程:在Ubuntu作業系統裡安裝Docker

Docker 是一個開源的應用容器引擎,讓開發者可以打包他們的應用以及依賴包到一個可移植的容器中,然後釋出到任何流行的 Linux 機器上,也可以實作虛拟化。容器是完全使用沙箱機制,互相之間不會有任何接口。

今天這篇文章咱們就來将Docker安裝到Ubuntu上。

1. 由于Ubuntu裡apt官方庫裡的docker版本可能比較低,是以先用下面的指令行解除安裝舊版本(如果有的話)

sudo apt-get remove docker docker-engine docker-ce docker.io

2. 更新apt包索引:

sudo apt-get update

3. 執行下列指令行,使apt可以通過HTTPS協定去使用存儲庫:

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

4. 添加Docker官方提供的GPG密鑰:

curl -fsSL

https://download.docker.com/linux/ubuntu/gpg

| sudo apt-key add -

什麼是GPG密鑰呢?大家可以從阮一峰老師的部落格:GPG入門教程裡獲得答案:

1991年,程式員Phil Zimmermann為了避開政府監視,開發了加密軟體PGP。這個軟體非常好用,迅速流傳開來,成了許多程式員的必備工具。但是,它是商業軟體,不能自由使用。是以,自由軟體基金會決定,開發一個PGP的替代品,取名為GnuPG。這就是GPG的由來。

上面的文字出自阮一峰老師的部落格連結:

http://www.ruanyifeng.com/blog/2013/07/gpg.html

5. 設定stable存儲庫:

sudo add-apt-repository “deb [arch=amd64]

https://download.docker.com/linux/ubuntu

$(lsb_release -cs) stable”

最後再次更新apt包索引:

6. 安裝最新版本的docker-ce:

sudo apt-get install -y docker-ce

安裝完畢後,可以看到Docker服務已經從/lib/systemd/system/docker.service啟動了,dockerd程序id為4921:

指令行ps -aux檢視程序id為4921的程序路徑:/usr/bin/dockerd -H fd://

使用指令sudo docker version可以檢視安裝docker的版本:

使用指令sudo docker run hello-world,能觀察到從遠端下載下傳這個測試用的容器:Pulling from library/hello-world:

然後看到列印消息:Hello from Docker! 說明Docker安裝成功。

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://hub.docker.com/

For more examples and ideas, visit:

https://docs.docker.com/engine/userguide/