Docker入门-docker镜像常用命令

Docker镜像常用命令

本篇主要内容:

  • docker的常用命令介绍
    • docker pull
    • docker images
    • docker tag
    • docker inspect
    • docker history
    • docker search
    • docker rmi
    • docker image rm
    • docker image prune
    • docker commit
    • docker import
    • docker build
    • docker save
    • docker load
    • docker push

介绍-查看日志

如果服务工作不正常,可以通过查看Docker服务的日志信息来确定问题,例如在RedHat系统上日志文件可能为/var/log/messages,在Ubuntu或CentOS系统上可以执行命令journalctl -u docker.service

介绍-获取镜像

可以使用docker [image] pull命令直接从DockerHub镜像源来下载镜像。该命令的格式为docker[image] pull NAME[:TAG]

docker pull

其中,NAME是镜像仓库名称(用来区分镜像),TAG是镜像的标签(往往用来表示版本信息)。通常情况下,描述一个镜像需要包括“名称+标签”信息。

下载过程中可以看出,镜像文件一般由若干层(layer)组成,6c953ac5d795这样的串是层的唯一id(实际上完整的id包括256比特,64个十六进制字符组成)。使用docker pull命令下载中会获取并输出镜像的各层信息。当不同的镜像包括相同的层时,本地仅存储了层的一份内容,减小了存储空间。

严格地讲,镜像的仓库名称中还应该添加仓库地址(即registry,注册服务器)作为前缀,只是默认使用的是官方Docker Hub服务,该前缀可以忽略。例如,docker pull ubuntu:18.04命令相当于docker pullregistry.hub.docker.com/ubuntu:18.04命令,即从默认的注册服务器Docker Hub Registry中的ubuntu仓库来下载标记为18.04的镜像。如果从非官方的仓库下载,则需要在仓库名称前指定完整的仓库地址。例如从网易蜂巢的镜像源来下载ubuntu:18.04镜像,可以使用如下命令,此时下载的镜像名称为hub.c.163.com/public/ubuntu:18.04:

docker pull hub.c.163.com/public/ubuntu:18.04

介绍-查看镜像

docker images

使用docker images或docker image ls命令可以列出本地主机上已有镜像的基本信息。

  • docker images
  • docker image ls
[root@localhost ~]# docker images
REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
registry.cn-shanghai.aliyuncs.com/c7n/frontbase   0.9.0               67bbc00289a6        3 months ago        166 MB
docker.io/minio/minio                             latest              7fb94ffde8f0        7 months ago        62.8 MB
docker.io/rabbitmq                                3-management        64a1f920fb0d        7 months ago        187 MB
docker.io/redis                                   latest              1319b1eaa0b7        7 months ago        104 MB
docker.io/nginx                                   latest              08393e824c32        7 months ago        132 MB
docker.io/mysql                                   5.7                 718a6da099d8        7 months ago        448 MB
docker.io/foxiswho/rocketmq                       server              16680398c098        18 months ago       489 MB
[root@localhost ~]# docker image ls
REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
registry.cn-shanghai.aliyuncs.com/c7n/frontbase   0.9.0               67bbc00289a6        3 months ago        166 MB
docker.io/minio/minio                             latest              7fb94ffde8f0        7 months ago        62.8 MB
docker.io/rabbitmq                                3-management        64a1f920fb0d        7 months ago        187 MB
docker.io/redis                                   latest              1319b1eaa0b7        7 months ago        104 MB
docker.io/nginx                                   latest              08393e824c32        7 months ago        132 MB
docker.io/mysql                                   5.7                 718a6da099d8        7 months ago        448 MB
docker.io/foxiswho/rocketmq                       server              16680398c098        18 months ago       489 MB
[root@localhost ~]# 

镜像大小信息只是表示了该镜像的逻辑体积大小,实际上由于相同的镜像层本地只会存储一份,物理上占用的存储空间会小于各镜像逻辑体积之和。

