什么視頻網(wǎng)站可以做鏈接深圳網(wǎng)站開發(fā)公司
文章目錄
- 1.Docker介紹
- 2.Docker安裝
- 3.免sudo設(shè)置
- 4. 使用docker命令
- 5.Images
- 6.運(yùn)行docker容器
- 7. 管理docker容器
- 8.創(chuàng)建image
- 9.Push Image
1.Docker介紹
Docker 是一個(gè)簡化在容器中管理應(yīng)用程序進(jìn)程的應(yīng)用程序。容器讓你在資源隔離的進(jìn)程中運(yùn)行你的應(yīng)用程序。類似于虛擬機(jī),但容器更具可移植性,更節(jié)約資源,并且更依賴于主機(jī)操作系統(tǒng)。
常見組件簡介如下
- Docker for Mac ? It allows one to run Docker containers on the Mac OS.
- Docker for Linux ? It allows one to run Docker containers on the Linux OS.
- Docker for Windows ? It allows one to run Docker containers on the Windows OS.
- Docker Engine ? It is used for building Docker images and creating Docker containers.
- Docker Hub ? This is the registry which is used to host various Docker images.
- Docker Compose ? This is used to define applications using multiple Docker containers.
接下來,此文章將記錄在 Ubuntu 20.04 上安裝和使用 Docker Community Edition (CE)。 安裝 Docker 本身,使用容器和鏡像,并將鏡像推送到 Docker 存儲(chǔ)庫。
2.Docker安裝
Ubuntu 官方存儲(chǔ)庫中提供的 Docker 安裝包可能不是最新版本。 為了確保獲得最新版本,我們將從官方 Docker 存儲(chǔ)庫安裝 Docker。 為此,我將添加一個(gè)新的包源,添加 Docker 的 GPG key以確保下載的文件是有效的,然后安裝該包。
首先,更新現(xiàn)有的軟件包列表:
sudo apt update
接下來,安裝一些必備包,讓 apt 通過 HTTPS來獲取相關(guān)package:
這里包含5個(gè)必備包:apt-transport-https、ca-certificates 、curl 、software-properties-commo
sudo apt install apt-transport-https ca-certificates curl software-properties-common
然后將官方 Docker 存儲(chǔ)庫的 GPG key添加到系統(tǒng)中:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
將 Docker 存儲(chǔ)庫添加到 APT 源:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
這將使用新添加的repo中的 Docker 包更新我們的package數(shù)據(jù)庫。
確保要從 Docker 存儲(chǔ)庫而不是默認(rèn)的 Ubuntu 存儲(chǔ)庫進(jìn)行安裝:
apt-cache policy docker-ce
output:
docker-ce:
Installed: (none)
Candidate: 5:19.03.93-0ubuntu-focal
Version table:
5:19.03.93-0ubuntu-focal 500
500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
insalled:(none)代表docer-ce尚未安裝
Candidate: 5:19.03.93-0ubuntu-focal 意指候選安裝,版本是 5:19.03.93-0ubuntu-focal
如果不清楚包的名字,使用如下命令
apt-cache pkgnames|grep docker
output:
golang-github-docker-notary-dev
docker-ce
docker-ce-cli
docker.io
安裝docker
sudo apt install docker-ce
Docker 現(xiàn)在應(yīng)該已安裝,守護(hù)進(jìn)程已啟動(dòng),并且該進(jìn)程可以在os重啟后自動(dòng)啟動(dòng)。 檢查它是否正在運(yùn)行:
sudo systemctl status docker
Output
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020-05-19 17:00:41 UTC; 17s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 24321 (dockerd)
Tasks: 8
Memory: 46.4M
CGroup: /system.slice/docker.service
└─24321 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
現(xiàn)在安裝 Docker 不僅提供了Docker 服務(wù)(守護(hù)進(jìn)程),還提供 docker 命令行工具或 Docker 客戶端。
3.免sudo設(shè)置
默認(rèn)情況下,docker命令只能由root用戶或docker組中的用戶運(yùn)行,該組是在Docker安裝過程中自動(dòng)創(chuàng)建。 如果嘗試運(yùn)行 docker 命令而不使用 sudo 前綴或不在 docker 組中,將會(huì)獲得到如下輸出:
Output
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See ‘docker run --help’.
如果想避免在運(yùn)行 docker 命令時(shí)輸入 sudo,需將用戶名添加到 docker 組:
sudo usermod -aG docker docker
要應(yīng)用新的組成員身份,需要注銷服務(wù)器并重新登錄,或輸入以下內(nèi)容:
su - docker
通過輸入以下內(nèi)容確認(rèn)docker用戶現(xiàn)已添加到 docker 組:
groups
Output
root docker
4. 使用docker命令
使用 docker 包括向其傳遞一系列選項(xiàng)和命令,后跟參數(shù)。 語法采用以下形式:
docker [option] [command] [arguments]
要查看所有可用的子命令,輸入:
docker
從 Docker 19 開始,可用子命令的完整列表包括:
attach Attach local standard input, output, and error streams to a running containerbuild Build an image from a Dockerfilecommit Create a new image from a container's changescp Copy files/folders between a container and the local filesystemcreate Create a new containerdiff Inspect changes to files or directories on a container's filesystemevents Get real time events from the serverexec Run a command in a running containerexport Export a container's filesystem as a tar archivehistory Show the history of an imageimages List imagesimport Import the contents from a tarball to create a filesystem imageinfo Display system-wide informationinspect Return low-level information on Docker objectskill Kill one or more running containersload Load an image from a tar archive or STDINlogin Log in to a Docker registrylogout Log out from a Docker registrylogs Fetch the logs of a containerpause Pause all processes within one or more containersport List port mappings or a specific mapping for the containerps List containerspull Pull an image or a repository from a registrypush Push an image or a repository to a registryrename Rename a containerrestart Restart one or more containersrm Remove one or more containersrmi Remove one or more imagesrun Run a command in a new containersave Save one or more images to a tar archive (streamed to STDOUT by default)search Search the Docker Hub for imagesstart Start one or more stopped containersstats Display a live stream of container(s) resource usage statisticsstop Stop one or more running containerstag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGEtop Display the running processes of a containerunpause Unpause all processes within one or more containersupdate Update configuration of one or more containersversion Show the Docker version informationwait Block until one or more containers stop, then print their exit codes
查看特定命令可用的選項(xiàng),輸入docker docker-subcommand --help,如要進(jìn)一步查看上面stop子命令的選項(xiàng)
docker stop --help
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER…]
Stop one or more running containers
Aliases:
docker container stop, docker stop
Options:
-s, --signal string Signal to send to the container
-t, --time int Seconds to wait before killing the container
查看有關(guān) Docker 的系統(tǒng)面信息,使用:
docker info
output:
Client: Docker Engine - Community
Version: 25.0.4
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.13.0
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.24.7
Path: /usr/libexec/docker/cli-plugins/docker-compose
Server:
Containers: 6
Running: 0
Paused: 0
Stopped: 6
Images: 4
Server Version: 25.0.4
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: ae07eda36dd25f8a1b98dfbf587313b99c0190bb
runc version: v1.1.12-0-g51d5e94
init version: de40ad0
Security Options:
apparmor
seccomp
Profile: builtin
Kernel Version: 5.4.0-172-generic
Operating System: Ubuntu 20.04.6 LTS
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.925GiB
Name: anzfam
ID: bb7ac293-d8c0-4e8f-ac9b-61a802c06a7c
Docker Root Dir: /var/lib/docker
Debug Mode: false
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
5.Images
Docker 容器是從 Docker image構(gòu)建的。 默認(rèn)情況下,Docker 從 Docker Hub 中提取這些image,Docker Hub 是由 Docker 項(xiàng)目背后的公司 Docker 管理的 Docker registry。 任何人都可以在 Docker Hub 上托管其 Docker image,因此大家需要的大多數(shù)應(yīng)用程序和 Linux 發(fā)行版都會(huì)在那里有托管的image。
注:image后面將統(tǒng)一稱作鏡像
要檢查是否可以從 Docker Hub 訪問和下載鏡像,輸入:
docker run hello-world
or
docker container run hello-world
注意:這里列出了兩種命令寫法,前者是舊的命形式,后者是新的命令形式,這種dock+object+command的形式更容易記憶,但為了保持向后兼容性,Docker 仍然保留了舊的命令形式,因此用戶可以根據(jù)自己的習(xí)慣和偏好選擇使用哪種形式。
后面的章節(jié)中,我也會(huì)盡量將這兩種形式都列出來
輸出將表明 Docker 工作正常:
Output
Unable to find image ‘hello-world:latest’ locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:6a65f928fb91fcfbc963f7aa6d57c8eeb426ad9a20c7ee045538ef34847f44f1
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” 鏡像,因此它從默認(rèn)存儲(chǔ)庫 Docker Hub 下載該鏡像。 下載鏡像后,Docker 從鏡像創(chuàng)建一個(gè)容器,并執(zhí)行容器內(nèi)的應(yīng)用程序并顯示消息。
可以使用 docker 命令連同search 子命令來搜索 Docker Hub 上可用的鏡像。 例如,要搜索 Ubuntu 鏡像,輸入:
docker search ubuntu
該腳本將抓取 Docker Hub 并返回名稱與搜索字符串匹配的所有鏡像的列表。 輸出將類似于以下內(nèi)容:
NAME DESCRIPTION STARS OFFICIAL
ubuntu Ubuntu is a Debian-based Linux operating sys… 16927 [OK]
neurodebian NeuroDebian provides neuroscience research s… 106 [OK]
websphere-liberty WebSphere Liberty multi-architecture images … 298 [OK]
open-liberty Open Liberty multi-architecture images based… 64 [OK]
ubuntu-upstart DEPRECATED, as is Upstart (find other proces… 115 [OK]
ubuntu-debootstrap DEPRECATED; use "ubuntu" instead 52 [OK]
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 112
ubuntu/squid Squid is a caching proxy for the Web. Long-t… 84
ubuntu/cortex Cortex provides storage for Prometheus. Long… 4
在“OFFICIAL ”列中,“OK”表示由項(xiàng)目背后的公司構(gòu)建和支持的鏡像。 一旦確定要您想要使用的鏡像像后,可以使用 pull 子命令將其下載到計(jì)算機(jī)。
執(zhí)行以下命令將官方 ubuntu 鏡像下載到計(jì)算機(jī)上:
docker pull ubuntu
將看到以下輸出:
Output
Using default tag: latest
latest: Pulling from library/ubuntu
d51af753c3d3: Pull complete
fc878cd0a91c: Pull complete
6154df8ff988: Pull complete
fee5db0ff82f: Pull complete
Digest: sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
下載鏡像后,您可以通過 run 子命令使用下載的鏡像來運(yùn)行容器。 如同在 hello-world 示例中看到的,如果當(dāng)docker 連同他的子命令一起執(zhí)行時(shí)鏡像時(shí)尚未被下載,則 Docker 客戶端將首先下載鏡像,然后使用它運(yùn)行容器。
要查看已下載到您的計(jì)算機(jī)的鏡像,輸入:
docker images
or
docker image ls
輸出將類似于以下內(nèi)容:
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ca2b0f26964c 9 days ago 77.9MB
hello-world latest d2c94e258dcb 10 months ago 13.3kB
正如將在后面看到的,用于運(yùn)行容器的鏡像可以被修改并用于生成新鏡像,然后可以將其上傳(術(shù)語“push”)到 Docker Hub 或其他 Docker registry。
6.運(yùn)行docker容器
在上面運(yùn)行的 hello-world 容器是一個(gè)容器示例,僅在發(fā)出測試消息后運(yùn)行并退出的。 功能簡單,實(shí)際上容器比這個(gè)更有用,而且可以與之交互。 類似于虛擬機(jī),只是更加節(jié)省資源。
作為示例,我們將使用新的 Ubuntu 鏡像運(yùn)行一個(gè)容器。 -i 和 -t 開關(guān)的組合可以使用 shell 訪問容器交互:
docker run -it ubuntu
or
docker container run -it unbuntu
命令提示符應(yīng)該會(huì)發(fā)生變化,反映現(xiàn)在正在容器內(nèi)工作,將具有以下形式:
Output
root@d9b100f2f636:/#
記下命令提示符中的容器 ID。 在此示例中,它是 d9b100f2f636。 稍后當(dāng)想要?jiǎng)h除容器時(shí),將需要該容器 ID 來識(shí)別該容器。
現(xiàn)在可以在容器內(nèi)運(yùn)行任何命令。 例如,讓我們更新容器內(nèi)的package數(shù)據(jù)庫。 不需要在任何命令前加上 sudo 前綴,因?yàn)橐呀?jīng)以 root 用戶身份在容器內(nèi)進(jìn)行操作:
apt update
然后在其中安裝任何應(yīng)用程序。 我們現(xiàn)在嘗試安裝 Node.js:
apt install nodejs
這將會(huì)從官方 Ubuntu 存儲(chǔ)庫將 Node.js 安裝到容器中。 安裝完成后,驗(yàn)證 Node.js 是否已安裝:
node -v
Output
v10.19.0
在容器內(nèi)所做的任何變更將僅套用于該容器。
要退出容器,在提示符下輸入 exit。
7. 管理docker容器
使用 Docker 一段時(shí)間后,計(jì)算機(jī)上將有許多active(正在運(yùn)行)和inactive容器。 要查看active狀態(tài)的容器,使用:
docker ps
or
docker container ls
output如下:
Output
CONTAINER ID IMAGE COMMAND CREATED
在本文中,啟動(dòng)了兩個(gè)容器; 一個(gè)來自 hello-world 鏡像,另一個(gè)來自 ubuntu 鏡像。 兩個(gè)容器都不再運(yùn)行,但它們?nèi)匀淮嬖谟谙到y(tǒng)上。
要查看所有容器(活動(dòng)和非活動(dòng)),需要使用 -a 開關(guān),否則默認(rèn)只顯示running狀態(tài)的容器
docker ps -a
or
docker ps container ls -a
Output:
1c08a7a0d0e4 ubuntu "/bin/bash" 2 minutes ago Exited (0) 8 seconds ago quizzical_mcnulty
a707221a5f6c hello-world "/hello" 6 minutes ago Exited (0) 6 minutes ago youthful_curie
查看創(chuàng)建的最新容器,使用 -l 開關(guān):
docker ps -l
or
docker container ls -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c08a7a0d0e4 ubuntu "/bin/bash" 2 minutes ago Exited (0) 40 seconds ago quizzical_mcnulty
要啟動(dòng)已停止的容器,使用 docker start,后跟容器 ID 或容器名稱。 現(xiàn)在嘗試啟動(dòng) ID 為 1c08a7a0d0e4 的基于 Ubuntu 的容器:
docker start 1c08a7a0d0e4
or
docker container start 1c08a7a0d0e4
容器將啟動(dòng),可以使用 docker ps 查看其狀態(tài):
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c08a7a0d0e4 ubuntu "/bin/bash" 3 minutes ago Up 5 seconds
要停止正在運(yùn)行的容器,使用 docker stop,后跟container_ID 或名稱。 這次,我們將使用 Docker 為容器分配的名稱,即 quizzical_mcnulty:
docker stop quizzical_mcnulty
or
docker container stop quizzical_mcnulty
一旦決定不再需要容器,使用 docker rm 命令刪除它,再次使用容器 ID 或名稱。 使用 docker ps -a 命令查找與 hello-world 鏡像關(guān)聯(lián)的容器 ID 或名稱并將其刪除。
docker rm youthful_curie
or
docker container rm youthful_curie
當(dāng)然也可以啟動(dòng)一個(gè)新容器并使用 --name 開關(guān)為其命名。 還可以使用 --rm 開關(guān)創(chuàng)建一個(gè)容器,該容器在停止時(shí)會(huì)自行刪除容器。
8.創(chuàng)建image
當(dāng)啟動(dòng)一個(gè) Docker 鏡像時(shí),可以像在虛擬機(jī)中一樣創(chuàng)建、修改和刪除文件。且所做的更改只會(huì)應(yīng)用于該容器。也可以啟動(dòng)和停止它,但一旦使用 docker rm 命令銷毀它,這些更改將永久丟失。
本節(jié)將展示如何將使用中的容器保存為一個(gè)新的 Docker 鏡像。
在 Ubuntu 容器中安裝 Node.js 后,現(xiàn)在有一個(gè)運(yùn)行在鏡像之上的容器,但該容器與前面用于創(chuàng)建它的原始鏡像不同。但是有可能希望以后將這個(gè) Node.js 容器作為新鏡像的基礎(chǔ)。
使用以下命令將更改提交到新的 Docker 鏡像實(shí)例。從容器創(chuàng)建一個(gè)新的鏡像
docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name
or
docker container commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name
- -m 開關(guān)用于commit message,提交時(shí)的說明文字,幫助其他人了解這個(gè)鏡像
- -a 用于指定作者。
- container_id 是您在本章前面啟動(dòng)交互式 Docker 會(huì)話時(shí)記下的 ID。
- repository/new_image_name,除非在 Docker Hub 上創(chuàng)建了其他repository,否則repository通常是您的 Docker Hub 用戶名。
例如,對于用戶 sammy,容器 ID 為 d9b100f2f636,命令為:
docker commit -m "added Node.js" -a "sammy" d9b100f2f636 sammy/ubuntu-nodejs
or
docker container commit -m "added Node.js" -a "sammy" d9b100f2f636 sammy/ubuntu-nodejs
當(dāng)提交鏡像時(shí),新鏡像將保存在計(jì)算機(jī)本地。 接下來,我們將鏡像推送到 Docker registry(例如 Docker Hub),以便其他人可以訪問它。
再次list docker images ,將顯示新鏡像像及其派生它的舊鏡像:
docker images
or
docker image ls
Output
REPOSITORY TAG IMAGE ID CREATED SIZE
sammy/ubuntu-nodejs latest 7c1f35226ca6 7 seconds ago 179MB
…
在此示例中,ubuntu-nodejs 是新鏡像,它源自 Docker Hub 中的現(xiàn)有 ubuntu 鏡像。 大小差異反映了所做的改變。 在這個(gè)例子中,變化是安裝了 NodeJS。 因此,下次需要使用預(yù)裝 NodeJS 的 Ubuntu 來運(yùn)行容器時(shí),可以只使用新鏡像。
還可以從 Dockerfile 構(gòu)建鏡像,這樣就可以在新鏡像中自動(dòng)安裝軟件。 關(guān)于dockerfile構(gòu)建鏡像可以參考其他文章
9.Push Image
從現(xiàn)有鏡像創(chuàng)建新鏡像后,下一個(gè)步驟是與選定的幾個(gè)朋友、Docker Hub 上的整個(gè)世界或您有權(quán)訪問的其他 Docker registry共享它。 要將鏡像推送到 Docker Hub 或任何其他 Docker registry,必須在docker hub官網(wǎng)上注冊一個(gè)賬戶。
這里展示怎樣將Docker 鏡像推送到 Docker Hub。 要了解如何創(chuàng)建自己的私有 Docker registry,請查看如何在設(shè)置私有 Docker registry。
要推送鏡像,請首先登錄 Docker Hub。
docker login -u docker-registry-username
系統(tǒng)將提示使用 Docker Hub 密碼進(jìn)行身份驗(yàn)證。 如果指定了正確的密碼,身份驗(yàn)證應(yīng)該會(huì)成功。
注意:如果您的 Docker registry用戶名與用于創(chuàng)建鏡像的本地用戶名不同,將必須使用您的registry用戶名來tag鏡像。 對于最后一步中給出的示例,可以輸入:
docker tag sammy/ubuntu-nodejs docker-registry-username/ubuntu-nodejs
假如我的本地用戶名是sammy,而docker hub用戶名是lakerhu,則應(yīng)該tag如下:
docker tag sammy/ubuntu-nodejs tommy/ubuntu-nodejs
然后使用以下方式push 這個(gè)鏡像:
docker push docker-registry-username/docker-image-name
要將 ubuntu-nodejs 鏡像推送到 lakerhu存儲(chǔ)庫,命令為:
docker push lakerhu/nodejs
or
docker image push lakerhu/nodejs
Output
Using default tag: latest
The push refers to repository [docker.io/lakerhu/nodejs]
9ba80eacb2ad: Pushing 14.42MB/132.9MB
9ba80eacb2ad: Pushing 89.34MB/132.9MB
9ba80eacb2ad: Pushed
…
將鏡像推送到registry后,它應(yīng)該列在您帳戶的儀表板上,如下圖所示
如果推送嘗試導(dǎo)致此類錯(cuò)誤,則可能沒有登錄:
Output
The push refers to a repository [docker.io/lakerhu/ubuntu-nodejs]
e3fbbfb44187: Preparing
5f70bf18a086: Preparing
a3b5c80a4eba: Preparing
7f18b442972b: Preparing
3ce512daaf78: Preparing
7aae4540b42d: Waiting
unauthorized: authentication required
使用 docker login 登錄并重復(fù)推送嘗試。 然后驗(yàn)證它是否存在于您的 Docker Hub 存儲(chǔ)庫頁面上。
現(xiàn)在可以使用 docker pull sammy/ubuntu-nodejs 將鏡像拉取到新機(jī)器并使用它來運(yùn)行新容器。
docker pull lakerhu/nodejs
or
docker image pull lakerhu/nodejs