Docker 简明教程

How to Install Docker on Linux?

Docker 已彻底改变软件开发行业,允许程序员将他们的应用程序及其所有依赖项打包在小型轻量级单元中,称为容器。通过将应用程序与底层操作系统分离,这些容器在各种环境中提供可靠的性能和轻松部署。

Docker has transformed the software development industry completely by allowing programmers to bundle their apps and all of their dependencies into small, lightweight units known as containers. By separating apps from the underlying operating system, these containers provide reliable performance and easy deployment in a variety of environments.

如果您是希望利用容器化的 Linux 用户,那么本章是您的理想去处。您将能够使用本详细指南中解释的各种方式在 Linux 系统上安装 Docker。无论您喜欢使用预构建的包、下载 DEB 文件还是利用便捷的安装脚本,我们都将根据您的要求提供详细的说明。

If you’re a Linux user hoping to take advantage of containerization, this chapter is the perfect place to be. You will be able to install Docker on your Linux system using various ways that will be explained in this detailed guide. We will provide detailed instructions according to your requirements, regardless of whether you like to use pre-built packages, download DEB files, or make use of handy installation scripts.

我们将详细讨论在 Linux 上安装 Docker 的以下方法 −

We will discuss in detail, the following approaches to installing Docker on Linux −

  1. Install using the apt repository

  2. Install from a package

  3. Install using the convenience script

因此,让我们了解这些方法如何在 Ubuntu 上安装 Docker。

So, let’s understand these approaches to install Docker on Ubuntu.

Prerequisites to Install Docker on Linux

在开始 Docker 安装过程之前,请确保您的 Linux 系统满足先决条件。这将确保 Docker 安装顺利且以最佳方式完成。

Make sure your Linux system satisfies the prerequisites before starting the Docker installation process. This will ensure that the Docker installation is done smoothly and in the best way.

Use a 64-bit Architecture - Docker 在 64 位设置中效果最佳。您可以在终端中使用"uname -m"命令来确认系统的架构。如果您的系统是 32 位,则直接安装 Docker 将很困难。但是,互联网上提供了针对特定用例的备用解决方案。

Use a 64-bit Architecture − Docker works best in a 64-bit setting. You can use the "uname -m" command in your terminal to confirm the architecture of your system. It will be difficult to install Docker directly if your system is 32-bit. However, alternative solutions are available on the internet for particular use cases.

docker installation1

Use Kernel Version 3.10 or Higher - 需要一个稳定的 Linux 内核,才能按照预期以最佳方式运行 Docker。您可以通过以下方法验证您正在使用的内核版本是否为 3.10 或更高版本。

Use Kernel Version 3.10 or Higher − A stable Linux kernel is required for Docker to work as expected and in the best way. You can verify that the kernel version you’re using is 3.10 or higher.

使用终端中的 “uname -r” 命令便可执行此操作。这将为您提供内核版本。如果您需要更新,您可以查阅您的发行版的文档以确定最佳操作方案。

You can do so using the “uname -r” command in your terminal. This will give you the version of your kernel. You may check the documentation for your distribution to determine the best course of action if you need an update.

docker installation2

Package Management - 安装 Docker 的途径在很大程度上取决于您的 Linux 发行版的包管理器。APT (Ubuntu/Debian) 和 Yum (Red Hat/CentOS) 是两个流行的示例。

Package Management − The approach to installing Docker largely depends on the package manager for your Linux distribution. APT (Ubuntu/Debian) and Yum (Red Hat/CentOS) are two popular examples.

如果您熟悉这些发行版,始终建议您遵循针对您的发行版的特有安装指南。在本章中,我们将讨论 Ubuntu 的途径。可以使用类似的命令针对其他 Linux 发行版,具体取决于其包管理器。

It’s always good to follow the installation guidelines unique to your distribution if you are familiar with these distributions. In this chapter, we will discuss the approaches for Ubuntu. Similar commands can be used for other Linux distributions depending on their package managers.

Additional Considerations

Virtualization Support - 您应该确保您的系统与诸如 KVM 的硬件虚拟化技术兼容,以获得更好的性能。在执行特定的容器化应用程序时,这一点尤为重要。

Virtualization Support − You should ensure that your system is compatible with the hardware virtualization technologies such as KVM for better performance. This is especially important when executing specific containerized apps.

Sudo Access - 几乎所有安装技术都需要 sudo 访问权限,因此请确保您拥有该权限。

Sudo Access − Almost all the installation techniques require sudo access, so make sure you have that handy.

一旦满足了这些要求,您便可以最佳方式在您的 Linux 机器上安装 Docker。在下一部分中,我们将探讨不同的安装技术,敬请期待!

Once you meet these requirements, you are in a good position to install Docker in the best possible way on your Linux machine. In the following section, we’ll explore the different installation techniques, so stay tuned!

Installing Docker using APT Repository

在首次在新主机上安装 Docker Engine 之前,重要的是设置 Docker 存储库。此后,您可以从该存储库轻松安装或更新 Docker。可以使用以下一组命令,设置 Docker 的 apt 存储库。

