介紹Docker是一個簡化容器中應用程序進程管理過程的應用程序。容器允許您在資源隔離的進程中運行應用程序。它們與虛擬機類似,但容器更便攜,更加資源友好,并且更依賴于主機操作系統(tǒng)。 在本教程中,您將在Debian 9上安裝和使用Docker Community Edition(CE)。您將安裝Docker本身,使用容器和映像,并將映像推送到Docker存儲庫。 先決條件要學習本教程,您需要具備以下條件:
第1步 - 安裝Docker官方Debian存儲庫中提供的Docker安裝包可能不是最新版本。為了確保我們獲得最新版本,我們將從官方Docker存儲庫安裝Docker。為此,我們將添加一個新的包源,從Docker添加GPG密鑰以確保下載有效,然后安裝該包。 首先,更新現(xiàn)有的包列表: sudo apt update 接下來,安裝一些允許 sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common 然后將官方Docker存儲庫的GPG密鑰添加到您的系統(tǒng): curl -fsSL https://download./linux/debian/gpg | sudo apt-key add - 將Docker存儲庫添加到APT源: sudo add-apt-repository "deb [arch=amd64] https://download./linux/debian $(lsb_release -cs) stable" 接下來,使用新添加的repo中的Docker包更新包數(shù)據(jù)庫: 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./linux/debian stretch/stable amd64 Packages 請注意, 最后,安裝Docker: sudo apt install docker-ce 現(xiàn)在應該安裝Docker,守護進程啟動,并啟用進程啟動進程。檢查它是否正在運行: sudo systemctl status docker 輸出應類似于以下內(nèi)容,表明該服務處于活動狀態(tài)并正在運行: ● 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. 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 現(xiàn)在安裝Docker不僅可以為您提供Docker服務(守護程序),還可以為您提供 第2步 - 在沒有Sudo的情況下執(zhí)行Docker命令(可選)默認情況下,該 docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?. See 'docker run --help'. 如果要在運行 sudo usermod -aG docker ${USER} 要應用新的組成員身份,請注銷服務器并重新登錄,或鍵入以下內(nèi)容: su - ${USER} 系統(tǒng)將提示您輸入用戶密碼以繼續(xù)。 通過鍵入以下內(nèi)容確認您的用戶現(xiàn)已添加到docker組: id -nG sammy sudo docker 如果您需要將用戶添加到您未登錄的 sudo usermod -aG docker username 本文的其余部分假定您以docker組中的用戶身份運行該 讓我們接下來探討 第3步 - 使用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 要查看有關(guān)Docker的系統(tǒng)范圍信息,請使用: docker info 讓我們探討其中的一些命令。我們將從處理圖像開始。 第4步 - 使用Docker鏡像Docker容器是從Docker鏡像構(gòu)建的。默認情況下,Docker從Docker Hub中獲取這些映像,Docker Hub是由Docker管理的Docker注冊表,Docker項目背后的公司。任何人都可以在Docker Hub上托管他們的Docker鏡像,因此您需要的大多數(shù)應用程序和Linux發(fā)行版都將在那里托管圖像。 要檢查您是否可以從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最初無法在本地找到 您可以通過使用命令 docker search ubuntu 該腳本將對Docker Hub進行爬網(wǎng),并返回名稱與搜索字符串匹配的所有圖像的列表。在這種情況下,輸出將類似于: 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表示由項目后面的公司構(gòu)建和支持的圖像。確定要使用的映像后,可以使用 執(zhí)行以下命令將官方 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 下載映像后,可以使用帶有 要查看已下載到計算機的圖像,請鍵入: docker images 輸出應類似于以下內(nèi)容: REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 16508e5c265d 13 days ago 84.1MB hello-world latest 2cb0d9787c4d 7 weeks ago 1.85kB 正如您將在本教程后面看到的那樣,用于運行容器的圖像可以被修改并用于生成新圖像,然后可以將其上載(推送是技術(shù)術(shù)語)到Docker Hub或其他Docker注冊表。 我們來看看如何更詳細地運行容器。 第5步 - 運行Docker容器在您在上一步中運行的 舉個例子,讓我們使用Ubuntu的最新圖像運行一個容器。-i和-t開關(guān)的組合為您提供了對容器的交互式shell訪問: docker run -it ubuntu 您的命令提示符應該更改以反映您現(xiàn)在正在容器內(nèi)工作的事實,并應采用以下形式: root@d9b100f2f636:/# 請注意命令提示符中的容器ID。在這個例子中,它是 現(xiàn)在您可以在容器內(nèi)運行任何命令。例如,讓我們更新容器內(nèi)的包數(shù)據(jù)庫。您不需要使用 apt update 然后在其中安裝任何應用程序。我們安裝Node.js: apt install nodejs 這將從官方Ubuntu存儲庫中安裝容器中的Node.js. 安裝完成后,驗證是否已安裝Node.js: node -v 您將看到終端中顯示的版本號: v8.10.0 您在容器內(nèi)進行的任何更改僅適用于該容器。 要退出容器,請在提示符處鍵入 讓我們看看下一步管理我們系統(tǒng)上的容器。 第6步 - 管理Docker容器使用Docker一段時間后,您的計算機上將有許多活動(運行)和非活動容器。要查看活動的,請使用: docker ps 您將看到類似于以下內(nèi)容的輸出: CONTAINER ID IMAGE COMMAND CREATED 在本教程中,您啟動了兩個容器; 一個來自 要查看所有容器 - 活動和非活動,請 使用 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 要查看您創(chuàng)建的最新容器,請將其傳遞給 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 d9b100f2f636 容器將啟動,您可以使用 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d9b100f2f636 ubuntu "/bin/bash" About an hour ago Up 8 seconds sharp_volhard 要停止正在運行的容器,請使用 docker stop sharp_volhard 一旦您決定不再需要容器,請使用該 docker rm festive_williams 您可以使用 容器可以轉(zhuǎn)換為可用于構(gòu)建新容器的映像。讓我們來看看它是如何工作的。 步驟7 - 將容器中的更改提交到Docker鏡像當您啟動Docker鏡像時,您可以像使用虛擬機一樣創(chuàng)建,修改和刪除文件。您所做的更改僅適用于該容器。您可以啟動和停止它,但是一旦使用該 本節(jié)介紹如何將容器的狀態(tài)保存為新的Docker鏡像。 在Ubuntu容器中安裝Node.js后,您現(xiàn)在有一個運行圖像的容器,但容器與您用來創(chuàng)建它的圖像不同。但是您可能希望稍后重新使用此Node.js容器作為新映像的基礎。 然后使用以下命令將更改提交到新的Docker鏡像實例。 docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name 該-m開關(guān)是提交信息,可以幫助你和其他人知道你所做的修改,而-a用于指定作者。當您啟動交互式Docker會話時, 例如,對于用戶sammy,使用容器ID 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 在此示例中, 您還可以從 現(xiàn)在讓我們與他人分享新圖像,以便他們可以從中創(chuàng)建容器。 步驟8 - 將Docker鏡像推送到Docker存儲庫從現(xiàn)有映像創(chuàng)建新映像之后的下一個邏輯步驟是與您選擇的幾個朋友,Docker Hub上的整個世界或您可以訪問的其他Docker注冊表共享它。要將映像推送到Docker Hub或任何其他Docker注冊表,您必須在那里擁有一個帳戶。 本節(jié)介紹如何將Docker鏡像推送到Docker Hub。 要推送圖像,請先登錄Docker Hub。 docker login -u docker-registry-username 系統(tǒng)將提示您使用Docker Hub密碼進行身份驗證。如果您指定了正確的密碼,則身份驗證應該成功。 注意:如果Docker注冊表用戶名與用于創(chuàng)建映像的本地用戶名不同,則必須使用注冊表用戶名標記映像。對于上一步中給出的示例,您可以鍵入: docker tag sammy/ubuntu-nodejs docker-registry-username/ubuntu-nodejs 然后你可以使用以下方法推送自己的圖像 docker push docker-registry-username/docker-image-name 要將 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 登錄 您現(xiàn)在可以使用 結(jié)論在本教程中,您安裝了Docker,使用了圖像和容器,并將修改后的圖像推送到Docker Hub。
|
|