共計 9031 個字符,預(yù)計需要花費 23 分鐘才能閱讀完成。
這篇“Ubuntu 下如何安裝與使用 Docker”文章的知識點大部分人都不太理解,所以丸趣 TV 小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Ubuntu 下如何安裝與使用 Docker”文章吧。
先決條件
要遵循本教程,您需要以下內(nèi)容:
64 位 ubuntu 16.04 droplet
使用 sudo 特權(quán)的非 root 用戶初始設(shè)置指南的 ubuntu 16.04 將介紹如何設(shè)置起來。)
注意:docker 需要的 ubuntu 64 位版本,以及一個內(nèi)核版本等于或大于 3.10 以上。
默認(rèn)的 64 位 ubuntu 16.04 droplet 滿足這些要求。
本教程中的所有命令都應(yīng)以非 root 用戶身份運行。如果需要該命令的 root 訪問權(quán)限,它會在前面加 sudo。初始設(shè)置指南的 ubuntu 16.04 解釋了如何添加用戶,并給他們 sudo 訪問。
第 1 步 – 安裝 docker
官方 ubuntu 16.04 存儲庫中提供的 docker 安裝包可能不是最新版本。要獲得最新和最好的版本,請從官方 docker 倉庫安裝 docker。本節(jié)向您展示如何做到這一點。
但首先,讓我們更新包數(shù)據(jù)庫:
sudo apt-get update
現(xiàn)在讓我們安裝 docker。將官方 docker 資源庫的 gpg 密鑰添加到系統(tǒng):
sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118e89f3a912897c070adbf76221572c52609d
將 docker 存儲庫添加到 apt 源:
echo deb https://apt.dockerproject.org/repo ubuntu-xenial main | sudo tee /etc/apt/sources.list.d/docker.list
使用新添加的軟件包中的 docker 軟件包更新軟件包數(shù)據(jù)庫:
sudo apt-get update
確保你將要從 docker repo 而不是默認(rèn)的 ubuntu 16.04 repo 安裝:
apt-cache policy docker-engine
您應(yīng)該看到類似以下的輸出:
apt-cache 策略的輸出 docker-engine
docker-engine:
installed: (none)
candidate: 1.11.1-0~xenial
version table:
1.11.1-0~xenial 500
500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 packages
1.11.0-0~xenial 500
500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 packages
請注意,docker-engine 沒有安裝,但安裝的候選人是從 docker 庫中的 ubuntu 16.04。該 docker-engine 版本號可能會不同。
最后,安裝 docker:
sudo apt-get install -y docker-engine
現(xiàn)在應(yīng)該安裝 docker,啟動守護進程,啟動進程啟動。檢查它是否正在運行:
sudo systemctl status docker
輸出應(yīng)類似于以下內(nèi)容,顯示服務(wù)是活動的并正在運行:
output
● docker.service - docker application container engine
loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
active: active (running) since sun 2016-05-01 06:53:52 cdt; 1 weeks 3 days ago
docs: https://docs.docker.com
main pid: 749 (docker)
安裝 docker 現(xiàn)在給你不只是 docker 服務(wù)(守護進程),而且 docker 命令行實用程序或 docker 客戶端。我們將探討如何使用 docker 在本教程后面的命令。
第 2 步 – 執(zhí)行沒有 sudo 的 docker 命令(可選)
默認(rèn)情況下,在運行 docker 命令需要 root 權(quán)限 - 也就是說,你必須前綴命令 sudo。它也可以由搬運工組,它是在安裝 docker 期間自動創(chuàng)建中的用戶運行。如果你嘗試運行 docker 沒有用前綴它命令 sudo 或不 docker 組之中,你會得到這樣的輸出:
output
docker: cannot connect to the docker daemon. is the docker daemon running on this host?.
see docker run --help .
如果你想避免打字 sudo 只要您運行的 docker 命令,您的用戶名添加到 docker 組:
sudo usermod -ag docker $(whoami)
您將需要注銷 droplet 并作為同一用戶重新啟用以啟用此更改。
如果需要添加用戶到 docker,你沒有作為登錄組,明確宣布用戶名使用:
sudo usermod -ag docker username
本文的其余部分假定您運行的 docker 作為 docker 的用戶組中的用戶命令。如果您選擇不,請前面加上命令 sudo。
第 3 步 – 使用 docker 命令
隨著 docker 的安裝和工作,現(xiàn)在是時候熟悉命令行實用程序。使用 docker 由它傳遞的選項和命令后跟參數(shù)的鏈條。語法采用以下形式:
docker [option] [command] [arguments]
要查看所有可用的子命令,請鍵入:
docker
從 docker 1.11.1 開始,可用子命令的完整列表包括:
output
attach attach to a running container
build build an image from a dockerfile
commit create a new image from a container s changes
cp copy files/folders between a container and the local filesystem
create create a new container
diff inspect changes on a container s filesystem
events get real time events from the server
exec run a command in a running container
export export a container s filesystem as a tar archive
history show the history of an image
images list images
import import the contents from a tarball to create a filesystem image
info display system-wide information
inspect return low-level information on a container or image
kill kill a running container
load load an image from a tar archive or stdin
login log in to a docker registry
logout log out from a docker registry
logs fetch the logs of a container
network manage docker networks
pause pause all processes within a container
port list port mappings or a specific mapping for the container
ps list containers
pull pull an image or a repository from a registry
push push an image or a repository to a registry
rename rename a container
restart restart a container
rm remove one or more containers
rmi remove one or more images
run run a command in a new container
save save one or more images to a tar archive
search search the docker hub for images
start start one or more stopped containers
stats display a live stream of container(s) resource usage statistics
stop stop a running container
tag tag an image into a repository
top display the running processes of a container
unpause unpause all processes within a container
update update configuration of one or more containers
version show the docker version information
volume manage docker volumes
wait block until a container stops, then print its exit code
要查看特定命令可用的開關(guān),請鍵入:
docker docker-subcommand --help
要查看有關(guān) docker 的系統(tǒng)范圍的信息,請使用:
docker info
第 4 步 – 使用 docker 鏡像
docker 容器是從 docker 鏡像運行的。默認(rèn)情況下,它從 docker hub(一個由 docker 項目管理的 docker 注冊中心)提取這些鏡像。任何人都可以在 docker hub 上構(gòu)建和托管 docker 鏡像,所以大多數(shù)應(yīng)用程序和 linux 發(fā)行版都需要運行 docker 容器,這些鏡像托管在 docker hub 上。
要檢查是否可以從 docker hub 訪問和下載圖像,請鍵入:
docker run hello-world
輸出,應(yīng)該包括以下內(nèi)容,應(yīng)該表明 docker 正常工作:
output
hello from docker.
this message shows that your installation appears to be working correctly.
...
您可以通過搜索有關(guān) docker hub 可用圖像 docker 用命令 search 子命令。例如,要搜索 ubuntu 映像,請鍵入:
docker search ubuntu
該腳本將抓取 docker hub 并返回其名稱與搜索字符串匹配的所有圖像的列表。在這種情況下,輸出將類似于:
output
name description stars official automated
ubuntu ubuntu is a debian-based linux operating s... 3808 [ok]
ubuntu-upstart upstart is an event-based replacement for ... 61 [ok]
torusware/speedus-ubuntu always updated official ubuntu docker imag... 25 [ok]
rastasheep/ubuntu-sshd dockerized ssh service, built on top of of... 24 [ok]
ubuntu-debootstrap debootstrap --variant=minbase --components... 23 [ok]
nickistre/ubuntu-lamp lamp server on ubuntu 6 [ok]
nickistre/ubuntu-lamp-wordpress lamp on ubuntu with wp-cli installed 5 [ok]
nuagebec/ubuntu simple always updated ubuntu docker images
... 4 [ok]
nimmis/ubuntu this is a docker images
different lts vers... 4 [ok]
maxexcloo/ubuntu docker base image built on ubuntu with sup... 2 [ok]
admiringworm/ubuntu base ubuntu images based on the official u... 1 [ok]
...
在正式列,ok 表示建造和項目背后的公司支持的圖像。一旦你確定你想使用的圖像,你可以用它下載到你的電腦 pull 子,就像這樣:
docker pull ubuntu
圖像下載后,您可以使用下載的圖像與當(dāng)時運行的容器 run 命令。如果圖像尚未下載時,docker 與執(zhí)行 run 命令后,docker 客戶端將首先下載的圖像,然后使用它運行一個容器:
docker run ubuntu
要查看已下載到您的計算機的圖像,請鍵入:
docker images
輸出應(yīng)類似于以下內(nèi)容:
output
repository tag image id created size
ubuntu latest c5f1cf30c96b 7 days ago 120.8 mb
hello-world latest 94df4f0ce8a4 2 weeks ago 967 b
正如您將在本教程中,你用來運行容器的圖像可以修改和用于產(chǎn)生新的圖像,然后可以上傳后面看到(推是技術(shù)術(shù)語),以 docker 集線器或其他 docker 登記。
第 5 步 – 運行 docker 容器
該 hello-world 你在前面跑容器是運行和退出,散發(fā)出測試消息后的容器的一個例子。然而,容器可以比這更有用,它們可以是交互式的。畢竟,它們類似于虛擬機,只是更加資源友好。
例如,讓我們使用 ubuntu 的最新鏡像運行一個容器。- i 和 - t 參數(shù)的組合為您提供了交互 shell 訪問到容器中:
docker run -it ubuntu
您的命令提示符應(yīng)該更改以反映您現(xiàn)在在容器內(nèi)工作的事實,并應(yīng)采取以下形式:
output
root@d9b100f2f636:/#
重要提示:請注意,在命令提示符容器 id。在上面的例子中,它是 d9b100f2f636。
現(xiàn)在你可以在容器中運行任何命令。例如,讓我們更新容器中的包數(shù)據(jù)庫。無需前綴任何命令 sudo,因為你具有 root 權(quán)限的容器內(nèi)工作:
apt-get update
然后在其中安裝任何應(yīng)用程序。讓我們安裝 nodejs,例如。
apt-get install -y nodejs
第 6 步 – 將容器中的更改提交到 docker 鏡像
默認(rèn)情況下 docker 文件系統(tǒng)是臨時的。如果啟動 docker 鏡像,您可以像虛擬機一樣創(chuàng)建,修改和刪除文件。但是,如果您停止容器并重新啟動,所有更改將會丟失:之前刪除的所有文件現(xiàn)在都將恢復(fù),并且您創(chuàng)建的所有新文件或修改都不會出現(xiàn)。這是因為 docker 鏡像比標(biāo)準(zhǔn)虛擬化世界中的圖像更像模板。
要了解如何讓他們持續(xù)通過容器的重啟保留在容器內(nèi)這些變化,你需要使用 docker 的數(shù)據(jù)量。請參閱如何在 ubuntu 14.04 與 docker 的數(shù)據(jù)量工作的詳細(xì)信息。
本節(jié)介紹如何將容器的狀態(tài)保存為新的 docker 鏡像。
在 ubuntu 容器中安裝 nodejs 之后,現(xiàn)在有一個容器運行一個映像,但容器不同于用來創(chuàng)建它的映像。
要將容器的狀態(tài)保存為新圖像,請先退出該容器:
exit
然后使用以下命令將更改提交到新的 docker 映像實例。- m 開關(guān)是提交信息,可以幫助您和其他人知道你做什么樣的變化,而 - a 用于指定的作者。容器 id 是您在啟動交互式 docker 會話時在教程中前面提到的那個。除非在 docker hub 上創(chuàng)建了其他存儲庫,否則該存儲庫通常是您的 docker hub 用戶名:
docker commit -m what did you do to the image -a author name container-id repository/new_image_name
例如:
docker commit -m added node.js -a sunday ogwu-chinuwa d9b100f2f636 finid/ubuntu-nodejs
注意:當(dāng)你提交圖像時,新的圖像保存在本地,也就是您的計算機上。
在本教程的后面,您將學(xué)習(xí)如何將圖像推送到 docker 注冊表(如 docker hub),以便您和其他人評估和使用它。
在該操作完成后,列出 docker 圖像現(xiàn)在在您的計算機上應(yīng)該顯示新的圖像,以及它的舊圖像,它派生自:
docker images
輸出應(yīng)類似于:
output
finid/ubuntu-nodejs latest 62359544c9ba 50 seconds ago 206.6 mb
ubuntu latest c5f1cf30c96b 7 days ago 120.8 mb
hello-world latest 94df4f0ce8a4 2 weeks ago 967 b
在上面的例子中,ubuntu 的的 nodejs 是新的圖像,將其從 docker 集線器現(xiàn)有的 ubuntu 圖像而得。大小差異反映了所做的更改。在這個例子中,更改是 nodejs 已安裝。所以,下次你需要運行一個容器使用 ubuntu 的 nodejs 預(yù)安裝,你可以只使用新的形象。圖像也可以從所謂的 dockerfile 構(gòu)建。但是這是一個非常復(fù)雜的過程,完全超出了本文的范圍。
第 7 步 – 列出 docker 容器
使用 docker 一段時間后,您的計算機上將有許多活動(正在運行)和不活動的容器。要查看活躍的,使用方法:
docker ps
您將看到類似于以下內(nèi)容的輸出:
output
container id image command created status ports names
f7c79cc556dd ubuntu /bin/bash 3 hours ago up 3 hours silly_spence
要查看所有的容器 - 活動和非活動,它傳遞 - a 開關(guān):
docker ps -a
要查看您所創(chuàng)建的最新的容器,它傳遞 - l 開關(guān):
docker ps -l
停止正在運行或活動的容器就像輸入:
docker stop container-id
該 container-id 可以從輸出中發(fā)現(xiàn),docker ps 命令。
第 8 步 – 將 docker 映像推送到 docker 存儲庫
從現(xiàn)有映像創(chuàng)建新映像后的下一個邏輯步驟是與您選擇的幾個朋友,docker hub 的整個世界或您可以訪問的其他 docker 注冊表共享它。要將映像推送到 docker hub 或任何其他 docker 注冊表,您必須有一個帳戶。
本節(jié)介紹如何將 docker 鏡像推送到 docker hub。要了解如何創(chuàng)建自己的私人 docker 注冊表,看看如何建立一個私人 docker 登記在 ubuntu 14.04。
要創(chuàng)建 docker hub 帳戶,注冊在 docker hub。之后,要推送您的映像,首先登錄到 docker hub。系統(tǒng)會提示您驗證:
docker login -u docker-registry-username
如果指定了正確的密碼,身份驗證應(yīng)該會成功。然后您可以使用以下方式推送您自己的圖片:
docker push docker-registry-username/docker-image-name
它需要一段時間來完成,當(dāng)完成后,輸出將類似于以下內(nèi)容:
output
the push refers to a repository [docker.io/finid/ubuntu-nodejs]
e3fbbfb44187: pushed
5f70bf18a086: pushed
a3b5c80a4eba: pushed
7f18b442972b: pushed
3ce512daaf78: pushed
7aae4540b42d: pushed
..
將圖片推送到注冊表后,應(yīng)將其列在您帳戶的信息中心上,如下圖所示。
如果推送嘗試導(dǎo)致此類錯誤,則可能未登錄:
output
the push refers to a repository [docker.io/finid/ubuntu-nodejs]
e3fbbfb44187: preparing
5f70bf18a086: preparing
a3b5c80a4eba: preparing
7f18b442972b: preparing
3ce512daaf78: preparing
7aae4540b42d: waiting
unauthorized: authentication required
登錄,然后重復(fù)推送嘗試。
以上就是關(guān)于“Ubuntu 下如何安裝與使用 Docker”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望丸趣 TV 小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注丸趣 TV 行業(yè)資訊頻道。