Before installing a Docker Engine on a new host for the first time, it’s important to set up the Docker repository. After that, you can easily install or update Docker from that repository. To set up the Docker’s apt repository, you can use the below set of commands.

$ sudo apt-get update
docker installation3
$ sudo apt-get install ca-certificates curl
docker installation4
$ sudo install -m 0755 -d /etc/apt/keyrings
$ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
$ sudo chmod a+r /etc/apt/keyrings/docker.asc
docker installation5

下一步是将存储库添加到 Apt 源。

The next step is to add the repository to Apt sources.

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc]
      https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update
docker installation6

Note - 如果你使用的是其他 Ubuntu 衍生版,例如 Linux Mint 或其他,则可能需要在上述命令中用 UBUNTU_CODENAME 替换 VERSION_CODENAME。

Note − If you are using another derivative of Ubuntu such as Linux Mint or others, you may need to replace VERSION_CODENAME with UBUNTU_CODENAME in the above command.

下一步是安装 Docker 软件包。要安装最新版本,您可以运行以下命令:

The next step is to install the Docker packages. To install the latest version, you can run the below commands −

$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
docker installation7

在完成安装后,您可以通过运行 hello-world 映像验证安装是否成功。

After you have completed the installation, you can verify the installation is successful by running the hello-world image.

$ sudo docker run hello-world
docker installation8

上述命令从 Docker 存储库下载 hello-world 映像,并运行与此映像关联的容器。它打印一条消息并退出。这验证了在您的 Ubuntu 主机上成功安装 Docker 引擎。

The above command downloads the hello-world image from the Docker repository and runs a container associated with it. It prints a message and exits. This verifies the successful installation of the Docker engine on your Ubuntu host.

无需安装最新版本,您也可以使用以下命令集来安装特定版本的 Docker 引擎。第一个命令列出了适用于 Ubuntu 的 Docker 可用版本。

Instead of installing the latest version, you can also install a specific version of the Docker engine using the below set of commands. The first command lists the available Docker versions for Ubuntu.

# List the available versions:
apt-cache madison docker-ce | awk '{ print $3 }'
docker installation9

然后,您可以在变量中设置所需版本,并使用以下命令进行安装。

Then you can set the desired version in a variable and install it using the below commands.

$ VERSION_STRING=5:26.1.1-1~ubuntu.22.04~jammy
$ sudo apt-get install docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING
   containerd.io docker-buildx-plugin docker-compose-plugin

Installing Docker From a Package

无需从 APT 存储库安装 Docker,您也可以手动下载针对特定发布版本的文件,并进行安装。但如果是这种情况,如果您要升级 Docker 引擎版本,您将必须手动进行操作。

Instead of installing Docker from the APT repository, you can also manually download the deb files for the specific release versions and install them. But in this case, if you want to upgrade the Docker engine versions, you will have to do it manually.

  1. Visit https://download.docker.com/linux/ubuntu/dists/.

  2. Then, select the version of Ubuntu from this list.

  3. Then, go to the path - pool/stable and select the architecture of your Linux host (amd64, armhf, arm64, or s390x).

  4. After that, you need to download the following deb files. containerd.io_<version>_<arch>.debdocker-ce_<version>_<arch>.debdocker-ce-cli_<version>_<arch>.debdocker-buildx-plugin_<version>_<arch>.debdocker-compose-plugin_<version>_<arch>.deb

  5. After downloading these, you can install these packages using the following command. This will also start the Docker daemon automatically.

$ sudo dpkg -i ./containerd.io_<version>_<arch>.deb \
   ./docker-ce_<version>_<arch>.deb \
   ./docker-ce-cli_<version>_<arch>.deb \
   ./docker-buildx-plugin_<version>_<arch>.deb \
   ./docker-compose-plugin_<version>_<arch>.deb

$ sudo service docker start

Installing Docker using Convenience Scripts

您还可以使用 Docker 在 https://get.docker.com/ 中提供的便利脚本来安装 Docker。对于开发环境来说,这很有用,但不建议用于生产。但是,在为生产环境创建自定义供应脚本时也很有用。

You can also install Docker using a convenience script provided by Docker at https://get.docker.com/. This is useful for development environments however not recommended for production. But it is useful in creating a provisioning script customized for production environments also.

这些是该脚本所做的操作及其 limitations

These are the things that the script does along with its limitations.

  1. To run, the script needs root or sudo capabilities.

  2. The script makes an effort to identify your Linux version and distribution and sets up your package manager for you.

  3. You cannot change most installation parameters with the script.

  4. Without requesting approval, the software installs suggestions and prerequisites. This could install a lot of packages, depending on how your host machine is configured right now.

  5. The script installs runc, containerd, and Docker at the most recent stable release by default. Docker versions may unexpectedly upgrade significantly if this script is used to provision a machine. Upgrades should always be tested in a test environment before being deployed to live systems.

  6. It is not intended for the script to upgrade an already-existing Docker installation. Dependencies might not be updated to the anticipated version when using the script to update an already-existing installation, leading to versions that are out of date.

