湖南网站建设 地址磐石网络,安阳区号查询,培训机构招生方案范文,磁力库前些天发现了一个人工智能学习网站#xff0c;通俗易懂#xff0c;风趣幽默#xff0c;最重要的屌图甚多#xff0c;忍不住分享一下给大家。点击跳转到网站。
如何在 Ubuntu 20.04 上安装和使用 Docker
介绍
Docker是一个可以简化容器中应用程序进程管理过程的应用程序。…前些天发现了一个人工智能学习网站通俗易懂风趣幽默最重要的屌图甚多忍不住分享一下给大家。点击跳转到网站。
如何在 Ubuntu 20.04 上安装和使用 Docker
介绍
Docker是一个可以简化容器中应用程序进程管理过程的应用程序。容器允许你在资源隔离的进程中运行应用程序。它们与虚拟机类似但容器更可移植更资源友好并且更依赖于主机操作系统。
下来将在 Ubuntu 20.04 上安装和使用 Docker Community Edition (CE)。将安装 Docker 本身使用容器和镜像并将镜像推送到 Docker 存储库。
笔记
本文将引导完成在 Ubuntu 服务器上安装 Docker。如果想要一键式方式将 Docker 应用程序部署到实时服务器请等下一期~
先决条件
需要以下内容
按照 Ubuntu 20.04初始服务器设置指南设置一台 Ubuntu 20.04 服务器包括 sudo 非 root 用户和防火墙。
第 1 步 — 安装 Docker
Ubuntu 官方存储库中提供的 Docker 安装包可能不是最新版本。为了确保我们获得最新版本我们将从官方 Docker 存储库安装 Docker。为此我们将添加一个新的包源添加来自 Docker 的 GPG 密钥以确保下载有效然后安装该包。
首先更新现有的软件包列表
sudo apt update接下来安装一些必备包以便apt通过 HTTPS 使用包
sudo apt install apt-transport-https ca-certificates curl software-properties-common然后将官方 Docker 存储库的 GPG 密钥添加到你的系统中
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -将 Docker 存储库添加到 APT 源
sudo add-apt-repository deb [archamd64] https://download.docker.com/linux/ubuntu focal stable这还将使用新添加的存储库中的 Docker 包更新我们的包数据库。
确保要从 Docker 存储库而不是默认的 Ubuntu 存储库进行安装
apt-cache policy docker-ce尽管 Docker 的版本号可能不同但将看到如下输出
apt-cache 策略 docker-ce 的输出
docker-ce:Installed: (none)Candidate: 5:19.03.9~3-0~ubuntu-focalVersion table:5:19.03.9~3-0~ubuntu-focal 500500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages请注意docker-ce尚未安装但安装候选来自 Ubuntu 20.04 的 Docker 存储库 ( focal)。
最后安装 Docker
sudo apt install docker-ceDocker 现在应该已安装守护进程已启动并且该进程可以在启动时启动。检查它是否正在运行
sudo systemctl status docker输出应类似于以下内容表明服务处于活动状态并正在运行
Output● docker.service - Docker Application Container EngineLoaded: 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.socketDocs: https://docs.docker.comMain PID: 24321 (dockerd)Tasks: 8Memory: 46.4MCGroup: /system.slice/docker.service└─24321 /usr/bin/dockerd -H fd:// --containerd/run/containerd/containerd.sock现在安装 Docker 不仅提供 Docker 服务守护进程还提供docker命令行实用程序或 Docker 客户端。我们将在本教程后面探讨如何使用该docker命令。
第 2 步 — 不使用 Sudo 执行 Docker 命令可选
默认情况下该命令只能由root用户或****docker组中的用户docker运行该组是在 Docker 安装过程中自动创建的。如果尝试运行该命令而不添加前缀无论是否在docker组中将得到如下输出dockersudo
Outputdocker: 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 ${USER}要应用新的组成员身份请注销服务器并重新登录或输入以下内容
su - ${USER}系统将提示输入用户密码以继续。
通过输入以下内容确认用户现已添加到docker组
groupsOutputsammy sudo docker如果需要将用户添加到你未登录的组中请使用以下方式显式声明该用户名
sudo usermod -aG docker username本文的其余部分假设你以dockerdocker组中的用户身份运行该命令。如果你选择没有请在命令前面加上sudo
第 3 步 — 使用 Docker 命令
使用docker包括向其传递一系列选项和命令后跟参数。语法采用以下形式
docker [option] [command] [arguments]要查看所有可用的子命令请输入
docker从 Docker 19 开始可用子命令的完整列表包括
Output 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 containers changescp Copy files/folders between a container and the local filesystemcreate Create a new containerdiff Inspect changes to files or directories on a containers filesystemevents Get real time events from the serverexec Run a command in a running containerexport Export a containers 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要查看特定命令可用的选项请输入
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 工作正常
OutputUnable 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:latestHello from Docker!
This message shows that your installation appears to be working correctly....Docker 最初无法在本地找到该hello-world镜像因此它从 Docker Hub默认存储库下载了该镜像。下载镜像后Docker 从镜像创建一个容器并执行容器内的应用程序并显示消息。
可以使用带有子命令docker的命令search来搜索 Docker Hub 上可用的镜像。例如要搜索 Ubuntu 镜像输入
docker search ubuntu该脚本将抓取 Docker Hub 并返回名称与搜索字符串匹配的所有镜像的列表。在这种情况下输出将类似于以下内容
OutputNAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 10908 [OK]
dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 428 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 244 [OK]
consol/ubuntu-xfce-vnc Ubuntu container with headless VNC session… 218 [OK]
ubuntu-upstart Upstart is an event-based replacement for th… 108 [OK]
ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with
...确定要使用的镜像后可以使用pull子命令将其下载到计算机。
执行以下命令将官方ubuntu镜像下载到计算机上
docker pull ubuntu将看到以下输出
OutputUsing 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子命令使用下载的镜像运行容器。如上所示如果使用子命令执行hello-world时尚未下载镜像则 Docker 客户端将首先下载镜像然后运行容器。
要查看已下载到计算机的镜像请输入
docker images输出将类似于以下内容
OutputREPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 1d622ef86b13 3 weeks ago 73.9MB
hello-world latest bf756fb1ae65 4 months ago 13.3kB用于运行容器的镜像可以被修改并用于生成新镜像然后可以将其上传技术术语*“推送”到 Docker Hub 或其他 Docker 注册表。*
第 5 步 — 运行 Docker 容器
上一步中运行的hello-world容器是在发出测试消息后运行并退出的容器示例。
作为示例我们可以使用最新的 Ubuntu 镜像运行一个容器
docker run -it ubuntu命令提示符应更改以反映你现在正在容器内工作的事实并且应采用以下形式
Outputrootd9b100f2f636:/#本文命令提示符中的容器 ID 是d9b100f2f636。之后想要删除容器时将需要D 来识别该容器。
我们可以在容器内运行任何命令。例如更新容器内的包数据库
apt update接着在其中安装任何应用程序。比如安装 Node.js
apt install nodejs这个将从官方 Ubuntu 存储库将 Node.js 安装到容器中。安装完成后验证 Node.js 是否已安装
node -v将看到终端中显示的版本号
Outputv10.19.0在容器内所做的任何更改仅适用于该容器。
要退出容器输入exit即可。
第 6 步 — 管理 Docker 容器
使用 Docker 一段时间后计算机上将有许多活动正在运行和非活动容器。要查看活动的请使用
docker ps将看到类似于以下内容的输出
OutputCONTAINER ID IMAGE COMMAND CREATED在本教程中启动了两个容器一个来自hello-world镜像另一个来自ubuntu镜像。两个容器都不再运行但它们仍然存在于你的系统上。
要查看所有容器活动和非活动请使用docker ps -a
docker ps -a将看到与此类似的输出
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要查看创建的最新容器
docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c08a7a0d0e4 ubuntu /bin/bash 2 minutes ago Exited (0) 40 seconds ago quizzical_mcnulty要启动已停止的容器使用docker start后跟容器 ID 或容器名称
docker start 1c08a7a0d0e4容器将启动可以使用以下docker ps命令查看其状态
OutputCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c08a7a0d0e4 ubuntu /bin/bash 3 minutes ago Up 5 seconds quizzical_mcnulty要停止正在运行的容器使用docker stop后跟容器 ID 或名称。
docker stop quizzical_mcnulty一旦决定不再需要容器使用删除命令docker rm再次使用容器 ID 或名称。
docker rm youthful_curie第 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用于指定作者。container_id是在本教程前面启动交互式 Docker 会话时注意到的那个。除非你在 Docker Hub 上创建了其他存储库否则repository通常是你的 Docker Hub 用户名。
例如对于用户sammy容器 ID 为d9b100f2f636命令将为
docker commit -m added Node.js -a sammy d9b100f2f636 sammy/ubuntu-nodejs当你提交镜像时新镜像将保存在你的计算机本地。
再次列出 Docker 镜像将显示新镜像及其派生自的旧镜像
docker images将看到如下输出
OutputREPOSITORY TAG IMAGE ID CREATED SIZE
sammy/ubuntu-nodejs latest 7c1f35226ca6 7 seconds ago 179MB
...在此示例中ubuntu-nodejs是新镜像它源自ubuntuDocker Hub 的现有镜像。大小差异反映了所做的更改。在这个例子中唯一的变化是安装了 NodeJS。因此下次你需要使用预装 NodeJS 的 Ubuntu 来运行容器时你可以只使用新镜像。
还可以从 Dockerfile构建镜像这使你可以在新镜像中自动安装软件。本章不做解释说明
第 8 步 — 将 Docker 镜像推送到 Docker 存储库
要将镜像推送到 Docker Hub 或任何其他 Docker 注册表你必须有一个帐户。
要推送你的镜像请首先登录 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该过程在上传镜像时可能需要一些时间才能完成但完成后输出将如下所示
OutputThe 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 存储库页面上。