Docker 简明教程

Docker - Quick Guide

Docker - Overview

Docker 是一种容器管理服务。Docker 的关键词是 develop, shiprun 。Docker 的整个理念是为了让开发人员能够轻松地开发应用程序,将它们装入容器,然后可以将这些容器部署到任何地方。

Docker is a container management service. The keywords of Docker are develop, ship and run anywhere. The whole idea of Docker is for developers to easily develop applications, ship them into containers which can then be deployed anywhere.

Docker 最初于 2013 年 3 月发布,自此之后,它已成为现代世界开发领域的热门词汇,尤其是在基于敏捷性的项目中。

The initial release of Docker was in March 2013 and since then, it has become the buzzword for modern world development, especially in the face of Agile-based projects.

docker overview

Features of Docker

  1. Docker has the ability to reduce the size of development by providing a smaller footprint of the operating system via containers.

  2. With containers, it becomes easier for teams across different units, such as development, QA and Operations to work seamlessly across applications.

  3. You can deploy Docker containers anywhere, on any physical and virtual machines and even on the cloud.

  4. Since Docker containers are pretty lightweight, they are very easily scalable.

Components of Docker

Docker 具有以下组件

Docker has the following components

  1. Docker for Mac − It allows one to run Docker containers on the Mac OS.

  2. Docker for Linux − It allows one to run Docker containers on the Linux OS.

  3. Docker for Windows − It allows one to run Docker containers on the Windows OS.

  4. Docker Engine − It is used for building Docker images and creating Docker containers.

  5. Docker Hub − This is the registry which is used to host various Docker images.

  6. Docker Compose − This is used to define applications using multiple Docker containers.

我们将在后续章节中详细讨论所有这些组件。

We will discuss all these components in detail in the subsequent chapters.

Docker 官方网站是 https://www.docker.com/ 。此网站包含有关 Docker 软件的所有信息和文档。它还提供了适用于各种操作系统的下载链接。

The official site for Docker is https://www.docker.com/ The site has all information and documentation about the Docker software. It also has the download links for various operating systems.

Installing Docker on Linux

要开始安装 Docker,我们将使用一个 Ubuntu 实例。如果没有 Ubuntu 实例,可以使用 Oracle Virtual Box 设置 Linux 虚拟实例。

To start the installation of Docker, we are going to use an Ubuntu instance. You can use Oracle Virtual Box to setup a virtual Linux instance, in case you don’t have it already.

以下屏幕截图显示了在 Oracle Virtual Box 上安装的简单 Ubuntu 服务器。系统上定义了一个名为 demo 的操作系统用户,该用户对服务器具有完全的根访问权限。

The following screenshot shows a simple Ubuntu server which has been installed on Oracle Virtual Box. There is an OS user named demo which has been defined on the system having entire root access to the sever.

demo button

要安装 Docker,我们需要按照以下步骤操作。

To install Docker, we need to follow the steps given below.

Step 1 - 在安装 Docker 之前,您首先必须确保运行的是正确的 Linux 内核版本。Docker 仅适用于 Linux 内核版本 3.8 及更高版本。我们可以通过运行以下命令执行此操作。

Step 1 − Before installing Docker, you first have to ensure that you have the right Linux kernel version running. Docker is only designed to run on Linux kernel version 3.8 and higher. We can do this by running the following command.

uname

此方法返回 Linux 系统的系统信息。

This method returns the system information about the Linux system.

Syntax

uname -a

Options

a - 用于确保返回系统信息。

a − This is used to ensure that the system information is returned.

Return Value

此方法返回 Linux 系统的以下信息-

This method returns the following information on the Linux system −

  1. kernel name

  2. node name

  3. kernel release

  4. kernel version

  5. machine

  6. processor

  7. hardware platform

  8. operating system

Example

uname –a

Output

当我们运行上述命令时,我们将获得以下结果-

When we run above command, we will get the following result −

output

从输出中,我们可以看到 Linux 内核版本是 4.2.0-27,高于版本 3.8,因此我们可以继续。

From the output, we can see that the Linux kernel version is 4.2.0-27 which is higher than version 3.8, so we are good to go.

Step 2 - 您需要使用以下命令更新包含最新软件包的操作系统-

Step 2 − You need to update the OS with the latest packages, which can be done via the following command −

apt-get

此方法从 Internet 安装软件包到 Linux 系统。

This method installs packages from the Internet on to the Linux system.

Syntax

sudo apt-get update

Options

  1. sudo − The sudo command is used to ensure that the command runs with root access.

  2. update − The update option is used ensure that all packages are updated on the Linux system.

Return Value

None

Example

sudo apt-get update

Output

当我们运行上述命令时,我们将获得以下结果-

When we run the above command, we will get the following result −

example output

此命令将连接到 Internet 并为 Ubuntu 下载最新的系统软件包。

This command will connect to the internet and download the latest system packages for Ubuntu.

Step 3 - 下一步是安装必要的证书,以后将需要使用这些证书与 Docker 网站配合使用来下载必要的 Docker 软件包。可以使用以下命令完成此操作。

Step 3 − The next step is to install the necessary certificates that will be required to work with the Docker site later on to download the necessary Docker packages. It can be done with the following command.

sudo apt-get install apt-transport-https ca-certificates
necessary docker packages

Step 4 - 下一步是添加新的 GPG 密钥。下载 Docker 所需的软件包时,需要此密钥来确保所有数据均已加密。

Step 4 − The next step is to add the new GPG key. This key is required to ensure that all data is encrypted when downloading the necessary packages for Docker.

以下命令将从 keyserver hkp://ha.pool.sks-keyservers.net:80 下载 ID 为 58118E89F3A912897C070ADBF76221572C52609D 的密钥,并将其添加到 adv 密钥串中。请注意,下载必要的 Docker 软件包需要此特定密钥。

The following command will download the key with the ID 58118E89F3A912897C070ADBF76221572C52609D from the keyserver hkp://ha.pool.sks-keyservers.net:80 and adds it to the adv keychain. Please note that this particular key is required to download the necessary Docker packages.

new pgp key

Step 5 − 接下来,根据你使用的 Ubuntu 版本,你需要将相关站点添加到 docker.list for apt package manager ,以便它能够检测 Docker 站点的 Docker 软件包并相应地下载它们。

Step 5 − Next, depending on the version of Ubuntu you have, you will need to add the relevant site to the docker.list for the apt package manager, so that it will be able to detect the Docker packages from the Docker site and download them accordingly.

  1. Precise 12.04 (LTS) ─ deb https://apt.dockerproject.org/repoubuntu-precise main

  2. Trusty 14.04 (LTS) ─ deb https://apt.dockerproject.org/repo/ ubuntu-trusty main

  3. Wily 15.10 ─ deb https://apt.dockerproject.org/repo ubuntu-wily main

  4. Xenial 16.04 (LTS) - https://apt.dockerproject.org/repo ubuntu-xenial main

由于我们的操作系统是 Ubuntu 14.04,因此我们将存储库名称用作“deb https://apt.dockerproject.org/repo ubuntu-trusty main”。

Since our OS is Ubuntu 14.04, we will use the Repository name as “deb https://apt.dockerproject.org/repoubuntu-trusty main”.

然后,我们需要将此存储库添加到 docker.list 中,如上所述。

And then, we will need to add this repository to the docker.list as mentioned above.

echo "deb https://apt.dockerproject.org/repo ubuntu-trusty main”
   | sudo tee /etc/apt/sources.list.d/docker.list
docker list

Step 6 − 接下来,我们发出 apt-get update command 以更新 Ubuntu 系统上的软件包。

Step 6 − Next, we issue the apt-get update command to update the packages on the Ubuntu system.

apt get update command

Step 7 − 如果你想验证软件包管理器是否指向正确的存储库,可以通过发出 apt-cache command 来完成。

Step 7 − If you want to verify that the package manager is pointing to the right repository, you can do it by issuing the apt-cache command.

apt-cache policy docker-engine

在输出中,你将获得以下链接:https://apt.dockerproject.org/repo/ [[role="bare"] [role="bare"]https://apt.dockerproject.org/repo/ ]

In the output, you will get the link to link:https://apt.dockerproject.org/repo/ [[role="bare"]https://apt.dockerproject.org/repo/]

apt get update command

Step 8 − 发出 apt-get update command 以确保本地系统上的所有软件包都是最新的。