images子命令主要支持如下选项,用户可以自行进行尝试:

  • -a, --all=true|false:列出所有(包括临时文件)镜像文件,默认为否;
  • –digests=true|false:列出镜像的数字摘要值,默认为否;
  • -f, --filter=[]:过滤列出的镜像,如dangling=true只显示没有被使用的镜像;也可指定带有特定标注的镜像等;
  • –format=“TEMPLATE”:控制输出格式,如.ID代表ID信息,.Repository代表仓库信息等;
  • –no-trunc=true|false:对输出结果中太长的部分是否进行截断,如镜像的ID信息,默认为是;
  • -q, --quiet=true|false:仅输出ID信息,默认为否。

更多子命令选项还可以通过man docker-images来查看。

docker tag

使用tag命令添加镜像标签,docker tag命令来为本地镜像任意添加新的标签。

[root@localhost ~]# docker images
REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
registry.cn-shanghai.aliyuncs.com/c7n/frontbase   0.9.0               67bbc00289a6        3 months ago        166 MB
docker.io/minio/minio                             latest              7fb94ffde8f0        7 months ago        62.8 MB
docker.io/rabbitmq                                3-management        64a1f920fb0d        7 months ago        187 MB
docker.io/redis                                   latest              1319b1eaa0b7        7 months ago        104 MB
docker.io/nginx                                   latest              08393e824c32        7 months ago        132 MB
docker.io/mysql                                   5.7                 718a6da099d8        7 months ago        448 MB
docker.io/foxiswho/rocketmq                       server              16680398c098        18 months ago       489 MB
[root@localhost ~]# docker tag docker.io/mysql:5.7 mymysql:5.7
[root@localhost ~]# docker images
REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
registry.cn-shanghai.aliyuncs.com/c7n/frontbase   0.9.0               67bbc00289a6        3 months ago        166 MB
docker.io/minio/minio                             latest              7fb94ffde8f0        7 months ago        62.8 MB
docker.io/rabbitmq                                3-management        64a1f920fb0d        7 months ago        187 MB
docker.io/redis                                   latest              1319b1eaa0b7        7 months ago        104 MB
docker.io/nginx                                   latest              08393e824c32        7 months ago        132 MB
docker.io/mysql                                   5.7                 718a6da099d8        7 months ago        448 MB
mymysql                                           5.7                 718a6da099d8        7 months ago        448 MB
docker.io/foxiswho/rocketmq                       server              16680398c098        18 months ago       489 MB
[root@localhost ~]# 

docker inspect

使用docker[image]inspect命令可以获取该镜像的详细信息,包括制作者、适应架构、各层的数字摘要等:

[root@localhost ~]# docker inspect mymysql:5.7
[
    {
    
    
        "Id": "sha256:718a6da099d82183c064a964523c0deca80619cb033aadd15854771fe592a480",
        "RepoTags": [
            "docker.io/mysql:5.7",
            "mymysql:5.7"
        ],
        "RepoDigests": [
            "docker.io/mysql@sha256:da58f943b94721d46e87d5de208dc07302a8b13e638cd1d24285d222376d6d84"
        ],
       .......
            ]
        }
    }
]
[root@localhost ~]# 

上面命令返回的内容是JSON格式的消息,如果只需要其中一项信息,可以使用- f进行过滤:

[root@localhost ~]# docker inspect -f {
    
    {".DockerVersion"}} mymysql:5.7
18.09.7
[root@localhost ~]# 

docker history

获取镜像每一层的信息

[root@localhost ~]# docker history mymysql:5.7
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
718a6da099d8        7 months ago        /bin/sh -c #(nop)  CMD ["mysqld"]               0 B                 
<missing>           7 months ago        /bin/sh -c #(nop)  EXPOSE 3306 33060            0 B               

docker search

使用docker search命令可以搜索Docker Hub官方仓库中的镜像。语法为docker search [option]keyword。支持的命令选项主要包括:

  • -f, --filter filter:过滤输出内容;
  • –format string:格式化输出内容;
  • –limit int:限制输出结果个数,默认为25个;
  • –no-trunc:不截断输出结果。
