如何在Debian 9上安裝和使用Docker


介紹

Docker是一個簡化容器中應用程序進程管理過程的應用程序。容器允許您在資源隔離的進程中運行應用程序。它們與虛擬機類似,但容器更便攜,更加資源友好,並且更依賴於主機操作系統。

在本教程中,您將在Debian 9上安裝和使用Docker Community Edition(CE)。您將安裝Docker本身,使用容器和映像,並將映像推送到Docker存儲庫。

先決條件

要學習本教程,您需要具備以下條件:

  • 一個Debian 9服務器按照Debian 9初始服務器設置指南設置,沒有服務器的同學可以在這里購買,不過我個人更推薦您使用免費的騰訊雲開發者實驗室進行試驗,學會安裝后再購買服務器
  • 一個具有sudo權限的非root用戶。
  • 在您的服務器上啟用防火牆,如果您使用的是騰訊雲的CVM服務器,您可以直接在騰訊雲控制台中的安全組進行設置。
  • 如果您希望創建自己的圖像並將其推送到Docker Hub,則可以使用Docker Hub上的帳戶,如步驟7和8所示。

第1步 - 安裝Docker

官方Debian存儲庫中提供的Docker安裝包可能不是最新版本。為了確保我們獲得最新版本,我們將從官方Docker存儲庫安裝Docker。為此,我們將添加一個新的包源,從Docker添加GPG密鑰以確保下載有效,然后安裝該包。

首先,更新現有的包列表:

sudo apt update

接下來,安裝一些允許apt使用包通過HTTPS的必備軟件包:

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

然后將官方Docker存儲庫的GPG密鑰添加到您的系統:

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

將Docker存儲庫添加到APT源:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"

接下來,使用新添加的repo中的Docker包更新包數據庫:

sudo apt update

確保您要從Docker repo而不是默認的Debian repo安裝:

apt-cache policy docker-ce

雖然Docker的版本號可能不同,但您會看到這樣的輸出:

docker-ce: Installed: (none) Candidate: 18.06.1~ce~3-0~debian Version table: 18.06.1~ce~3-0~debian 500 500 https://download.docker.com/linux/debian stretch/stable amd64 Packages

請注意,docker-ce未安裝,但安裝的候選者來自Debian 9(stretch)的Docker存儲庫。

最后,安裝Docker:

sudo apt install docker-ce

現在應該安裝Docker,守護進程啟動,並啟用進程啟動進程。檢查它是否正在運行:

sudo systemctl status docker

輸出應類似於以下內容,表明該服務處於活動狀態並正在運行:

● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2018-07-05 15:08:39 UTC; 2min 55s ago Docs: https://docs.docker.com Main PID: 21319 (dockerd) CGroup: /system.slice/docker.service ├─21319 /usr/bin/dockerd -H fd:// └─21326 docker-containerd --config /var/run/docker/containerd/containerd.toml

現在安裝Docker不僅可以為您提供Docker服務(守護程序),還可以為您提供docker命令行實用程序或Docker客戶端。我們將在本教程后面探討如何使用該docker命令。

第2步 - 在沒有Sudo的情況下執行Docker命令(可選)

默認情況下,該docker命令只能由root用戶或docker組中的用戶運行,該用戶在Docker的安裝過程中自動創建。如果您嘗試運行該docker命令而不使用sudo或不在docker組中作為前綴,您將獲得如下輸出:

docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?. See 'docker run --help'.

如果要在運行docker命令時避免鍵入sudo,請將用戶名添加到docker組中:

sudo usermod -aG docker ${USER}

要應用新的組成員身份,請注銷服務器並重新登錄,或鍵入以下內容:

su - ${USER}

系統將提示您輸入用戶密碼以繼續。

通過鍵入以下內容確認您的用戶現已添加到docker組:

id -nG
sammy sudo docker

如果您需要將用戶添加到您未登錄的docker組中,請使用以下方式明確聲明該用戶名:

sudo usermod -aG docker username

本文的其余部分假定您以docker組中的用戶身份運行該docker命令。如果您選擇不這樣做,請使用sudo在前面添加命令。

讓我們接下來探討docker命令。

第3步 - 使用Docker命令

使用docker包括傳遞一系列選項和命令,后跟參數。語法采用以下形式:

docker [option] [command] [arguments]

要查看所有可用的子命令,請鍵入:

docker

從Docker 18開始,可用子命令的完整列表包括:

