碼頭工人 是一個開源平台,使軟件開發人員能夠構建、部署和管理容器化應用程序。 這意味著應用程序被打包到容器中,容器是在任何操作系統中運行所需的源代碼、庫和其他依賴項的組合。
此外,docker 使使用容器時更簡單、更流暢、更安全。
在本教程中,我將完成在 Ubuntu 20.04 上安裝 Docker 的所有步驟,並學習使用 docker 容器/圖像時的基本概念。
安裝碼頭工人
在官方軟件倉庫中 Ubuntu 20.04,默認提供docker安裝包。 但是,它不是最新版本。 建議從官方 docker 存儲庫安裝最新版本的 docker。
首先,您需要更新當前的軟件包列表並安裝必備軟件包,以便通過運行以下命令將新的存儲庫添加到您的系統中:
$ sudo apt update
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg-agent
接下來,你必須 導入 GPG 密鑰 Docker 存儲庫:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
然後, 添加docker倉庫 到您系統上的 APT 源:
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
運行以下命令更新新添加的存儲庫:
$ sudo apt update
確保您將 安裝碼頭工人 從它的存儲庫而不是 Ubuntu 存儲庫:
$ apt-cache policy docker-ce
你會看到輸出是這樣的:
docker-ce:
Installed: (none)
Candidate: 5:19.03.11~3-0~ubuntu-focal
Version table:
5:19.03.11~3-0~ubuntu-focal 500
500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
5:19.03.10~3-0~ubuntu-focal 500
500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
5:19.03.9~3-0~ubuntu-focal 500
500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
上面的輸出表明 Ubuntu 20.04 的 Docker 存儲庫中的“docker-ce”將安裝在您的系統上。
最後,安裝 docker-this (社區版):
$ sudo apt install docker-ce
為了 核實 無論docker是否安裝成功並正常運行,運行以下命令:
$ sudo systemctl status docker
輸出將顯示 docker 服務處於活動狀態(正在運行):
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2020-06-17 15:29:48 +07; 8min ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 136640 (dockerd)
Tasks: 17
Memory: 50.8M
CGroup: /system.slice/docker.service
└─136640 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
...
你剛剛在你的 Ubuntu 機器上成功安裝了 docker。 我們將在本教程的以下部分深入探討使用 docker 命令的方式。
不執行docker sudo 特權
默認情況下,您必須使用以下命令運行 docker 命令 sudo 特權或 docker 組中的用戶。 如果您嘗試在沒有 sudo 或者俱有不在 docker 組中的用戶權限,您將收到如下錯誤:
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.
解決方法是將用戶添加到“docker”組:
$ sudo usermod -aG docker <user-name>
註銷當前會話,然後再次登錄以申請會員資格。 通過運行以下命令驗證用戶是否在 ‘docker’ 組中:
$ groups
ubuntu sudo docker
從現在開始,您可以在不使用 ‘ 的情況下運行 docker 命令sudo’。
Docker 命令
本節將解釋如何 使用碼頭工人 命令。 在詳細介紹之前,讓我們先看一下“docker”命令的語法:
$ docker [options] [sub-commands] [arguments]
要列出 docker 的所有可用子命令,請運行:
$ docker
有很多選項、子命令和參數需要記住。 讓我們在本教程的以下部分深入研究其中的一些。
Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
context Manage contexts
engine Manage the docker engine
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
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
Run 'docker COMMAND --help' for more information on a command.
Docker 鏡像
Docker 鏡像由源代碼、庫和應用程序作為容器運行所需的所有必要依賴項組成。 Docker 鏡像可以從頭開始構建,但您可以從容器註冊表中提取這些鏡像。 註冊表是您可以存儲圖像並使用唯一名稱下載它們的地方。 默認情況下,docker 的容器註冊中心是 Docker Hub。
為了檢查您是否可以從中提取圖像 碼頭工人中心,您可以運行以下命令:
$ docker run hello-world
正如您在以下輸出中所看到的:docker 在本地找不到“hello-world”鏡像,它從 Docker Hub 拉取了鏡像。 下載鏡像後,docker 會從該鏡像創建一個容器。 容器中的應用程序將被執行並顯示消息:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
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
...
為了在 Docker Hub 上搜索圖像,您可以使用帶有 ‘search’ 子命令的 ‘docker’ 命令。 假設您要搜索 ‘alpine’ 圖像,請運行:
$ docker search alpine