[root@localhost ~]# docker search --filter=is-official=true nginx
INDEX       NAME              DESCRIPTION                STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/nginx   Official build of Nginx.   14591     [OK]       
[root@localhost ~]# docker search --filter=stars=4 nginx
INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/nginx                              Official build of Nginx.                        14591     [OK]       
docker.io   docker.io/jwilder/nginx-proxy                Automated Nginx reverse proxy for docker c...   1985                 [OK]
docker.io   docker.io/richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable ...   810                  [OK]
[root@localhost ~]# 

docker rmi

使用docker rmi或docker image rm命令可以删除镜像,命令格式为docker rmi IMAGE[IMAGE…],其中IMAGE可以为标签或ID。

  • docker rmi
  • docker images rm

支持选项包括:

  • -f, -force:强制删除镜像,即使有容器依赖它;
  • -no-prune:不要清理未带标签的父镜像

docker image prune

使用Docker一段时间后,系统中可能会遗留一些临时的镜像文件,以及一些没有被使用的镜像,可以通过docker image prune命令来进行清理。

支持选项包括:

  • -a, -all:删除所有无用镜像,不光是临时镜像;
  • -filter filter:只清理符合给定过滤器的镜像;
  • -f, -force:强制删除镜像,而不进行提示确认。

介绍-创建镜像

创建镜像的方法主要有三种:

  1. 基于已有镜像的容器创建
  2. 基于本地模板导入
  3. 基于Dockerfile创建。

docker commit

docker [container] commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]命令用于基于已有容器创建。

主要选项包括:

  • -a, --author="":作者信息;
  • -c, --change=[]:提交的时候执行Dockerfile指令,包括CMD|ENTRYPOINT|ENV|EXPOSE|LABEL|ONBUILD|USER|VOLUME|WORKDIR等;
  • -m, --message="":提交消息;
  • -p, --pause=true:提交时暂停容器运行。

执行下面的命令时,发现如果没有指定repository和tag,生成的镜像是none的。

[root@localhost ~]# docker run -it docker.io/nginx /bin/bash
root@b753d1d56bf2:/# ll
bash: ll: command not found
root@b753d1d56bf2:/# ls
bin   docker-entrypoint.d   home   media  proc	sbin  tmp
boot  docker-entrypoint.sh  lib    mnt	  root	srv   usr
dev   etc		    lib64  opt	  run	sys   var
root@b753d1d56bf2:/# echo "hello world">lth.txt
root@b753d1d56bf2:/# ls *.txt
lth.txt
root@b753d1d56bf2:/# exit
[root@localhost ~]# docker commit -m "create lth.txt" -a "lth" b753d1d56bf2
sha256:cafa72eb8775794eb612ac13c98642e81eb27c3dfe89184419c71aec4d7b3e50
[root@localhost ~]# docker commit -m "create lth.txt" -a "lth" b753d1d56bf2 test:0.1
[root@localhost ~]# docker images -a
REPOSITORY                                        TAG                 IMAGE ID            CREATED              SIZE
test                                              0.1                 4fc2a9276ef3        13 seconds ago       132 MB
<none>                                            <none>              cafa72eb8775        About a minute ago   132 MB
registry.cn-shanghai.aliyuncs.com/c7n/frontbase   0.9.0               67bbc00289a6        3 months ago         166 MB
docker.io/minio/minio                             latest              7fb94ffde8f0        7 months ago         62.8 MB
docker.io/rabbitmq                                3-management        64a1f920fb0d        7 months ago         187 MB
docker.io/redis                                   latest              1319b1eaa0b7        7 months ago         104 MB
docker.io/nginx                                   latest              08393e824c32        7 months ago         132 MB
docker.io/mysql                                   5.7                 718a6da099d8        7 months ago         448 MB
docker.io/foxiswho/rocketmq                       server              16680398c098        18 months ago        489 MB
[root@localhost ~]# 