$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh

在基于 Debian 的发行版上,Docker 引擎会自动启动。在基于 RPM 的发行版上,您可以使用 systemctl 或 service 命令手动启动它。

On Debian-based distributions, the Docker engines start automatically. On RPM-based distributions, you can use systemctl or service commands to start it manually.

How to Uninstall Docker Engine on Linux?

在 Ubuntu 上卸载 Docker 引擎很简单。您可以使用以下命令卸载 Docker 软件包,然后删除所有配置、映像、容器等。

Uninstalling the Docker engine on Ubuntu is simple. You can use the below commands to uninstall the Docker packages and then remove all the configurations and images, containers, etc.

$ sudo apt-get purge docker-ce docker-ce-cli containerd.io
   docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
$ sudo rm -rf /var/lib/docker
$ sudo rm -rf /var/lib/containerd

Conclusion

您现在已成功在 Linux 机器上安装 Docker 引擎。在本全面指南中,您已学习了如何从 APT 存储库、下载软件包以及使用方便的脚本来安装 Docker。

You have now installed Docker Engine on your Linux machine successfully. In this comprehensive guide, you have learned how to install Docker from the APT repository, by downloading packages, and also using a convenience script.

您可以选择最适合您要求的方法。我们还说明了在需要删除 Docker 引擎或安装不同版本时,彻底卸载 Docker 引擎的步骤。

You can choose the best approach suitable for your requirements. We have also explained the steps to completely uninstall Docker Engine in case you want to remove it or install a different version.

Frequently Asked Questions

Q1. Is Docker free for Linux?

事实上,独立开发人员和爱好者可以使用 Docker 的免费社区版。此版本包含创建、执行和维护容器的所有必要功能。

Indeed, independent developers and enthusiasts can utilize Docker’s free Community Edition. All of the essential features for creating, executing, and maintaining containers are included in this edition.

Docker 免费版本非常适合刚入门 Linux 的用户,而订阅计划则 附带额外的功能,例如私有注册表和企业支持。

The free edition of Docker is ideal for those just starting with Linux, while subscription plans come with extra features like private registries and enterprise support.

Q2. How much RAM do I need for Docker?

你运行的容器的复杂性将决定你需要多少内存用于 Docker。为基本的 Docker 用途预留至少 2GB 的 RAM 是一个比较公平的经验法则。但是,在容器内部使用大量内存的应用程序可能需要更多内存。

The complexity of the containers you run will determine how much Memory you need for Docker. Setting aside at least 2GB of RAM for basic Docker usage is a fair rule of thumb. But apps that use a lot of memory inside containers can need more.

注意你的系统使用了多少内存,并根据需要修改分配量。

Keep an eye on how much RAM your system is using and modify the allocation as necessary.

Q3. Is Docker a VM?

不,虚拟机 (VM) 和 Docker 容器有一些显著差异。Docker 容器共享宿主的内核,而虚拟机 (VM) 模拟一个完整的操作系统。因此,容器轻量且启动速度比虚拟机更快。

No, there are some significant differences between virtual machines (VMs) and Docker containers. Docker containers share the host’s kernel, but virtual machines (VMs) simulate a whole operating system. As a result, containers are lightweight and start up faster than virtual machines.

将虚拟机 (VM) 视为你计算机内的完整计算机,而容器是共享相同资源的独立进程。

Consider virtual machines (VMs) as entire computers inside your computer, whereas containers are separate processes that share the same resources.

Q4. Is Docker good for beginners?

尽管 Docker 有学习曲线,但其基本思想非常简单。针对初学者的在线资源和教程非常丰富。Docker 有助于优化开发流程,因为它可以将应用程序及其依赖项打包到单独的部分中。

Although Docker has a learning curve, its fundamental ideas are quite simple. Online resources and tutorials geared towards beginners are abundant. Docker is useful for optimizing development processes because it can package apps and their dependencies into separate pieces.

初学者可以利用 Docker 的功能,通过一点工作来改善他们的开发体验。

Beginners can take advantage of Docker’s capabilities to improve their development experience with a little work.

Q5. Which is the best Linux OS for Docker?

大多数流行的 Linux 发行版,包括 Fedora、Ubuntu、Debian 和 CentOS,都对 Docker 有很好的支持。“最佳”操作系统取决于你的舒适度和品味。如果你不熟悉 Linux,Ubuntu 是一个很受欢迎的选择,以其用户友好而闻名。

The majority of popular Linux distributions, including Fedora, Ubuntu, Debian, and CentOS, have good Docker support. The "best" OS is determined by your comfort level and taste. Ubuntu is a well-liked option that is renowned for being user-friendly if you’re new to Linux.

最终,Docker 应该与任何当前的 Linux 发行版兼容,这些发行版都具有强大的包管理器。

In the end, Docker should be compatible with any current Linux distribution that has a robust package manager.