【云原生】一看就会的Docker镜像管理

请添加图片描述

Docker image镜像管理

​ docker images 是docker的三大组件之一。 docker把下载的 images 存储到docker主机上,如果一个 image 不 在主机上,docker会从一个镜像仓库下载,默认的仓库是 DOCKER HUB 公共仓库。 接下来将介绍更多关于 docker images 的内容,包括: • 使用和管理本地主机上的 images • 创建一个基础的 images • 上传 images 到 docker hub (公共images仓库) • 列出本地主机上已经存在的 images

1.显示出当前本机上的images

[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   21 months ago   13.3kB

信息:
repository:来自于那个仓库
tag:镜像的版本 latest为最新版
image id:镜像ID
size:镜像大小

选项:
-a:显示所有的镜像
-f:根据指定格式输出
-q:只显示出镜像的ID

2.获取下载一个镜像

​ 当我们需要使用这个镜像时,就可以直接启动了,无需来进行下载,在进行创建启动

[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   21 months ago   13.3kB
centos        latest    5d0da3dc9764   21 months ago   231MB

3.查找images

​ docker的一个特点是很多人因为各种不同的用途创建了各种不同的images,它们都被上传到了docker hub共有仓库上,我们们可以在docker hub的网站上来查找它们。使用docker search命令。

[root@localhost ~]# docker search mysql
NAME                            DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   14242     [OK]
mariadb                         MariaDB Server is a high performing open sou…   5443      [OK]
percona                         Percona Server is a fork of the MySQL relati…   615       [OK]
phpmyadmin                      phpMyAdmin - A web interface for MySQL and M…   825       [OK]
bitnami/mysql                   Bitnami MySQL Docker Image                       90                   [OK]
circleci/mysql                  MySQL is a widely used, open-source relation…   29
bitnami/mysqld-exporter                                                          5
ubuntu/mysql                    MySQL open source fast, stable, multi-thread…   51

信息
name:镜像的名字
description:镜像的描述信息
stars:星数(该image的欢迎程度)
official:是否是官方创建

4.查看镜像的详细信息

​ 查看信息,完整的ID号, repotags当前镜像的版本,镜像的大小等信息信息,或者使用docker images --no-trunc

[root@localhost ~]# docker inspect centos
[
    {
    
    
        "Id": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "RepoTags": [
            "centos:latest"
        ],
        "RepoDigests": [
            "centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-09-15T18:20:05.184694267Z",
        "Container": "9bf8a9e2ddff4c0d76a587c40239679f29c863a967f23abf7a5babb6c2121bf1",
        "ContainerConfig": {
    
    
            "Hostname": "9bf8a9e2ddff",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/bash\"]"
            ],
            "Image": "sha256:f5b050f177fd426be8fe998a8ecf3fb1858d7e26dff4080b29a327d1bd5ba422",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
    
    
                "org.label-schema.build-date": "20210915",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "DockerVersion": "20.10.7",
        "Author": "",
        "Config": {
    
    
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "sha256:f5b050f177fd426be8fe998a8ecf3fb1858d7e26dff4080b29a327d1bd5ba422",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
    
    
                "org.label-schema.build-date": "20210915",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 231219825,
        "VirtualSize": 231219825,
        "GraphDriver": {
    
    
            "Data": {
    
    
                "MergedDir": "/var/lib/docker/overlay2/892d8942ea7d54cab5feb69ad45adfd509b48e8fc0684adc7b0b0df8ed54cfb2/merged",
                "UpperDir": "/var/lib/docker/overlay2/892d8942ea7d54cab5feb69ad45adfd509b48e8fc0684adc7b0b0df8ed54cfb2/diff",
                "WorkDir": "/var/lib/docker/overlay2/892d8942ea7d54cab5feb69ad45adfd509b48e8fc0684adc7b0b0df8ed54cfb2/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
    
    
            "Type": "layers",
            "Layers": [
                "sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59"
            ]
        },
        "Metadata": {
    
    
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

或者
[root@localhost ~]# docker images --no-trunc
REPOSITORY    TAG       IMAGE ID                                                                  CREATED         SIZE
hello-world   latest    sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412   21 months ago   13.3kB
centos        latest    sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6   21 months ago   231MB

5.创建我们自己的image

​ 有的时候别人的镜像不一定适合我们,这个时候就需要我们自定义的来创建镜像,来进行创建使用

1.创建一个容器,安装一个httpd服务

[root@localhost ~]# docker run -it centos /bin/bash
[root@cda2197c7f3b /]# yum -y install nginx

2.退出将当前容器做为镜像

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                            PORTS     NAMES
cda2197c7f3b   centos    "/bin/bash"   13 minutes ago   Exited (130) About a minute ago             nice_williams
74ad543dbd55   centos    "/bin/bash"   13 minutes ago   Exited (0) 13 minutes ago                   sharp_cohen
[root@localhost ~]# docker commit -m "add httpd" -a 'test' cda2197c7f3b centos8:httpd
sha256:8ccee778a96a46b0a815f24844df5a6fd5eb7c164a11f9cce2077b1a6bf647bf
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
centos8       httpd     8ccee778a96a   7 seconds ago   280MB
hello-world   latest    feb5d9fea6a5   21 months ago   13.3kB
centos        latest    5d0da3dc9764   21 months ago   231MB

3.使用当前创建的镜像进行创建容器

[root@localhost ~]# docker run -it centos8:httpd
[root@7b703e1a038a /]# rpm -q httpd
httpd-2.4.37-43.module_el8.5.0+1022+b541f3b1.x86_64

4.创建镜像命令

[root@localhost ~]# docker commit --help

Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

Aliases:
  docker container commit, docker commit

Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <[email protected]>")
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)

格式:
docker commit [选项] 容器名称或者ID [镜像名字:[标签]]
选项:
-a:作者,联系方式
-c:创建的镜像应用dockerfile指令
-m:描述信息
-p:在创建镜像时,自动停止容器,默认为true

6.上传到个人仓库docker hub中

1.将要打包的镜像进行更改为仓库的用户名和仓库名称

[root@localhost ~]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED             SIZE
conquerbug/test   v1        8ccee778a96a   About an hour ago   280MB
centos8           httpd     8ccee778a96a   About an hour ago   280MB
hello-world       latest    feb5d9fea6a5   21 months ago       13.3kB
centos            latest    5d0da3dc9764   21 months ago       231MB

[root@localhost ~]# docker tag 8ccee778a96a conquerbug/test:v1

2.进行登录docker hub账号并上传

[root@localhost ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: conquerbug
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

[root@localhost ~]# docker push conquerbug/test:v1
The push refers to repository [docker.io/conquerbug/test]
6ef790a7eff0: Pushing [==================================================>]  48.89MB
74ddd0ec08fa: Pushing [==================================================>]  238.6MB

7.存出镜像和载入镜像

1.将两个镜像进行打包

[root@localhost ~]# docker save -o centos-hello.tar hello-world centos

2.删除这两个镜像

[root@localhost ~]# docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
[root@localhost ~]# docker rmi centos
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

3.重新载入镜像

[root@localhost ~]# docker load --input centos-hello.tar
e07ee1baac5f: Loading layer [==================================================>]  14.85kB/14.85kB
Loaded image: hello-world:latest
74ddd0ec08fa: Loading layer [==================================================>]  238.6MB/238.6MB
Loaded image: centos:latest
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   21 months ago   13.3kB
centos        latest    5d0da3dc9764   21 months ago   231MB

8.删除镜像和容器

1.删除镜像

[root@localhost ~]# docker rmi hello-world
Untagged: hello-world:latest
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a

2.删除容器

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE           COMMAND       CREATED       STATUS                     PORTS     NAMES
7b703e1a038a   centos8:httpd   "/bin/bash"   2 hours ago   Exited (0) 2 hours ago               mystifying_pare
cda2197c7f3b   centos          "/bin/bash"   2 hours ago   Exited (130) 2 hours ago             nice_williams
74ad543dbd55   centos          "/bin/bash"   2 hours ago   Exited (0) 2 hours ago               sharp_cohen
[root@localhost ~]# docker rm -f 7b703e1a038a cda2197c7f3b 74ad543dbd55
7b703e1a038a
cda2197c7f3b
74ad543dbd55
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

三,Docker Container容器管理

1.创建容器

1.前台运行创建

​ 注意:下面是创建容器并进入容器,当exit退出容器后,容器就不在运行,需要使用-d选项,后台运行

[root@localhost ~]# docker run -i -t centos:7 /bin/bash
[root@4af91bbbc860 /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 03:19 pts/0    00:00:00 /bin/bash
root         15      1  0 03:19 pts/0    00:00:00 ps -ef
[root@4af91bbbc860 /]# exit
exit
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED         STATUS                     PORTS     NAMES
4af91bbbc860   centos:7   "/bin/bash"   7 minutes ago   Exited (0) 8 seconds ago             naughty_meitner

选项:
-name:指定容器名称,默认是随机名字
-i:创建一个交互式容器
-t:为容器分配一个伪终端,绑定到容器的标准输入上
-d:后台运行容器

2.后台运行创建

[root@localhost ~]# docker run -itd --name centos7-1 centos:7 /bin/bash
08b6e56e54f2d645601cd71020a922c7b5f916725401b7408e358a782a965284
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED         STATUS                          PORTS     NAMES
08b6e56e54f2   centos:7   "/bin/bash"   2 seconds ago   Up 2 seconds                              centos7-1
4af91bbbc860   centos:7   "/bin/bash"   8 minutes ago   Exited (0) About a minute ago       naughty_meitner

选项:
-name:指定容器名称,默认是随机名字
-i:创建一个交互式容器
-t:为容器分配一个伪终端,绑定到容器的标准输入上
-d:后台运行容器

2.查看容器

[root@localhost ~]# docker ps --help

Usage:  docker ps [OPTIONS]

List containers

Aliases:
  docker container ls, docker container list, docker container ps, docker ps

Options:
  -a, --all             Show all containers (default shows just running)	#查看所有容器 默认之查看在运行的容器
  -f, --filter filter   Filter output based on conditions provided
      --format string   Format output using a custom template:
                        'table':            Print output in table format
                        with column headers (default)
                        'table TEMPLATE':   Print output in table format
                        using the given Go template
                        'json':             Print in JSON format
                        'TEMPLATE':         Print output using the given Go
                        template.
                        Refer to https://docs.docker.com/go/formatting/ for
                        more information about formatting output with templates
  -n, --last int        Show n last created containers (includes all			#查看多少行容器
                        states) (default -1)
  -l, --latest          Show the latest created container (includes all states)		#查看最近创建的容器
      --no-trunc        Don't truncate output		#显示完整ID号
  -q, --quiet           Only display container IDs		#只显示ID号
  -s, --size            Display total file sizes	#显示容器大小
1.查看所有的容器
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                      PORTS     NAMES
08b6e56e54f2   centos:7   "/bin/bash"   27 minutes ago   Up 27 minutes                         centos7-1
4af91bbbc860   centos:7   "/bin/bash"   35 minutes ago   Exited (0) 28 minutes ago             naughty_meitner
2.查看容器的ID

​ 注:可以用于删除容器时,过滤

[root@localhost ~]# docker ps -q
08b6e56e54f2

3.管理容器

1.开启容器

[root@localhost ~]# docker start 4af91bbbc860
4af91bbbc860
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS          PORTS     NAMES
08b6e56e54f2   centos:7   "/bin/bash"   30 minutes ago   Up 30 minutes             centos7-1
4af91bbbc860   centos:7   "/bin/bash"   38 minutes ago   Up 1 second               naughty_meitner

2.关闭容器

[root@localhost ~]# docker stop 08b6e56e54f2
08b6e56e54f2
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                        PORTS     NAMES
08b6e56e54f2   centos:7   "/bin/bash"   30 minutes ago   Exited (137) 10 seconds ago             centos7-1
4af91bbbc860   centos:7   "/bin/bash"   39 minutes ago   Up 53 seconds                           naughty_meitner

3.强制关闭容器

[root@localhost ~]# docker kill 4af91bbbc860
4af91bbbc860
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                        PORTS     NAMES
08b6e56e54f2   centos:7   "/bin/bash"   31 minutes ago   Exited (137) 51 seconds ago             centos7-1
4af91bbbc860   centos:7   "/bin/bash"   39 minutes ago   Exited (137) 1 second ago               naughty_meitner

4.重启容器

[root@localhost ~]# docker restart 4af91bbbc860
4af91bbbc860
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                            PORTS     NAMES
08b6e56e54f2   centos:7   "/bin/bash"   32 minutes ago   Exited (137) About a minute ago             centos7-1
4af91bbbc860   centos:7   "/bin/bash"   40 minutes ago   Up 7 seconds                                naughty_meitner

4.查看容器信息

1.查看所有信息

[root@localhost ~]# docker inspect 4af91bbbc860

2.查看容器IP

[root@localhost ~]# docker inspect -f '{
    
    {.NetworkSettings.IPAddress}}' 4af91bbbc860
172.17.0.2

3.查看PID

[root@localhost ~]# docker inspect -f '{
    
    {.State.Pid}}' 4af91bbbc860
72633

5.进入容器

注意:进入当前的伪终端,如果exit退出之后,当前的容器也会随之关闭,因为当前只存在一个终端。

1.进入当前的伪终端中

[root@localhost ~]# docker attach 4af91bbbc860
[root@4af91bbbc860 /]# exit
exit
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                             PORTS     NAMES
08b6e56e54f2   centos:7   "/bin/bash"   42 minutes ago   Exited (137) 11 minutes ag     o             centos7-1
4af91bbbc860   centos:7   "/bin/bash"   50 minutes ago   Exited (0) 5 seconds ago                     naughty_meitner

2.创建一个新的伪终端并进入

[root@localhost ~]# docker exec -it 4af91bbbc860 /bin/bash
[root@4af91bbbc860 /]# exit
exit
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                        PORTS     NAMES
08b6e56e54f2   centos:7   "/bin/bash"   43 minutes ago   Exited (137) 13 minutes ago             centos7-1
4af91bbbc860   centos:7   "/bin/bash"   51 minutes ago   Up 28 seconds                           naughty_meitner

6.导出\导入容器

1.导出容器

​ 将当前容器导出到本地

[root@localhost ~]# docker export 4af91bbbc860 > centos7.tar
[root@localhost ~]# ls
anaconda-ks.cfg  centos7.tar  original-ks.cfg

2.导入容器

​ 将当前容器导入为竟镜像,然后再基于这个镜像创建容器

[root@localhost ~]# cat centos7.tar | docker import - centos7:test
sha256:d0373e6a70dc1413fa992d11c40b9260fe742db22196429835d33b9cc3eb2135
[root@localhost ~]# docker images
REPOSITORY                                          TAG       IMAGE ID       CREATED          SIZE
centos7                                             test      d0373e6a70dc   11 seconds ago   204MB

7.删除容器

1.删除单个容器

[root@localhost ~]# docker rm -f 4af91bbbc860

选项:
-f:强制删除

2.删除多个容器

[root@localhost ~]# docker rm -f 4af91bbbc860  08b6e56e54f2

3.删除全部容器

[root@localhost ~]# docker rm -f  $(docker ps -aq)
2.导入容器

​ 将当前容器导入为竟镜像,然后再基于这个镜像创建容器

[root@localhost ~]# cat centos7.tar | docker import - centos7:test
sha256:d0373e6a70dc1413fa992d11c40b9260fe742db22196429835d33b9cc3eb2135
[root@localhost ~]# docker images
REPOSITORY                                          TAG       IMAGE ID       CREATED          SIZE
centos7                                             test      d0373e6a70dc   11 seconds ago   204MB

7.删除容器

1.删除单个容器

[root@localhost ~]# docker rm -f 4af91bbbc860

选项:
-f:强制删除

2.删除多个容器

[root@localhost ~]# docker rm -f 4af91bbbc860  08b6e56e54f2

3.删除全部容器

[root@localhost ~]# docker rm -f  $(docker ps -aq)

猜你喜欢

转载自blog.csdn.net/weixin_53678904/article/details/131345289