docker import

docker [image] import[OPTIONS] file|URL|-[REPOSITORY [:TAG]]该命令基于本地模板导入。用户也可以直接从一个操作系统模板文件导入一个镜像。

docker build

基于Dockerfile创建是最常见的方式。Dockerfile是一个文本文件,利用给定的指令描述基于某个父镜像创建新镜像的过程。

使用docker build命令出现了下面的报错信息,是因为语句缺失了一个路径参数

[root@localhost docker]# docker build -t halm-front:1.0
"docker build" requires exactly 1 argument(s).

下面是我用来打前端镜像的脚本及命令

[root@localhost docker]# ll
总用量 37872
-rw-r--r-- 1 root root      946 3月  19 23:31 build.sh
drwxr-xr-x 7 root root     4096 3月  19 23:25 dist
-rw-r--r-- 1 root root 38766799 3月  19 23:07 dist.zip
-rw-r--r-- 1 root root      483 3月  19 23:06 Dockerfile
[root@localhost docker]# 
[root@localhost docker]# cat build.sh 
#!/bin/bash
#***********************************************
#Author:        luotianhao
#Mail:          [email protected]
#Version:       1.0
#Date:          2021-03-19
#FileName:      build.sh
#Description:   This is a test script.
#***********************************************

CURR_PATH=$PWD

DIST_PATH="$CURR_PATH/dist"

[ ! -d $DIST_PATH ] && {
    
    
  mkdir dist
} || {
    
    
  rm -rf dist
  mkdir dist
}

function doBuild(){
    
    
[ ! -e "dist.zip" ] && {
    
    
  echo "没有dist.zip文件"
  exit 1;
} || {
    
    
  unzip dist.zip -d dist/
  read -p "ple input your service name:" SERVICE_NAME
  doValidateNull $SERVICE_NAME
  read -p "ple input your tag:" TAG
  doValidateNull $TAG
  MY_REPOSITORY="$SERVICE_NAME:$TAG"
  docker build -t $MY_REPOSITORY .
}
}

doValidateNull(){
    
    
  [ 1 -ne $# ] &&{
    
    
    echo "only validate null to one arg"
    exit 1;
  } || {
    
    
    [ -z $1 ] && {
    
    
      echo "your input is null,ple retry"
      exit 1;
    } || {
    
    
      return 0;
    }
  }
}

doBuild
[root@localhost docker]# pwd
/u01/learn/docker
[root@localhost docker]# sh build.sh 
.....
Removing intermediate container 3f06f3408a73
Successfully built 2e683e4090f8
[root@localhost docker]# docker images
REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
halm-front                                        1.0                 2e683e4090f8        4 seconds ago       289 MB

介绍-存出和载入镜像

用户可以使用docker [image] save和docker[image] load命令来存出和载入镜像。

docker save

如果要导出镜像到本地文件,可以使用docker[image] save命令。该命令支持-o、-outputstring参数,导出镜像到指定的文件中

[root@localhost docker]# docker save -o front.tar registry.cn-shanghai.aliyuncs.com/c7n/frontbase:0.9.0
[root@localhost docker]# ls front.*
front.tar
[root@localhost docker]# 

docker load

可以使用docker [image] load将导出的tar文件再导入到本地镜像库。支持-i、-input string选项,从指定文件中读入镜像内容。

[root@localhost docker]# docker load -i front.tar

介绍-上传镜像

可以使用docker [image] push命令上传镜像到仓库,默认上传到Docker Hub官方仓库(需要登录)。命令格式为docker [image] push NAME[:TAG] |[REGISTRY_HOST[:REGISTRY_PORT]/]NAME[:TAG]

后面可以自己搭建一个harbor仓库来存放镜像

  1. docker login 10.185.16.62
  2. docker push 10.185.16.62/ylj/javabase:0.8.0

猜你喜欢

转载自blog.csdn.net/weixin_43169156/article/details/115018490