Step 8 − Issue the apt-get update command to ensure all the packages on the local system are up to date.

packages updation

Step 9 − 对于 Ubuntu Trusty、Wily 和 Xenial,我们必须安装 linux-image-extra-* 内核软件包,它允许使用 aufs storage driver 。此驱动程序由较新版本的 Docker 使用。

Step 9 − For Ubuntu Trusty, Wily, and Xenial, we have to install the linux-image-extra-* kernel packages, which allows one to use the aufs storage driver. This driver is used by the newer versions of Docker.

可以通过使用以下命令来完成。

It can be done by using the following command.

sudo apt-get install linux-image-extra-$(uname -r)
   linux-image-extra-virtual
aufs storage driver

Step 10 − 最后一步是安装 Docker,我们可以使用以下命令完成此操作 −

Step 10 − The final step is to install Docker and we can do this with the following command −

sudo apt-get install –y docker-engine

此处, apt-get 使用 install 选项从 Docker 网站下载 Docker-engine 镜像,并完成 Docker 安装。

Here, apt-get uses the install option to download the Docker-engine image from the Docker website and get Docker installed.

Docker-engine 是基于 Ubuntu 的系统的 Docker Corporation 的官方软件包。

The Docker-engine is the official package from the Docker Corporation for Ubuntu-based systems.

docker engine

在下一部分中,我们将了解如何检查所安装的 Docker 版本。

In the next section, we will see how to check for the version of Docker that was installed.

Docker Version

要查看正在运行的 Docker 版本,可以发出以下命令 −

To see the version of Docker running, you can issue the following command −

Syntax

docker version

Options

  1. version − It is used to ensure the Docker command returns the Docker version installed.

Return Value

输出将提供系统上已安装的 Docker 版本的各种详细信息。

The output will provide the various details of the Docker version installed on the system.

Example

sudo docker version

Output

运行上述程序时,我们将获得以下结果 −

When we run the above program, we will get the following result −

docker version output

Docker Info

要查看系统上正在运行的 Docker 的更多信息,可以发出以下命令 −

To see more information on the Docker running on the system, you can issue the following command −

Syntax

docker info

Options

  1. info − It is used to ensure that the Docker command returns the detailed information on the Docker service installed.

Return Value

输出将提供系统上已安装 Docker 的各种详细信息,如 −

The output will provide the various details of the Docker installed on the system such as −

  1. Number of containers

  2. Number of images

  3. The storage driver used by Docker

  4. The root directory used by Docker

  5. The execution driver used by Docker

Example

sudo docker info

Output

当我们运行上述命令时,我们将获得以下结果-

When we run the above command, we will get the following result −

docker info example output

Docker for Windows

Docker 已为 Windows 提供开箱即用的支持,但您需要有以下配置才能在 Windows 上安装 Docker。

Docker has out-of-the-box support for Windows, but you need to have the following configuration in order to install Docker for Windows.

System Requirements

Windows OS

Windows 10 64 bit

Memory

2 GB RAM (recommended)

您可以从以下位置下载适用于 Windows 的 Docker − https://docs.docker.com/docker-for-windows/

You can download Docker for Windows from − https://docs.docker.com/docker-for-windows/

docker for windows

Docker ToolBox

Docker ToolBox 已为 Windows 的旧版本(例如 Windows 8.1 和 Windows 7)进行了设计。您需要有以下配置才能在 Windows 上安装 Docker。

Docker ToolBox has been designed for older versions of Windows, such as Windows 8.1 and Windows 7. You need to have the following configuration in order to install Docker for Windows.

System Requirements

Windows OS

Windows 7 , 8, 8.1

Memory

2 GB RAM (recommended)

Virtualization

This should be enabled.

您可以从以下位置下载 Docker ToolBox − https://www.docker.com/products/docker-toolbox

You can download Docker ToolBox from − https://www.docker.com/products/docker-toolbox

docker toolbox

Docker - Installation

让我们了解一下各个产品的安装。

Let’s go through the installation of each product.

Docker for Windows

下载安装程序后,双击它来启动安装程序,然后按照下面给出的步骤进行操作。

Once the installer has been downloaded, double-click it to start the installer and then follow the steps given below.

Step 1 − 单击协议条款,然后单击“安装”按钮继续进行安装。

Step 1 − Click on the Agreement terms and then the Install button to proceed ahead with the installation.

docker setup

Step 2 − 完成后,单击“完成”按钮完成安装。

Step 2 − Once complete, click the Finish button to complete the installation.

finish

Docker ToolBox

下载安装程序后,双击它来启动安装程序,然后按照下面给出的步骤进行操作。

Once the installer has been downloaded, double-click it to start the installer and then follow the steps given below.

Step 1 − 在开始屏幕上单击“下一步”按钮。

Step 1 − Click the Next button on the start screen.

docker toolbox next

Step 2 − 在下一个屏幕上保留默认位置并单击“下一步”按钮。

Step 2 − Keep the default location on the next screen and click the Next button.

destination location

Step 3 − 保留默认组件并单击“下一步”按钮继续。

Step 3 − Keep the default components and click the Next button to proceed.

select components

Step 4 − 保留“其他任务”原样,然后单击“下一步”按钮。

Step 4 − Keep the Additional Tasks as they are and then click the Next button.

additional tasks

Step 5 − 在最终屏幕上,单击“安装”按钮。

Step 5 − On the final screen, click the Install button.

install

Working with Docker Toolbox

现在我们来看一下如何在 Windows 上使用 Docker Toolbox 处理 Docker 容器。第一步是启动 Docker Toolbox 应用程序,在安装 Docker 工具箱时,会在桌面上创建该应用程序的快捷方式。

Let’s now look at how Docker Toolbox can be used to work with Docker containers on Windows. The first step is to launch the Docker Toolbox application for which the shortcut is created on the desktop when the installation of Docker toolbox is carried out.

quickstart terminal

接下来,启动 Docker 工具箱时,您将看到正在进行配置。

Next, you will see the configuration being carried out when Docker toolbox is launched.

docker toolbox launched

完成后,您将看到 Docker 已配置并启动。您将获取一个 Docker 交互式外壳。

Once done, you will see Docker configured and launched. You will get an interactive shell for Docker.

interactive shell

为了测试 Docker 是否正常运行,我们可以使用 Docker run command 下载并运行一个简单的 HelloWorld Docker container

To test that Docker runs properly, we can use the Docker run command to download and run a simple HelloWorld Docker container.

Docker run command 的工作原理如下 −

The working of the Docker run command is given below −

docker run

此命令用于在 Docker 容器中运行命令。

This command is used to run a command in a Docker container.

Syntax

docker run image

Options

  1. Image − This is the name of the image which is used to run the container.

Return Value

输出将在所需容器中运行命令。

The output will run the command in the desired container.

Example

sudo docker run hello-world

此命令将下载 hello-world 映像(如果当前不存在),并将 hello-world 作为容器运行。

This command will download the hello-world image, if it is not already present, and run the hello-world as a container.

Output

当我们运行上述命令时,我们将获得以下结果-

When we run the above command, we will get the following result −

docker container

如果您想在 Windows 上运行 Ubuntu OS,可以使用以下命令下载 Ubuntu 映像 −

If you want to run the Ubuntu OS on Windows, you can download the Ubuntu Image using the following command −

Docker run –it ubuntu bash

此处,您通过 –it 选项告诉 Docker 以交互方式运行命令。

Here you are telling Docker to run the command in the interactive mode via the –it option.

ubuntu image

在输出中,您可以看到 Ubuntu 映像已下载并运行,然后您将以 root 用户身份登录 Ubuntu 容器。

In the output you can see that the Ubuntu image is downloaded and run and then you will be logged in as a root user in the Ubuntu container.

Docker - Hub

Docker Hub 是云上的一个注册表服务,允许您下载由其他社区构建的 Docker 映像。您还可以将您自己构建的 Docker 映像上传到 Docker hub。在本章中,我们将看到如何从 Docker hub 下载并使用 Jenkins Docker 映像。

Docker Hub is a registry service on the cloud that allows you to download Docker images that are built by other communities. You can also upload your own Docker built images to Docker hub. In this chapter, we will see how to download and the use the Jenkins Docker image from Docker hub.

Docker hub 的官方网站是 − https://www.docker.com/community-edition#/add_ons

The official site for Docker hub is − https://www.docker.com/community-edition#/add_ons