如何在 Ubuntu 18.04 上安装和使用 Docker

此教程的早期版本是由 finid撰写的。

介绍

Docker是一个简化 containers 中的应用程序流程管理过程的应用程序. 容器允许您在资源隔离的流程中运行应用程序. 它们类似于虚拟机,但容器更便携式,更友好资源,更依赖主机操作系统。

有关 Docker 容器的不同组件的详细介绍,请参阅 The Docker Ecosystem: An Introduction to Common Components

在本教程中,你将安装并使用Docker Community Edition(CE)在Ubuntu 18.04上,你将安装Docker本身,处理容器和图像,并将图像推到Docker仓库。

前提条件

要遵循本教程,您将需要以下内容:

  • 一个 Ubuntu 18.04 服务器设置为遵循 Ubuntu 18.04 初始服务器设置指南,包括一个 sudo 非根用户和防火墙。
  • Docker Hub上设置一个帐户,如果你想创建自己的图像并将它们推到 Docker Hub,如步骤 7 和 8. 所示。

步骤 1 - 安装 Docker

在官方 Ubuntu 存储库中可用的 Docker 安装包可能不是最新版本. 为了确保我们得到最新版本,我们将从官方 Docker 存储库安装 Docker. 为了做到这一点,我们将添加一个新的包源,添加 Docker 的 GPG 密钥以确保下载有效,然后安装包。

首先,更新您的现有包列表:

1sudo apt update

接下来,安装一些先决条件的包,允许apt在 HTTPS 上使用包:

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

然后将官方 Docker 存储库的 GPG 密钥添加到您的系统中:

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

将 Docker 存储库添加到 APT 源:

1sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

接下来,从新添加的 Repo 更新包数据库以使用 Docker 包:

1sudo apt update

请确保您即将从Docker repo安装,而不是默认的Ubuntu repo:

1apt-cache policy docker-ce

您将看到这样的输出,但Docker的版本号可能不同:

1[label Output of apt-cache policy docker-ce]
2docker-ce:
3  Installed: (none)
4  Candidate: 18.03.1~ce~3-0~ubuntu
5  Version table:
6     18.03.1~ce~3-0~ubuntu 500
7        500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages

请注意,docker-ce没有安装,但安装的候选人来自 Ubuntu 18.04 的 Docker 存储库(bionic)。

最后,安装Docker:

1sudo apt install docker-ce

Docker现在应该安装,戴蒙开始,并启用了启动过程。

1sudo systemctl status docker

输出应类似于以下,表明服务是活跃和运行的:

1[secondary_label Output]
2 docker.service - Docker Application Container Engine
3   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
4   Active: active (running) since Mon 2021-08-09 19:42:32 UTC; 33s ago
5     Docs: https://docs.docker.com
6 Main PID: 5231 (dockerd)
7    Tasks: 7
8   CGroup: /system.slice/docker.service
9           └─5231 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

现在安装Docker不仅为您提供Docker服务(daemon),还为您提供Docker命令行实用程序或Docker客户端,我们将在本教程中进一步探索如何使用Docker命令。

步骤 2 — 执行Docker命令而无需Sudo(可选)

默认情况下,docker命令只能由 root 用户或由 ** docker** 组中的用户运行,该命令在 Docker 安装过程中自动创建。

1[secondary_label Output]
2docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/create": dial unix /var/run/docker.sock: connect: permission denied.
3See 'docker run --help'.

如果您想避免在运行docker命令时键入sudo,请将您的用户名添加到docker组:

1sudo usermod -aG docker ${USER}

若要应用新的组会员资格,请退出服务器并重新登录,或键入以下内容:

1su - ${USER}

您将被要求输入您的用户密码以继续。

确认您的用户现在已被添加到 docker 组,键入:

1id -nG
1[secondary_label Output]
2sammy sudo docker

如果您需要将用户添加到未登录的docker组中,请明示该用户名使用:

1sudo usermod -aG docker username

本文的其余部分假定您正在运行docker命令作为 docker 组中的用户。

接下来我们来探索docker命令。

步骤 3 – 使用 Docker 命令

使用docker是通过一个选项和命令链,然后是参数。

1docker [option] [command] [arguments]

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

1docker