​
  attach      Attach local standard input, output, and error streams 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 to files or directories 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 Docker objects kill Kill one or more running containers 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 pause Pause all processes within one or more containers 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 one or more containers 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 (streamed to STDOUT by default) 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 one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes

要查看特定命令可用的選項,請鍵入:

docker docker-subcommand --help

要查看有關Docker的系統范圍信息,請使用:

docker info

讓我們探討其中的一些命令。我們將從處理圖像開始。

第4步 - 使用Docker鏡像

Docker容器是從Docker鏡像構建的。默認情況下,Docker從Docker Hub中獲取這些映像,Docker Hub是由Docker管理的Docker注冊表,Docker項目背后的公司。任何人都可以在Docker Hub上托管他們的Docker鏡像,因此您需要的大多數應用程序和Linux發行版都將在那里托管圖像。

要檢查您是否可以從Docker Hub訪問和下載圖像,請鍵入:

docker run hello-world

輸出將指示Docker正常工作:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world 9db2ca6ccae0: Pull complete Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc Status: Downloaded newer image for hello-world:latest ​ Hello from Docker! This message shows that your installation appears to be working correctly. ...

Docker最初無法在本地找到hello-world圖像,因此它從Docker Hub下載了圖像,Docker Hub是默認存儲庫。下載映像后,Docker從映像創建了一個容器,並在容器中執行了應用程序,顯示了該消息。

您可以通過使用命令 search子命令和 docker 命令來搜索Docker Hub上可用的圖像。例如,要搜索Ubuntu映像,請鍵入:

docker search ubuntu

該腳本將對Docker Hub進行爬網,並返回名稱與搜索字符串匹配的所有圖像的列表。在這種情況下,輸出將類似於:

NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                 Ubuntu is a Debian-based Linux operating sys…   8320 [OK] dorowu/ubuntu-desktop-lxde-vnc Ubuntu with openssh-server and NoVNC 214 [OK] rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 170 [OK] consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 128 [OK] ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 95 [OK] ubuntu-upstart Upstart is an event-based replacement for th… 88 [OK] neurodebian NeuroDebian provides neuroscience research s… 53 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 43 [OK] ubuntu-debootstrap debootstrap --variant=minbase --components=m… 39 [OK] nuagebec/ubuntu Simple always updated Ubuntu docker images w… 23 [OK] tutum/ubuntu Simple Ubuntu docker images with SSH access 18 i386/ubuntu Ubuntu is a Debian-based Linux operating sys… 13 1and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 12 [OK] ppc64le/ubuntu Ubuntu is a Debian-based Linux operating sys… 12 eclipse/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 6 [OK] darksheer/ubuntu Base Ubuntu Image -- Updated hourly 4 [OK] codenvy/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 4 [OK] 1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 ubuntu-16-nginx-php-5.6-wordpress-4 3 [OK] pivotaldata/ubuntu A quick freshening-up of the base Ubuntu doc… 2 1and1internet/ubuntu-16-sshd ubuntu-16-sshd 1 [OK] ossobv/ubuntu Custom ubuntu image from scratch (based on o… 0 smartentry/ubuntu ubuntu with smartentry 0 [OK] 1and1internet/ubuntu-16-healthcheck ubuntu-16-healthcheck 0 [OK] pivotaldata/ubuntu-gpdb-dev Ubuntu images for GPDB development 0 paasmule/bosh-tools-ubuntu Ubuntu based bosh-cli 0 [OK] ...

OFFICIAL列中,OK表示由項目后面的公司構建和支持的圖像。確定要使用的映像后,可以使用pull子命令將其下載到計算機。

執行以下命令將官方ubuntu映像下載到您的計算機:

docker pull ubuntu

您將看到以下輸出:

Using default tag: latest latest: Pulling from library/ubuntu 6b98dfc16071: Pull complete 4001a1209541: Pull complete 6319fc68c576: Pull complete b24603670dc3: Pull complete 97f170c87c6f: Pull complete Digest: sha256:5f4bdc3467537cbbe563e80db2c3ec95d548a9145d64453b06939c4592d67b6d Status: Downloaded newer image for ubuntu:latest

下載映像后,可以使用帶有run子命令的下載映像運行容器。正如您在hello-world示例中看到的,如果在使用run子命令執行docker時未下載圖像,則Docker客戶端將首先下載圖像,然后使用它運行容器。

要查看已下載到計算機的圖像,請鍵入:

docker images

輸出應類似於以下內容:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              16508e5c265d        13 days ago         84.1MB hello-world latest 2cb0d9787c4d 7 weeks ago 1.85kB

正如您將在本教程后面看到的那樣,用於運行容器的圖像可以被修改並用於生成新圖像,然后可以將其上載(推送是技術術語)到Docker Hub或其他Docker注冊表。

我們來看看如何更詳細地運行容器。

第5步 - 運行Docker容器

在您在上一步中運行的hello-world容器是一類容器的例子,那就是發出測試消息后運行並且退出的容器。容器可以比這更有用,它們可以是交互式的。畢竟,它們類似於虛擬機,但是時屬於資源更加友好型的。

舉個例子,讓我們使用Ubuntu的最新圖像運行一個容器。-i-t開關的組合為您提供了對容器的交互式shell訪問:

docker run -it ubuntu

您的命令提示符應該更改以反映您現在正在容器內工作的事實,並應采用以下形式:

root@d9b100f2f636:/#

請注意命令提示符中的容器ID。在這個例子中,它是d9b100f2f636。稍后您需要該容器ID以在要刪除容器時標識容器。

現在您可以在容器內運行任何命令。例如,讓我們更新容器內的包數據庫。您不需要使用sudo來為任何命令添加前綴,因為您以root用戶身份在容器內操作:

apt update

然后在其中安裝任何應用程序。我們安裝Node.js:

apt install nodejs

這將從官方Ubuntu存儲庫中安裝容器中的Node.js. 安裝完成后,驗證是否已安裝Node.js:

node -v

您將看到終端中顯示的版本號:

v8.10.0

您在容器內進行的任何更改僅適用於該容器。

要退出容器,請在提示符處鍵入 exit

讓我們看看下一步管理我們系統上的容器。

第6步 - 管理Docker容器

使用Docker一段時間后,您的計算機上將有許多活動(運行)和非活動容器。要查看活動的,請使用:

docker ps

您將看到類似於以下內容的輸出:

CONTAINER ID        IMAGE               COMMAND             CREATED             

在本教程中,您啟動了兩個容器; 一個來自hello-world圖像,另一個來自ubuntu圖像。兩個容器都不再運行,但它們仍然存在於您的系統上。

要查看所有容器 - 活動和非活動,請 使用-a開關運行docker ps

docker ps -a

您將看到類似於此的輸出:

d9b100f2f636        ubuntu              "/bin/bash"         About an hour ago   Exited (0) 8 minutes ago sharp_volhard 01c950718166 hello-world "/hello" About an hour ago Exited (0) About an hour ago festive_williams

要查看您創建的最新容器,請將其傳遞給-l交換機:

docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
d9b100f2f636        ubuntu              "/bin/bash" About an hour ago Exited (0) 10 minutes ago sharp_volhard

要啟動已停止的容器,請使用docker start,后跟容器ID或容器名稱。讓我們啟動基於Ubuntu的容器,其ID為 d9b100f2f636

docker start d9b100f2f636

容器將啟動,您可以使用docker ps來查看其狀態:

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d9b100f2f636        ubuntu              "/bin/bash"         About an hour ago   Up 8 seconds sharp_volhard

要停止正在運行的容器,請使用docker stop,后跟容器ID或名稱。這次,我們將使用Docker分配容器的名稱,即sharp_volhard

docker stop sharp_volhard

一旦您決定不再需要容器,請使用該docker rm命令將其刪除,再次使用容器ID或名稱。使用該docker ps -a命令查找與hello-world映像關聯的容器的容器ID或名稱,然后將其刪除。

docker rm festive_williams

您可以使用--name開關啟動一個新容器並為其命名。您還可以使用該--rm開關創建一個在停止時自行刪除的容器。有關這些選項和其他選項的更多信息,請參閱該docker run help命令。

容器可以轉換為可用於構建新容器的映像。讓我們來看看它是如何工作的。

步驟7 - 將容器中的更改提交到Docker鏡像

當您啟動Docker鏡像時,您可以像使用虛擬機一樣創建,修改和刪除文件。您所做的更改僅適用於該容器。您可以啟動和停止它,但是一旦使用該docker rm命令銷毀它,更改將永久丟失。

本節介紹如何將容器的狀態保存為新的Docker鏡像。

在Ubuntu容器中安裝Node.js后,您現在有一個運行圖像的容器,但容器與您用來創建它的圖像不同。但是您可能希望稍后重新使用此Node.js容器作為新映像的基礎。

然后使用以下命令將更改提交到新的Docker鏡像實例。

docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name

-m開關是提交信息,可以幫助你和其他人知道你所做的修改,而-a用於指定作者。當您啟動交互式Docker會話時,container_id是您在本教程前面提到的那個。除非您在Docker Hub上創建了其他存儲庫,否則repository通常是您的Docker Hub用戶名。

例如,對於用戶sammy,使用容器ID d9b100f2f636,命令將是:

docker commit -m "added Node.js" -a "sammy" d9b100f2f636 sammy/ubuntu-nodejs

當你提交的圖像,新的圖像在您的計算機上本地保存。在本教程的后面,您將學習如何將映像推送到Docker Hub之類的Docker注冊表,以便其他人可以訪問它。

再次列出Docker圖像將顯示新圖像以及從中派生的舊圖像:

docker images

你會看到這樣的輸出:

REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
sammy/ubuntu-nodejs latest 7c1f35226ca6 7 seconds ago 179MB ubuntu latest 113a43faa138 4 weeks ago 81.2MB hello-world latest e38bc07ac18e 2 months ago 1.85kB

在此示例中,ubuntu-nodejs是新圖像,它是從Docker Hub 的現有ubuntu圖像派生的。尺寸差異反映了所做的變化。在此示例中,更改是NodeJS已安裝。因此,下次需要使用預先安裝了NodeJS的Ubuntu運行容器時,您可以使用新映像。

您還可以從Dockerfile構建映像,這樣可以在新映像中自動安裝軟件。但是,這超出了本教程的范圍。

現在讓我們與他人分享新圖像,以便他們可以從中創建容器。

步驟8 - 將Docker鏡像推送到Docker存儲庫

從現有映像創建新映像之后的下一個邏輯步驟是與您選擇的幾個朋友,Docker Hub上的整個世界或您可以訪問的其他Docker注冊表共享它。要將映像推送到Docker Hub或任何其他Docker注冊表,您必須在那里擁有一個帳戶。

本節介紹如何將Docker鏡像推送到Docker Hub。

要推送圖像,請先登錄Docker Hub。

docker login -u docker-registry-username

系統將提示您使用Docker Hub密碼進行身份驗證。如果您指定了正確的密碼,則身份驗證應該成功。

注意:如果Docker注冊表用戶名與用於創建映像的本地用戶名不同,則必須使用注冊表用戶名標記映像。對於上一步中給出的示例,您可以鍵入:

docker tag sammy/ubuntu-nodejs docker-registry-username/ubuntu-nodejs

然后你可以使用以下方法推送自己的圖像

docker push docker-registry-username/docker-image-name

要將ubuntu-nodejs圖像推送到sammy存儲庫,命令將是:

docker push sammy/ubuntu-nodejs

上傳圖像時,該過程可能需要一些時間才能完成,但完成后,輸出將如下所示:

The push refers to a repository [docker.io/sammy/ubuntu-nodejs] e3fbbfb44187: Pushed 5f70bf18a086: Pushed a3b5c80a4eba: Pushed 7f18b442972b: Pushed 3ce512daaf78: Pushed 7aae4540b42d: Pushed ...

將圖像推送到注冊表后,它應該列在您帳戶的儀表板上,如下圖所示。

如果推送嘗試導致此類錯誤,那么您可能沒有登錄:

OutputThe push refers to a repository [docker.io/sammy/ubuntu-nodejs] e3fbbfb44187: Preparing 5f70bf18a086: Preparing a3b5c80a4eba: Preparing 7f18b442972b: Preparing 3ce512daaf78: Preparing 7aae4540b42d: Waiting unauthorized: authentication required

登錄docker login並重復推送嘗試。然后驗證它是否存在於Docker Hub存儲庫頁面上。

您現在可以使用docker pull sammy/ubuntu-nodejs將圖像拉到新計算機並使用它來運行新容器。

結論

在本教程中,您安裝了Docker,使用了圖像和容器,並將修改后的圖像推送到Docker Hub。

更多Docker教程請前往騰訊雲+社區學習更多知識。


參考文獻:《How to Install and Use Docker on Debian 9》

原創聲明,本文系作者授權雲+社區發表,未經許可,不得轉載。

如有侵權,請聯系 yunjia_community@tencent.com 刪除。

編輯於 2018-11-05
5

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM