Docker 基础知识
安装Docker

安装 Docker

我们将使用 Easylearning Playgroud 来交互式的演示安装 docker 的方式。

实验环境
镜像ID:
3100
镜像名称:
ubuntu20-k3d
实例类型:
2核4G
实例标注:
drill-with-k3d

点击Create My Playground 开始创建新环境,这通常需要 1~2 分钟。

当 WebTerminal 就绪后,你可以复制命令行到终端上执行,或直接点击最右侧的>按钮自动发送命令到当前的终端上执行。

在 Playgound 开始安装 Docker

Ubuntu 作为一个广泛使用的 Linux 发行版,具备许多优势,使得它可以直接安装 Docker 并高效运行。使用 apt 包管理器,这是一个功能强大且易于使用的工具。通过 apt,用户可以方便地安装、更新和管理软件包。Docker 官方提供了适用于 Ubuntu 的安装包,使得安装过程变得非常简单。

更新源列表中获取最新的包信息。

sudo apt update

点击命令行最右侧的三角形图标,会自动命令发送至 Playgroud 中 Terminal 中执行,你将看到类似下面的输出:

Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease
Get:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease [128 kB]
Hit:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Fetched 128 kB in 4s (35.7 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
4 packages can be upgraded. Run 'apt list --upgradable' to see them.

使用 apt 安装 docker,点击命令行最右侧的三角形图标,再将命令发送到 Terminal 中执行:

sudo apt install docker.io -y

你将看到类似下面的输出:

Reading package lists... Done
Building dependency tree
After this operation, 284 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/universe amd64 pigz amd64 2.4-1 [57.4 kB]
...
Get:9 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 ubuntu-fan all 0.12.13ubuntu0.1 [34.4 kB]
Preconfiguring packages ...
Setting up docker.io (24.0.7-0ubuntu2~20.04.1) ...
Adding group `docker' (GID 120) ...
Done.
...

验证 Docker 版本

sudo docker version

得到类似下面的输出,说明安装成功了。

Client:
 Version:           24.0.7
 API version:       1.43
 Go version:        go1.21.1
 Git commit:        24.0.7-0ubuntu2~20.04.1
 Built:             Wed Mar 13 20:29:24 2024
 OS/Arch:           linux/amd64
 Context:           default

Server:
 Engine:
  Version:          24.0.7
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.21.1
  Git commit:       24.0.7-0ubuntu2~20.04.1
  Built:            Wed Mar 13 20:29:24 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.12
  GitCommit:
 runc:
  Version:          1.1.12-0ubuntu2~20.04.1
  GitCommit:
 docker-init:
  Version:          0.19.0
  GitCommit:

Hello World in Docker

我们已经在 Playgound 中安装了 docker,接下来我们来运一下 hello-world, 进一步验证 docker 是否正能常运行,以及初步了解 Docker 的几个核心概念。

拉取 Hello World Docker 镜像

sudo docker pull hello-world

运行 Docker Hello World 容器

sudo docker run hello-world

观察输出

运行命令后,Docker 将从 hello-world 镜像启动一个容器。输出应类似于以下内容:

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

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

初识 Docker 镜像和容器

Docker 镜像

Docker 镜像就像是一个软件的安装包或模板。它包含了运行特定应用所需的所有文件、库和配置。可以将其看作是一个应用程序的“快照”,包含了运行该应用程序的所有必要组件。 想象一下,Docker 镜像就像是一个预先安装好操作系统和应用程序的虚拟机模板。这个模板可以被复制和部署,但是本身是静态和不可变的。

Docker 容器

Docker 容器则是从镜像启动的一个实例,它是镜像的一个运行中的版本。容器是独立的、隔离的、轻量级的运行环境。它使用镜像作为基础,包含运行应用程序所需的所有内容,并且可以在主机系统上独立运行。 如果镜像是预先配置好的虚拟机模板,那么容器就是根据这个模板启动的虚拟机实例。每个容器都是一个独立的实例,可以独立运行和执行任务,并且可以随时创建、启动、停止和删除。

这里仅简单了解一下,后面的章节会用更多的例子来更好理解什么 Docker 镜像和容器,以及如何使用它们。