如您所見,輸出返回名稱為“alpine”的所有圖像的列表。
此外,您可以通過運行命令 ‘ 下載 docker 鏡像docker鏡像拉取‘ 後跟圖像的名稱。 為了 example:
$ docker image pull alpine
根據 Internet 的速度,下載可能需要一分鐘。
Using default tag: latest
latest: Pulling from library/alpine
df20fa9351a1: Pull complete
Digest: sha256:185518070891758909c9f839cf4ca393ee977ac378609f700f60a771a2dfe321
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
另外,如果要列出所有下載的圖像,請運行以下命令:
$ docker image ls
輸出將顯示圖像的詳細信息,包括:存儲庫、標籤、圖像 ID、創建時間和文件大小。
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest a24bb4013296 2 weeks ago 5.57MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
有時,你需要 刪除泊塢窗 圖像,您可以運行以下命令:
$ docker image rm <image-name>
或者
$ docker image rm <image-id>
Docker 容器
容器是 docker 鏡像的運行實例。 容器執行單個應用程序或進程的運行時。 您可以通過運行“docker container”子命令與容器交互並啟動/停止或刪除它。
為了 example,為了啟動基於“Ubuntu”映像的 docker 容器,請運行:
ubuntu@linoxide:~$ docker container run ubuntu
從 Docker Hub 下載更新的 Ubuntu 鏡像並啟動 Ubuntu 容器後,它返回系統命令提示符,因為 Ubuntu 容器在啟動後立即停止。
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
a4a2a29f9ba4: Pull complete
127c9761dcba: Pull complete
d13bf203e905: Pull complete
4039240d2e0b: Pull complete
Digest: sha256:35c4a2c15539c6c1e4e5fa4e554dac323ad0107d8eb5c582d6ff386b383b7dce
Status: Downloaded newer image for ubuntu:latest
ubuntu@linoxide:~$
為了通過命令行與 Ubuntu 容器進行交互,您可以使用以下命令啟動容器 -it
選項:
$ docker container run -it ubuntu
現在,您已進入容器並訪問其交互式 shell。
root@45cc68d4395e:/#
到目前為止,您已經在 Ubuntu 系統上運行了許多容器。 要列出所有活動(正在運行)容器,請運行以下命令:
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
由於結果返回一個空列表,因此沒有任何容器正在運行。 如果要列出所有容器:活動和非活動,請運行:
$ docker container ls -a
輸出類似於:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45cc68d4395e ubuntu "/bin/bash" 14 minutes ago Exited (0) 11 minutes ago stoic_dhawan
fc91e50d223e ubuntu "/bin/bash" 26 minutes ago Exited (0) 26 minutes ago sleepy_ritchie
ce0d81682913 alpine "/bin/bash" 26 minutes ago Created trusting_kowalevski
4291477f5ac2 alpine "/bin/sh" 27 minutes ago Exited (130) 27 minutes ago priceless_wozniak
86c0fd44a4f4 alpine "/bin/sh" 28 minutes ago Exited (0) 28 minutes ago objective_agnesi
3fdcaa196fca hello-world "/hello" 31 minutes ago Exited (0) 31 minutes ago eloquent_rhodes
一旦你想刪除/移除一個容器,運行:
$ docker container rm <container-id>
結論
在本教程中,我們通過示例學習瞭如何在 Ubuntu 20.04 上安裝 Docker 以及如何使用圖像、容器。 它們是 Docker 概念的基礎知識,通常由開發人員使用。
感謝您的閱讀,請在下面的評論部分留下您的建議。