# Docker run流程

fc92ae64f4ce7f4e3960ed5f267ff5c39a4418aed3dadc0b.png

# 镜像是什么?

之前我们也介绍过镜像的含义,这里再来回顾一下镜像。

  • 一个分层存储的文件
  • 一个软件的环境
  • 一个镜像可以创建N个容器
  • 一种标准化的交付
  • 一个不包含Linux内核而又精简的Linux操作系统

镜像不是一个单一的文件,而是有多层构成。我们可以通过docker history <ID/NAME>查看镜像中各层内容及大小,每层对应着Dockerfile中的一条指令。Docker镜像默认存储在/var/lib/docker/中。

[root@lswwdapldn ~]# docker history mysql:8.0.27
IMAGE          CREATED         CREATED BY                                      SIZE      COMMENT
3218b38490ce   15 months ago   /bin/sh -c #(nop)  CMD ["mysqld"]               0B        
<missing>      15 months ago   /bin/sh -c #(nop)  EXPOSE 3306 33060            0B        
<missing>      15 months ago   /bin/sh -c #(nop)  ENTRYPOINT ["docker-entry…   0B        
<missing>      15 months ago   /bin/sh -c ln -s usr/local/bin/docker-entryp…   34B       
<missing>      15 months ago   /bin/sh -c #(nop) COPY file:345a22fe55d3e678…   14.5kB    
<missing>      15 months ago   /bin/sh -c #(nop) COPY dir:2e040acc386ebd23b…   1.12kB    
<missing>      15 months ago   /bin/sh -c #(nop)  VOLUME [/var/lib/mysql]      0B        
<missing>      15 months ago   /bin/sh -c {   echo mysql-community-server m…   380MB     
<missing>      15 months ago   /bin/sh -c echo 'deb http://repo.mysql.com/a…   55B       
<missing>      15 months ago   /bin/sh -c #(nop)  ENV MYSQL_VERSION=8.0.27-…   0B        
<missing>      15 months ago   /bin/sh -c #(nop)  ENV MYSQL_MAJOR=8.0          0B        
<missing>      15 months ago   /bin/sh -c set -ex;  key='A4A9406876FCBD3C45…   1.84kB    
<missing>      15 months ago   /bin/sh -c apt-get update && apt-get install…   52.2MB    
<missing>      15 months ago   /bin/sh -c mkdir /docker-entrypoint-initdb.d    0B     
......
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 它从哪里来?

Docker Hub是由Docker公司负责维护的公共注册中心,包含大量的容器镜像,Docker工具默认从这个公共镜像库下载镜像。地址:https://hub.docker.com/explore (opens new window)

配置加速器在上一篇已经讲到,可以去查看。

# 镜像常用命令

docker 执行命令格式: docker [options] command(具体命令)

  • 查看docker所有命令
[root@lswwdapldn ~]# docker --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Common Commands:
  run         Create and run a new container from an image
  exec        Execute a command in a running container
  ps          List containers
  build       Build an image from a Dockerfile
  pull        Download an image from a registry
  push        Upload an image to a registry
  images      List images
  login       Log in to a registry
  logout      Log out from a registry
  search      Search Docker Hub for images
  version     Show the Docker version information
  info        Display system-wide information
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  • 查看docker run 命令
[root@lswwdapldn ~]# docker run --help

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Create and run a new container from an image

Aliases:
  docker container run, docker run

Options:
      --add-host list                  Add a custom host-to-IP mapping (host:ip)
  -a, --attach list                    Attach to STDIN, STDOUT or STDERR
      --blkio-weight uint16            Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
      --blkio-weight-device list       Block IO weight (relative device weight) (default [])
      --cap-add list                   Add Linux capabilities
      --cap-drop list                  Drop Linux capabilities
      --cgroup-parent string           Optional parent cgroup for the container
      --cgroupns string                Cgroup namespace to use (host|private)
                                       'host':    Run the container in the Docker host's cgroup namespace
                                       'private': Run the container in its own private cgroup namespace
                                       '':        Use the cgroup namespace as configured by the
                                                  default-cgroupns-mode option on the daemon (default)
      --cidfile string                 Write the container ID to the file
      --cpu-period int                 Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int                  Limit CPU CFS (Completely Fair Scheduler) quota
      --cpu-rt-period int              Limit CPU real-time period in microseconds
      --cpu-rt-runtime int             Limit CPU real-time runtime in microseconds
......
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 1、镜像列表

我们可以使用 docker images 来列出本地主机上的镜像。

[root@lswwdapldn ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED         SIZE
nginx              latest    605c77e624dd   14 months ago   141MB
mysql              8.0.27    3218b38490ce   15 months ago   516MB
hello-world        latest    feb5d9fea6a5   17 months ago   13.3kB
1
2
3
4
5

各个选项说明:

  • **REPOSITORY:**表示镜像的仓库源
  • **TAG:**镜像的标签
  • **IMAGE ID:**镜像ID
  • **CREATED:**镜像创建时间
  • **SIZE:**镜像大小

同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本。

# 2、查询镜像

# 执行命令
docker images mysql

# 返回结果
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
mysql        8.0.27    3218b38490ce   15 months ago   516MB
1
2
3
4
5
6

我们可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/ (opens new window),这个在前面的Docker架构那一篇中有所介绍。

我们也可以使用 docker search 命令来搜索镜像。比如我们需要一个 httpd 的镜像来作为我们的 web 服务。我们可以通过 docker search 命令搜索 httpd 来寻找适合我们的镜像。

# 查询指定的镜像格式
docker search [options] <镜像名>[:TAG]

# 显示完整的镜像信息
docker search --no-trunc <镜像名>
1
2
3
4
5
[root@lswwdapldn ~]# docker search mysql
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   13945     [OK]       
mariadb                         MariaDB Server is a high performing open sou…   5314      [OK]       
percona                         Percona Server is a fork of the MySQL relati…   601       [OK]       
phpmyadmin                      phpMyAdmin - A web interface for MySQL and M…   762       [OK]       
circleci/mysql                  MySQL is a widely used, open-source relation…   29                   
bitnami/mysql                   Bitnami MySQL Docker Image                      80                   [OK]
bitnami/mysqld-exporter                                                         4                    
ubuntu/mysql                    MySQL open source fast, stable, multi-thread…   43                   
cimg/mysql                                                                      0                    
rapidfort/mysql                 RapidFort optimized, hardened image for MySQL   14                   
google/mysql                    MySQL server for Google Compute Engine          23                   [OK]
ibmcom/mysql-s390x              Docker image for mysql-s390x                    2                    
rapidfort/mysql8-ib             RapidFort optimized, hardened image for MySQ…   0                    
hashicorp/mysql-portworx-demo                                                   0                    
newrelic/mysql-plugin           New Relic Plugin for monitoring MySQL databa…   1                    [OK]
rapidfort/mysql-official        RapidFort optimized, hardened image for MySQ…   0                    
databack/mysql-backup           Back up mysql databases to... anywhere!         82                   
linuxserver/mysql               A Mysql container, brought to you by LinuxSe…   38                   
mirantis/mysql                                                                  0                    
docksal/mysql                   MySQL service images for Docksal - https://d…   0                    
vitess/mysqlctld                vitess/mysqlctld                                1                    [OK]
linuxserver/mysql-workbench                                                     48                   
bitnamicharts/mysql                                                             0                    
eclipse/mysql                   Mysql 5.7, curl, rsync                          0                    [OK]
drud/mysql                                                    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  • NAME: 镜像仓库源的名称

  • DESCRIPTION: 镜像的描述

  • OFFICIAL: 是否 docker 官方发布

  • stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。

  • AUTOMATED: 自动构建。

# 3、拉取镜像

从远程仓库下载镜像命令格式:docker pull <镜像名>[:TAG | @DIGEST]

  • TAG:版本号、标签
  • DIGEST:摘要

推荐通过「版本号」下载镜像,如果不指定版本,默认最新版 latest

[root@lswwdapldn ~]# docker pull mysql
1

# 4、删除镜像

完整的命令格式:docker image rm [options] <镜像名>[:TAG | IMAGE ID]

简写的命令格式:docker rmi [options] <镜像名>[:TAG | IMAGE ID]

我们现在来实践一下我们删除之前的hello-world镜像。

[root@lswwdapldn ~]#  docker rmi hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container c1092bda5000 is using its referenced image feb5d9fea6a5
1
2

返回结果报错了,原因有两点,要么是容器(container)曾经运行过(类似于 Windows 里运行的软件无法删除),要么存在镜像依赖。

[root@lswwdapldn ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND     CREATED     STATUS      PORTS       NAMES             
c1092bda5000   hello-world   "/hello"    5 hours ago Exited (0) 5 hours ago strange_poitras                                                        
1
2
3

显然,我们只要删除container即可:

[root@lswwdapldn ~]# docker rm c1092bda5000
c1092bda5000
1
2

然后再次查看,发现hello-world没了。

docker ps -a
1

最后再来删除镜像

[root@lswwdapldn ~]# docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
1
2
3
4
5

# 5、镜像信息

镜像是由一层一层的文件系统组成,在下载镜像的时候就发现,下载了很多文件,那么如何查看这些文件信息呢?

在 Windows 系统,如果查看文件夹的信息,右键 -> 属性 即可查看文件夹里的文件个数、创建时间等信息。镜像也可以查看自己的信息。

查看镜像信息的命令格式:docker images inspect <镜像名>[:TAG | ID]

docker images inspect <镜像名>[:TAG | ID]

# 可以简写
docker inspect <镜像名>[:TAG | ID]
1
2
3
4

例如:查看mysql的镜像信息

[root@lswwdapldn ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED         SIZE
nginx              latest    605c77e624dd   14 months ago   141MB
mysql              8.0.27    3218b38490ce   15 months ago   516MB

[root@lswwdapldn ~]# docker image inspect mysql:8.0.27
[
    {
        "Id": "sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b",
        "RepoTags": [
            "mysql:8.0.27"
	。。。。。。
	。。。。。。
	。。。。。。
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 问题

这里记录一些平时可能会遇到的问题

1、在制作Docker镜像时 REPOSITORY TAG 为 <none> <none>

[root@lswwdapldn test]# docker build -t test .    #执行此命令通过dockerfile构建镜像
[root@lswwdapldn test]# docker images 
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
<none>                             <none>              6015ffec3ed9        3 minutes ago       444 MB
1
2
3
4

就出现<none> <none>构建的镜像没有名字跟标签

[root@lswwdapldn test]# docker tag 6015ffec3ed9 test:v1.0    #手动通过image id 进行打标签
[root@lswwdapldn test]# docker images 
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
test                               v1.0                6015ffec3ed9        5 minutes ago       444 MB
1
2
3
4

2、我想删除这些名为<none>的镜像

  • 查询所有的none镜像
docker images  | grep none
1
  • 查询所有的none镜像的id
docker images  | grep none | awk '{print $3}'
1
  • 删除所有的none镜像
docker images  | grep none | awk '{print $3}' | xargs docker rmi
1

3、docker none镜像

  • 有效的 none 镜像

    • Docker文件系统的组成,docker镜像是由很多 layers组成的,每个 layer之间有父子关系,所有的docker文件系统层默认都存储在/var/lib/docker/graph目录下,docker称之为图层数据库。
    • 最后做一个总结< none>:< none> 镜像是一种中间镜像,我们可以使用docker images -a来看到,他们不会造成硬盘空间占用的问题(因为这是镜像的父层,必须存在的),但是会给我们的判断带来迷惑。
  • 无效的 none 镜像

    • 另一种类型的< none>:< none> 镜像是dangling images ,这种类型会造成磁盘空间占用问题。
    • docker的悬挂(dangling)文件系统与上面的原理类似,他是没有被使用到的并且不会关联任何镜像,因此我们需要一种机制去清理这些悬空镜像。
    • 我们在上文已经提到了有效的< none>镜像,他们是一种中间层,那无效的< none>镜像又是怎么出现的?这些 dangling镜像主要是我们触发 docker builddocker pull命令产生的。

    使用下面的命令可以清理

    docker rmi $(docker images -f "dangling=true" -q)
    
    1
最近更新: 1/8/2024, 3:36:54 PM
Copyright © - 码上言   |