从Docker 20开始,可用的子命令的完整列表包括:

 1[secondary_label Output]
 2
 3  attach Attach local standard input, output, and error streams to a running container
 4  build Build an image from a Dockerfile
 5  commit Create a new image from a container's changes
 6  cp Copy files/folders between a container and the local filesystem
 7  create Create a new container
 8  diff Inspect changes to files or directories on a container's filesystem
 9  events Get real time events from the server
10  exec Run a command in a running container
11  export Export a container's filesystem as a tar archive
12  history Show the history of an image
13  images List images
14  import Import the contents from a tarball to create a filesystem image
15  info Display system-wide information
16  inspect Return low-level information on Docker objects
17  kill Kill one or more running containers
18  load Load an image from a tar archive or STDIN
19  login Log in to a Docker registry
20  logout Log out from a Docker registry
21  logs Fetch the logs of a container
22  pause Pause all processes within one or more containers
23  port List port mappings or a specific mapping for the container
24  ps List containers
25  pull Pull an image or a repository from a registry
26  push Push an image or a repository to a registry
27  rename Rename a container
28  restart Restart one or more containers
29  rm Remove one or more containers
30  rmi Remove one or more images
31  run Run a command in a new container
32  save Save one or more images to a tar archive (streamed to STDOUT by default)
33  search Search the Docker Hub for images
34  start Start one or more stopped containers
35  stats Display a live stream of container(s) resource usage statistics
36  stop Stop one or more running containers
37  tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
38  top Display the running processes of a container
39  unpause Unpause all processes within one or more containers
40  update Update configuration of one or more containers
41  version Show the Docker version information
42  wait Block until one or more containers stop, then print their exit codes

若要查看特定命令可用的选项,请键入:

1docker docker-subcommand --help

若要查看有关 Docker 的全系统信息,请使用:

1docker info

让我们探索一些这些命令,我们将开始使用图像。

步骤 4 – 使用 Docker 图像

Docker 容器是由 Docker 图像构建的。默认情况下,Docker 将这些图像从 Docker Hub中提取,这是由 Docker 管理的 Docker 注册表,这是 Docker 项目背后的公司。

若要检查您是否可以从 Docker Hub 访问和下载图像,请键入:

1docker run hello-world

输出将表明 Docker 正在正确工作:

 1[secondary_label Output]
 2Unable to find image 'hello-world:latest' locally
 3latest: Pulling from library/hello-world
 4b8dfde127a29: Pull complete
 5Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
 6Status: Downloaded newer image for hello-world:latest
 7
 8Hello from Docker!
 9This message shows that your installation appears to be working correctly.
10. . .

Docker最初无法在本地找到你好的图像,所以它从Docker Hub下载了图像,这是默认存储库。

您可以使用docker命令与search子命令搜索在Docker Hub上可用的图像,例如,要搜索Ubuntu图像,键入:

1docker search ubuntu

该脚本会扫描 Docker Hub 并返回所有与搜索字符串相匹配的图像的列表。

 1[secondary_label Output]
 2NAME DESCRIPTION STARS OFFICIAL AUTOMATED
 3ubuntu Ubuntu is a Debian-based Linux operating sys   7917                [OK]
 4dorowu/ubuntu-desktop-lxde-vnc Ubuntu with openssh-server and NoVNC 193                                     [OK]
 5rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi   156                                     [OK]
 6ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 93                                      [OK]
 7ubuntu-upstart Upstart is an event-based replacement for th   87                  [OK]
 8neurodebian NeuroDebian provides neuroscience research s   50                  [OK]
 9ubuntu-debootstrap debootstrap --variant=minbase --components=m   38                  [OK]
101and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 36                                      [OK]
11nuagebec/ubuntu Simple always updated Ubuntu docker images w   23                                      [OK]
12tutum/ubuntu Simple Ubuntu docker images with SSH access 18
13i386/ubuntu Ubuntu is a Debian-based Linux operating sys   13
14ppc64le/ubuntu Ubuntu is a Debian-based Linux operating sys   12
151and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 10                                      [OK]
161and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10 ubuntu-16-nginx-php-phpmyadmin-mariadb-10 6                                       [OK]
17eclipse/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc,    6                                       [OK]
18codenvy/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc,    4                                       [OK]
19darksheer/ubuntu Base Ubuntu Image -- Updated hourly 4                                       [OK]
201and1internet/ubuntu-16-apache ubuntu-16-apache 3                                       [OK]
211and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 ubuntu-16-nginx-php-5.6-wordpress-4 3                                       [OK]
221and1internet/ubuntu-16-sshd ubuntu-16-sshd 1                                       [OK]
23pivotaldata/ubuntu A quick freshening-up of the base Ubuntu doc   1
241and1internet/ubuntu-16-healthcheck ubuntu-16-healthcheck 0                                       [OK]
25pivotaldata/ubuntu-gpdb-dev Ubuntu images for GPDB development 0
26smartentry/ubuntu ubuntu with smartentry 0                                       [OK]
27ossobv/ubuntu
28...

