commit
500325b7c8
|
@ -1,13 +1,13 @@
|
||||||
# 使用 Docker 镜像
|
# Use Docker Images
|
||||||
|
|
||||||
在之前的介绍中,我们知道镜像是 Docker 的三大组件之一。
|
As we have introduced before, `image` is one of the 3 major components of Docker.
|
||||||
|
|
||||||
Docker 运行容器前需要本地存在对应的镜像,如果本地不存在该镜像,Docker 会从镜像仓库下载该镜像。
|
Upon running docker container, it can run local image or if the image is not locally available, it will download from a registry.
|
||||||
|
|
||||||
本章将介绍更多关于镜像的内容,包括:
|
In this chapter we will introduce more about `image`, including
|
||||||
|
|
||||||
* 从仓库获取镜像;
|
* Pull images from registry
|
||||||
|
|
||||||
* 管理本地主机上的镜像;
|
* Manage local images
|
||||||
|
|
||||||
* 介绍镜像实现的基本原理。
|
* The mechanisms and implementaion of images
|
||||||
|
|
|
@ -1,19 +1,20 @@
|
||||||
## 获取镜像
|
## Pull Docker Images
|
||||||
|
|
||||||
之前提到过,[Docker Hub](https://hub.docker.com/explore/) 上有大量的高质量的镜像可以用,这里我们就说一下怎么获取这些镜像。
|
As we have mentioned, there are many high quality docker images on [Docker Hub](https://hub.docker.com/explore/). In this section, we will introduce how to `pull` these images.
|
||||||
|
|
||||||
从 Docker 镜像仓库获取镜像的命令是 `docker pull`。其命令格式为:
|
The command to fetch image from docker registry is `docker pull`. The command format is:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签]
|
docker pull [OPTIONS] [Docker Registry ADDRESS[:PORT]/]NAME[:TAG]
|
||||||
```
|
```
|
||||||
|
|
||||||
具体的选项可以通过 `docker pull --help` 命令看到,这里我们说一下镜像名称的格式。
|
More options can be found by `docker pull --help` command. Now let us see the format for image names.
|
||||||
|
|
||||||
* Docker 镜像仓库地址:地址的格式一般是 `<域名/IP>[:端口号]`。默认地址是 Docker Hub。
|
* Docker Repository Address: the address format is typically `<domain/IP>[:PORT]`. The default address is Docker Hub.
|
||||||
* 仓库名:如之前所说,这里的仓库名是两段式名称,即 `<用户名>/<软件名>`。对于 Docker Hub,如果不给出用户名,则默认为 `library`,也就是官方镜像。
|
|
||||||
|
|
||||||
比如:
|
* Repository: as mentioned before, the repository name consists of 2 parts, i.e., `username/software-name`(separated by the slash). For docker Hub, if the username is not specified, the default is `library`, where all the official images are in.
|
||||||
|
|
||||||
|
For example,
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ docker pull ubuntu:18.04
|
$ docker pull ubuntu:18.04
|
||||||
|
@ -27,17 +28,17 @@ Digest: sha256:147913621d9cdea08853f6ba9116c2e27a3ceffecf3b492983ae97c3d643fbbe
|
||||||
Status: Downloaded newer image for ubuntu:18.04
|
Status: Downloaded newer image for ubuntu:18.04
|
||||||
```
|
```
|
||||||
|
|
||||||
上面的命令中没有给出 Docker 镜像仓库地址,因此将会从 Docker Hub 获取镜像。而镜像名称是 `ubuntu:18.04`,因此将会获取官方镜像 `library/ubuntu` 仓库中标签为 `18.04` 的镜像。
|
The Docker image repository is not given, so it will pull the image from Docker Hub. Since the image name is `ubuntu:18.04`, so it will get the official image with tag `18.04` from `library/ubuntu`.
|
||||||
|
|
||||||
从下载过程中可以看到我们之前提及的分层存储的概念,镜像是由多层存储所构成。下载也是一层层的去下载,并非单一文件。下载过程中给出了每一层的 ID 的前 12 位。并且下载结束后,给出该镜像完整的 `sha256` 的摘要,以确保下载一致性。
|
From the download log, we can see the layered storage concept - images are composed of multiple layers of storage. And we download images layer by layer instead of a single file. During the download process, the first 12 hexadecimal bits of each layer are shown. And after the download, the `sha256` summary is given, to verify the integrity of downloaded files.
|
||||||
|
|
||||||
在使用上面命令的时候,你可能会发现,你所看到的层 ID 以及 `sha256` 的摘要和这里的不一样。这是因为官方镜像是一直在维护的,有任何新的 bug,或者版本更新,都会进行修复再以原来的标签发布,这样可以确保任何使用这个标签的用户可以获得更安全、更稳定的镜像。
|
When using the above command, you may find that the layer ID and `sha256` you see are different from what they are here, because the official layer is maintained and updated frequently. In case there is any new bug or new edition, the image will be rebuilt and published with the original tag. This makes sure that all the users use safer and more stable images.
|
||||||
|
|
||||||
*如果从 Docker Hub 下载镜像非常缓慢,可以参照 [镜像加速器](/install/mirror.md) 一节配置加速器。*
|
*If it is slow to download images from Docker Hub, you can refer to [Image Accelerators](/install/mirror.md) to configure accelerator.*
|
||||||
|
|
||||||
### 运行
|
### Run
|
||||||
|
With the image, we can run a container based on the image. Taking the above `ubuntu:18.04` as an example, if we want to start the `bash` inside it for interactive operations, we can execute the following commands.
|
||||||
|
|
||||||
有了镜像后,我们就能够以这个镜像为基础启动并运行一个容器。以上面的 `ubuntu:18.04` 为例,如果我们打算启动里面的 `bash` 并且进行交互式操作的话,可以执行下面的命令。
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ docker run -it --rm \
|
$ docker run -it --rm \
|
||||||
|
@ -59,13 +60,16 @@ VERSION_CODENAME=bionic
|
||||||
UBUNTU_CODENAME=bionic
|
UBUNTU_CODENAME=bionic
|
||||||
```
|
```
|
||||||
|
|
||||||
`docker run` 就是运行容器的命令,具体格式我们会在 [容器](../container) 一节进行详细讲解,我们这里简要的说明一下上面用到的参数。
|
`docker run` is the command for running the container, the detailed format will be explained in the [container](../container) chapter. Here we only illustrate the parameters used above.
|
||||||
|
|
||||||
* `-it`:这是两个参数,一个是 `-i`:交互式操作,一个是 `-t` 终端。我们这里打算进入 `bash` 执行一些命令并查看返回结果,因此我们需要交互式终端。
|
* `-it`: There are 2 parameters here, the first is `-i`, for interactive operations, another is `-t`, which is for terminal. What we intend to do is to enter the `bash` terminal of docker, then execute some commands and see the output. That's why we need the interactive terminal.
|
||||||
* `--rm`:这个参数是说容器退出后随之将其删除。默认情况下,为了排障需求,退出的容器并不会立即删除,除非手动 `docker rm`。我们这里只是随便执行个命令,看看结果,不需要排障和保留结果,因此使用 `--rm` 可以避免浪费空间。
|
|
||||||
* `ubuntu:18.04`:这是指用 `ubuntu:18.04` 镜像为基础来启动容器。
|
|
||||||
* `bash`:放在镜像名后的是 **命令**,这里我们希望有个交互式 Shell,因此用的是 `bash`。
|
|
||||||
|
|
||||||
进入容器后,我们可以在 Shell 下操作,执行任何所需的命令。这里,我们执行了 `cat /etc/os-release`,这是 Linux 常用的查看当前系统版本的命令,从返回的结果可以看到容器内是 `Ubuntu 18.04.1 LTS` 系统。
|
* `--rm`: Remove the docker after stop it. In default, for troubleshooting, the docker is not removed immediately after quitting, unless manually remove it using `docker rm`. But in our case, we only test the commands and to see the resutls, we don't care much about the results, so we use `--rm` to avoid wasting space.
|
||||||
|
|
||||||
最后我们通过 `exit` 退出了这个容器。
|
* `ubuntu:18.04`: use `ubuntu:18:04` as the base image to start the container.
|
||||||
|
|
||||||
|
* `bash`: What we have after the image name is **command**, since we want an interactive shell, so we use `bash` as the command here.
|
||||||
|
|
||||||
|
After entering the comainer, we can execute any command we want. Here, we executed `cat etc/os-release`, which is the commonly-used command to view the version of the current OS. We can see from the result that the container is based on `Ubuntu 18.04.1 LTS`.
|
||||||
|
|
||||||
|
In the end, we quit the container with `exit`.
|
||||||
|
|
Loading…
Reference in New Issue