官方列中,OK表示由该项目背后的公司创建和支持的图像,一旦您确定了您想要使用的图像,您可以使用拖动子命令将其下载到您的计算机。

执行以下命令将官方ubuntu图像下载到您的计算机:

1docker pull ubuntu

您将看到以下输出:

1[secondary_label Output]
2Using default tag: latest
3latest: Pulling from library/ubuntu
416ec32c2132b: Pull complete
5Digest: sha256:82becede498899ec668628e7cb0ad87b6e1c371cb8a1e597d83a47fac21d6af3
6Status: Downloaded newer image for ubuntu:latest
7docker.io/library/ubuntu:latest

在下载图像后,您可以使用下载图像使用运行子命令运行一个容器.正如您在问候世界示例中所看到的那样,如果在使用运行子命令执行docker时没有下载图像,那么Docker客户端首先会下载图像,然后使用它运行一个容器。

要查看已下载到您的计算机的图像,键入:

1docker images

输出应该与以下相似:

1[secondary_label Output]
2REPOSITORY TAG IMAGE ID CREATED SIZE
3ubuntu latest 1318b700e415 13 days ago 72.8MB
4hello-world latest d1165f221234 5 months ago 13.3kB

正如您将在本教程中稍后看到的那样,您用于运行容器的图像可以被修改并用于生成新图像,然后可以上传(_pushed_是技术术语)到Docker Hub或其他Docker注册表。

让我们来看看如何更详细地运行容器。

步骤 5 – 运行 Docker 容器

你在上一步中运行的你好世界容器是发出测试消息后运行的和离开的容器的一个例子。容器比这更有用,并且可以互动。

作为一个例子,让我们使用Ubuntu的最新图像运行一个容器. -i 和 ** -t** 交换机的组合为您提供交互式容器访问:

1docker run -it ubuntu

您的命令提示应该改变以反映您现在在容器内部工作的事实,并应采取以下形式:

1[secondary_label Output]
2[environment second]
3root@d9b100f2f636:/#

在此示例中,它是「d9b100f2f636」。

现在您可以运行容器内部的任何命令,例如,让我们更新容器内部的包数据库,您不需要用sudo预先定义任何命令,因为您作为 root 用户在容器内部运行:

1[environment second]
2apt update

然后安装任何应用程序,让我们安装Node.js:

1[environment second]
2apt install nodejs

此操作将 Node.js 安装到官方 Ubuntu 存储库中的容器中. 安装完成后,请验证 Node.js 已安装:

1[environment second]
2node -v

您将看到在您的终端中显示的版本号:

1[secondary_label Output]
2[environment second]
3v10.19.0

您在容器内部所做的任何更改只适用于该容器。

要退出容器,请在提示处键入退出

让我们来看看我们系统上的集装箱的管理。

步骤6:管理 Docker 容器

使用 Docker 一段时间后,您的计算机上将有许多活跃(运行)和不活跃的容器。

1docker ps

您将看到类似于以下的输出:

1[secondary_label Output]
2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

在本教程中,你开始了两个容器,一个来自你好世界图像,另一个来自ubuntu图像。

要查看所有容器 - 活跃和不活跃,请使用-a交换机运行docker ps:

1docker ps -a

你会看到类似于此的输出:

1e4dcb273b696 ubuntu        "bash"     About a minute ago Exited (0) 30 seconds ago suspicious_hopper
279b892f318e9 hello-world   "/hello"   3 minutes ago Exited (0) 3 minutes ago boring_jang

要查看您创建的最新容器,请通过-l开关:

1docker ps -l
1CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e4dcb273b696 ubuntu    "bash"    2 minutes ago Exited (0) About a minute ago suspicious_hopper

要启动一个停止的容器,请使用docker start,然后是容器ID或容器名称。

1docker start e4dcb273b696

容器将启动,您可以使用docker ps查看其状态:

1CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e4dcb273b696 ubuntu    "bash"    2 minutes ago Up 4 seconds suspicious_hopper

要阻止运行容器,请使用docker stop,然后是容器ID或名称.这次,我们将使用Docker分配给容器的名称,即sharp_volhard:

1docker stop suspicious_hopper

一旦您决定不再需要一个容器,请使用docker rm命令删除它,再次使用容器ID或名称。

1docker rm boring_jang

您可以使用--name交换机启动一个新的容器并给它一个名称,您也可以使用--rm交换机创建一个在停止时自行移除的容器,请参阅docker run help命令,了解有关这些选项和其他信息。

容器可以转化为图像,您可以使用这些图像来构建新的容器。

步骤 7 — 对 Docker 图像的容器进行更改

当您启动 Docker 图像时,您可以创建、修改和删除文件,就像您可以使用虚拟机一样。您所做的更改只适用于该容器。

本节向您展示如何将容器的状态保存为新 Docker 图像。

在 Ubuntu 容器内安装 Node.js 后,您现在有一个容器运行图像,但容器与您创建它时使用的图像不同。

然后使用以下命令对新 Docker 图像实例进行更改。

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

-m 交换器用于请求信息,帮助您和其他人知道您所做的更改,而 ** -a** 用于指定作者。 container_id 是您在启动交互式 Docker 会话时在教程中提到的信息。

例如,对于用户 sammy ,其容器ID为 d9b100f2f636,该命令将是:

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

当您 commit 一个图像时,新图像会被本地保存到您的计算机上. 在本教程中,您将学习如何将图像推到 Docker 注册表,如 Docker Hub,以便其他人可以访问它。

再次列出 Docker 图像将显示新的图像,以及它是由原来的旧图像:

1docker images

你会看到这样的输出:

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

在本示例中,ubuntu-nodejs是新图像,它是从 Docker Hub 的现有ubuntu图像中提取的。大小差异反映了所做的更改。

您还可以从Dockerfile构建图像,这允许您在新图像中自动安装软件。

现在让我们与其他人分享新图像,以便他们可以从中创建容器。

步骤 8 — 将 Docker 图像推到 Docker 存储库

在从现有图像创建新图像后,下一个逻辑步骤是将其与几个朋友共享,在Docker Hub上的整个世界或您可以访问的其他Docker注册表。

要了解如何创建自己的私人 Docker 注册表,请参阅 如何在 Ubuntu 14.04 上设置私人 Docker 注册表

要按下您的图像,请先登录 Docker Hub。

1docker login -u docker-registry-username

您将被要求使用您的 Docker Hub 密码进行身份验证. 如果您指定了正确的密码,则该身份验证应该成功。

<$>[note] ** 注意:** 如果您的 Docker 注册表用户名与您创建图像时使用的本地用户名不同,则需要用您的注册表用户名标记您的图像。

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

美元

然后你可以推自己的图像使用:

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

要将ubuntu-nodejs图像推到 sammy 存储库,命令将是:

1docker push sammy/ubuntu-nodejs

该过程可能需要一些时间来完成,因为它上传图像,但当完成时,输出将看起来像这样:

 1[secondary_label Output]
 2The push refers to a repository [docker.io/sammy/ubuntu-nodejs]
 3e3fbbfb44187: Pushed
 45f70bf18a086: Pushed
 5a3b5c80a4eba: Pushed
 67f18b442972b: Pushed
 73ce512daaf78: Pushed
 87aae4540b42d: Pushed
 9
10...

将图像推到注册表后,该图像应列入您的帐户仪表板,如下图像所示。

New Docker image listing on Docker Hub

如果推试导致此类错误,那么您可能没有登录:

1[secondary_label Output]
2The push refers to a repository [docker.io/sammy/ubuntu-nodejs]
3e3fbbfb44187: Preparing
45f70bf18a086: Preparing
5a3b5c80a4eba: Preparing
67f18b442972b: Preparing
73ce512daaf78: Preparing
87aae4540b42d: Waiting
9unauthorized: authentication required

使用docker login登录并重复推试,然后验证它是否存在于您的 Docker Hub 存储页面上。

您现在可以使用docker pull sammy/ubuntu-nodejs将图像拖到新的机器上,并使用它来运行新的容器。

结论

在本教程中,您安装了Docker,使用了图像和容器,并将修改的图像推到Docker Hub。

Published At
Categories with 技术
comments powered by Disqus