docker镜像拉取---docker pull
docker pull image_name[:tag]
这是直接拉取官方镜像
image_name
: 镜像的名称,例如ubuntu
,nginx
,mysql
等。tag
: 镜像的标签,表示版本或者特定的标识。如果未指定标签,默认为latest
。
例如,如果您想拉取官方的 centos 镜像,可以运行:
docker pull centos
如果您想拉取 MySQL 5.7 版本的官方镜像,可以运行:
docker pull mysql:5.7
在这里,mysql
是镜像名称,5.7
是标签。如果不指定标签,默认会拉取 latest
版本。
请确保您的计算机上已经安装 Docker,并且 Docker 服务正在运行。运行 docker pull
命令后,Docker 将自动从 Docker Hub 或者您配置的其他镜像仓库拉取相应的镜像。
docker基本操作之镜像操作
1.查看镜像——docker images
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 23 months ago 141MB
mysql latest 3218b38490ce 23 months ago 516MB
centos latest 5d0da3dc9764 2 years ago 231MB
REPOSITORY:镜像属于的仓库;
TAG:镜像的标签信息,标记同一个仓库中的不同镜像;
IMAGE ID:镜像的唯一ID 号,唯一标识一个镜像,经过md5方式加密过;
CREATED:镜像创建时间;
VIRTUAL SIZE:镜像大小;
2.查看镜像只显示id---docker images -q
[root@docker ~]# docker images -q
605c77e624dd
3218b38490ce
5d0da3dc9764
3.查看镜像的详细信息----docker image inspect iname
[root@docker ~]# docker image inspect nginx
[
{
"Id": "sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85",
"RepoTags": [
"nginx:latest"
],
"RepoDigests": [
"nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31"
],
"Parent": "",
"Comment": "",
"Created": "2021-12-29T19:28:29.892199479Z",
"Container": "ca3e48389f7160bc9d9a892d316fcbba459344ee3679998739b1c3cd8e56f7da",
"ContainerConfig": {
"Hostname": "ca3e48389f71",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.21.5",
"NJS_VERSION=0.7.1",
"PKG_RELEASE=1~bullseye"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"nginx\" \"-g\" \"daemon off;\"]"
],
"Image": "sha256:82941edee2f4d17c55563bb926387c3ae39fa1a99777f088bc9d3db885192209",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"DockerVersion": "20.10.7",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.21.5",
"NJS_VERSION=0.7.1",
"PKG_RELEASE=1~bullseye"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "sha256:82941edee2f4d17c55563bb926387c3ae39fa1a99777f088bc9d3db885192209",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGQUIT"
},
"Architecture": "amd64",
"Os": "linux",
"Size": 141479488,
"VirtualSize": 141479488,
"GraphDriver": {
"Data": {
"LowerDir": "/docker-data/overlay2/cb2fec57ae9bdeb8f1cf0ba4ace1993d80d0f7b216813b1f7cfc7001afdded1b/diff:/docker-data/overlay2/3d95f0d279270ce97f6f9561aff76c8fda2dd6cc43c9ab48666b4eb6d5b8ac40/diff:/docker-data/overlay2/a6d3f2a0351a1bd5bdfd99bd9cecfc6ce699e18e09a42c256c84c17f88ea0361/diff:/docker-data/overlay2/749aa8ed3acb19614038f267658318a8a87c8dfbcce9b74de42285b3a30d4432/diff:/docker-data/overlay2/d5151c9f42c4b992f9a995f92df98235e2cb111693822593ecba9b071ad2ccaa/diff",
"MergedDir": "/docker-data/overlay2/0a21b58b2f777ff14f6733b644a03524ddf78f0f1b2fb324c6b0f7b0b684ba12/merged",
"UpperDir": "/docker-data/overlay2/0a21b58b2f777ff14f6733b644a03524ddf78f0f1b2fb324c6b0f7b0b684ba12/diff",
"WorkDir": "/docker-data/overlay2/0a21b58b2f777ff14f6733b644a03524ddf78f0f1b2fb324c6b0f7b0b684ba12/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f",
"sha256:e379e8aedd4d72bb4c529a4ca07a4e4d230b5a1d3f7a61bc80179e8f02421ad8",
"sha256:b8d6e692a25e11b0d32c5c3dd544b71b1085ddc1fddad08e68cbd7fda7f70221",
"sha256:f1db227348d0a5e0b99b15a096d930d1a69db7474a1847acbc31f05e4ef8df8c",
"sha256:32ce5f6a5106cc637d09a98289782edf47c32cb082dc475dd47cbf19a4f866da",
"sha256:d874fd2bc83bb3322b566df739681fbd2248c58d3369cb25908d68e7ed6040a6"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
4.删除镜像----docker rmi iname
docker rmi是docker rm image的简写
#格式:
docker rmi 仓库名称:标签 #当一个镜像有多个标签时,只是删除其中指定的标签
docker rmi 镜像ID号 #会彻底删除该镜像
5.批量删除镜像
#docker images -q 可以加载镜像id
#批量删除所有镜像
docker rmi `docker images -q`
#批量删除nginx镜像
docker rmi `docker images|grep "nginx"`
或者
docker rmi $(docker images -q)
## 删除所有镜像 -f 强制删除,但是实际镜像不会删除
docker rmi $(docker images -q) [-f]
## 如果镜像删除不掉,说明可能有容器正在运行
6.查看镜像的制作过程---docker history iname
[root@docker ~]# docker history nginx
IMAGE CREATED CREATED BY SIZE COMMENT
605c77e624dd 23 months ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
<missing> 23 months ago /bin/sh -c #(nop) STOPSIGNAL SIGQUIT 0B
<missing> 23 months ago /bin/sh -c #(nop) EXPOSE 80 0B
<missing> 23 months ago /bin/sh -c #(nop) ENTRYPOINT ["/docker-entr… 0B
<missing> 23 months ago /bin/sh -c #(nop) COPY file:09a214a3e07c919a… 4.61kB
<missing> 23 months ago /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7… 1.04kB
<missing> 23 months ago /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0… 1.96kB
<missing> 23 months ago /bin/sh -c #(nop) COPY file:65504f71f5855ca0… 1.2kB
<missing> 23 months ago /bin/sh -c set -x && addgroup --system -… 61.1MB
<missing> 23 months ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~bullseye 0B
<missing> 23 months ago /bin/sh -c #(nop) ENV NJS_VERSION=0.7.1 0B
<missing> 23 months ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.21.5 0B
<missing> 23 months ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B
<missing> 23 months ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 23 months ago /bin/sh -c #(nop) ADD file:09675d11695f65c55… 80.4MB
7.修改镜像名
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 23 months ago 141MB
mysql latest 3218b38490ce 23 months ago 516MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@docker ~]# docker tag nginx nginx1
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx1 latest 605c77e624dd 23 months ago 141MB
nginx latest 605c77e624dd 23 months ago 141MB
mysql latest 3218b38490ce 23 months ago 516MB
centos latest 5d0da3dc9764 2 years ago 231MB
容器操作
1.创建容器——docker create
创建容器但不启动
新创建的容器默认处于停止状态,不运行任何程序,需要在其中发起一个进程来启动容器。
#格式:docker create [选项] 镜像
#常用选项:
-i:让容器的输入保持打开
-t:让 Docker 分配一个伪终端
#示例:
docker create -it nginx:latest /bin/bash
2.创建并运行一个新Docker 容器:docker run
同一个镜像可以启动多个容器,每次执行run子命令都会运行一个全新的容器
[root@docker ~]# docker run -itd -P --name nginx nginx
f26e618c46c9552150d16f8dcfff085bfd0dd13a742da2ebaa9c563b6e122bf9
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f26e618c46c9 nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 80/tcp nginx
-i 是标准输入输出
-t 分配一个终端或控制台
-d 后台运行
-P 随机生成端口
--name 生成镜像的名字 后面跟着指定的镜像
-p 指定端口映射
-e 指定环境变量
区别
docker run -itd iname # 运行会启动容器
docker create -it iname # 创建容器,但是不启动
3.查看运行中容器,-a 可以查看所有状态
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
294d5243d71b mysql "docker-entrypoint.s…" 2 hours ago Up 2 hours 3306/tcp, 33060/tcp mysql
f26e618c46c9 nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 80/tcp nginx
1b563326aeed centos "/bin/bash" 2 hours ago Up 2 hours centos
4.查看容器的详细信息----docker inspect iname
[root@docker ~]# docker inspect nginx
[
{
"Id": "f26e618c46c9552150d16f8dcfff085bfd0dd13a742da2ebaa9c563b6e122bf9",
"Created": "2023-12-05T11:55:08.827092726Z",
"Path": "/docker-entrypoint.sh",
"Args": [
"nginx",
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 11349,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-12-05T12:04:15.112214551Z",
"FinishedAt": "2023-12-05T12:03:31.992271732Z"
},
5.启动关闭,重启容器
[root@docker ~]# docker stop nginx
nginx
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f26e618c46c9 nginx "/docker-entrypoint.…" 2 hours ago Exited (0) 12 seconds ago nginx
[root@docker ~]# docker start nginx
nginx
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f26e618c46c9 nginx "/docker-entrypoint.…" 2 hours ago Up 2 seconds 80/tcp nginx
[root@docker ~]# docker restart nginx
nginx
6.挂起,恢复容器
[root@docker ~]# docker pause nginx
nginx
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f26e618c46c9 nginx "/docker-entrypoint.…" 2 hours ago Up 47 seconds (Paused) 80/tcp nginx
[root@docker ~]# docker unpause nginx
nginx
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f26e618c46c9 nginx "/docker-entrypoint.…" 2 hours ago Up About a minute 80/tcp nginx
7.强制关闭容器
[root@docker ~]# docker kill nginx
nginx
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f26e618c46c9 nginx "/docker-entrypoint.…" 2 hours ago Exited (137) 2 seconds ago nginx
stop和kill的区别:
docker stop命令给容器中的进程发送SIGTERM信号,默认行为是会导致容器退出,当然,容器内程序可以捕获该信号并自行处理,例如可以选择忽略。而docker kill则是给容器的进程发送SIGKILL信号,该信号将会使容器必然退出。
8.进入容器——docker exec
进入容器的容器状态必须是up状态
和shell 是两种运行模式
docker run -it会创建前台进程,但是会在输入exit后终止进程。
docker attach会通过连接stdin,连接到容器内输入输出流,会在输入exit后终止容器进程
docker exec -it 会连接到容器,可以像sSH一样进入容器内部,进行操作,可以通过exit退出容器,不影响容器运行。
#需要进入容器进行命令操作时,可以使用 docker exec 命令进入运行着的容器。
#格式:docker exec -it 容器ID/名称 /bin/bash
-i 选项表示让容器的输入保持打开;
-t 选项表示让 Docker 分配一个伪终端。
#示例:进入(三种方式)
docker run -itd centos:7 /bin/bash #先运行容器
docker ps -a
①使用run进入,可以使用ctrl+d退出,直接退出终端
docker run -it centos:7 /bin/bash
②想永久性进入,退出后还是运行状态,用docker exec
docker ps -a
docker exec -it b99e0771c4e1 /bin/bash
③docker attach,会通过连接stdin,连接到容器内输入输出流,公在输入exit后终止容器进程(临时性的,不推荐)
①使用run进入,是一次性进入
②永久性进入,用docker exec
9.查看容器内的进程-----docker top cname
root@docker ~]# docker top nginx
UID PID PPID C STIME TTY TIME CMD
root 13070 13049 0 22:14 pts/0 00:00:00 nginx: master process nginx -g daemon off;
101 13107 13070 0 22:14 pts/0 00:00:00 nginx: worker process
101 13108 13070 0 22:14 pts/0 00:00:00 nginx: worker process
101 13109 13070 0 22:14 pts/0 00:00:00 nginx: worker process
101 13110 13070 0 22:14 pts/0 00:00:00 nginx: worker process
10.查看容器使用宿主机资源情况----docker stats [cname]
[root@docker ~]# docker stats nginx
11.查看容器日志---docker logs iname
[root@docker ~]# docker logs nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/12/05 11:55:09 [notice] 1#1: using the "epoll" event method
2023/12/05 11:55:09 [notice] 1#1: nginx/1.21.5
2023/12/05 11:55:09 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/12/05 11:55:09 [notice] 1#1: OS: Linux 3.10.0-1160.el7.x86_64
2023/12/05 11:55:09 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
12.查看容器内部变化
[root@docker ~]# docker diff nginx
C /root
A /root/.bash_history
C /run
A /run/nginx.pid
C /var
C /var/cache
C /var/cache/nginx
A /var/cache/nginx/fastcgi_temp
A /var/cache/nginx/proxy_temp
A /var/cache/nginx/scgi_temp
A /var/cache/nginx/uwsgi_temp
A /var/cache/nginx/client_temp
C /etc
C /etc/nginx
C /etc/nginx/conf.d
C /etc/nginx/conf.d/default.conf
13.展示已经映射的端口----docker port cname
[root@docker ~]# docker port nginx
[root@docker ~]#
没有是因为我们创建容器的时候 没有设置,下面设置一下 以nginx为例子
随机生成端口
[root@docker ~]# docker run -itd -P --name nginx nginx
2054571a8006a0c5382d883832428d51ba1379860a417ad82530b3362bbc3b98
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2054571a8006 nginx "/docker-entrypoint.…" 3 seconds ago Up 3 seconds 0.0.0.0:32775->80/tcp, :::32775->80/tcp nginx
再次查看映射端口
[root@docker ~]# docker port nginx
80/tcp -> 0.0.0.0:32775
80/tcp -> [::]:32775
这时候我们想要访问容器的nginx,及访问宿主机的32775端口即可
指定生成端口,我可以指定映射宿主机的多少端口,而不是随机
[root@docker ~]# docker run -itd -p 250:80 --name nginx nginx
5481c57c1c1fd207db3050c7bd9436bad65ef84351c59c0b059370155555ba97
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5481c57c1c1f nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 0.0.0.0:250->80/tcp, :::250->80/tcp nginx
查看映射端口
[root@docker ~]# docker port nginx
80/tcp -> 0.0.0.0:250
80/tcp -> [::]:250
14.修改容器的文件---docker cp cname:path hostpath
当我们进入容器后,发现很多 vi vim 都不能使用,那么当我们想要修改nginx的配置文件该怎么修改呢,可以将文件导出来,修改完 在导进去
[root@docker ~]# docker exec -it nginx /bin/bash
root@5481c57c1c1f:/# vim /etc/nginx/nginx.conf
bash: vim: command not found
root@5481c57c1c1f:/# vi /etc/nginx/nginx.conf
bash: vi: command not found
复制
[root@docker ~]# docker cp nginx:/etc/nginx/nginx.conf .
Successfully copied 2.56kB to /root/.
[root@docker ~]# ls
anaconda-ks.cfg nginx.conf
修改完在复制进去
[root@docker ~]# docker cp nginx.conf nginx:/etc/nginx/nginx.conf
Successfully copied 2.56kB to nginx:/etc/nginx/nginx.conf
15.删除容器---docker rm -f
#格式:docker rm [-f] 容器ID/名称
1.#不能删除运行状态的容器,只能-f强制删除,或者先停止再删除
docker rm 3224eb044879
2.#已经退出的容器,可以直接删除
docker rm 1270a6791069
3.#基于名称匹配的方式删除
docker rm -f distracted_panini
4.#删除所有运行状态的容器
docker rm -f `docker ps -q`
5.#删除所有容器
docker rm -f `docker ps -aq`
6.#有选择性的批量删除 (正则匹配)
docker ps -a l awk ' {print "docker rm "$1}'l bash
7.#删除退出状态的容器
for i in `dockef ps -a l grep -i exit / awk '{print $1}' '; do docker rm -f $i;done
1.#不能删除运行状态的容器,只能-f强制删除,或者先停止再删除
2.#已经退出的容器,可以直接删除
16.登录MySQL容器
[root@docker ~]# docker run -itd --name mysql -e MYSQL_ROOT_PASSWORD=Qianfeng@123 mysql
294d5243d71b41ab8c802695588d53d81352aee627c7d732c0ab15f28da46b32
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
294d5243d71b mysql "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 3306/tcp, 33060/tcp mysql
-e指定环境变量 后面跟镜像mysql
进入容器
[root@docker ~]# docker exec -it mysql mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.27 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> ^DBye
容器打包
将容器的文件系统打包成tar文件,也就是把正在运行的容器直接导出为tar包的镜像文件
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5481c57c1c1f nginx "/docker-entrypoint.…" 20 minutes ago Up 20 minutes 0.0.0.0:250->80/tcp, :::250->80/tcp nginx
294d5243d71b mysql "docker-entrypoint.s…" 2 hours ago Up 2 hours 3306/tcp, 33060/tcp mysql
1b563326aeed centos "/bin/bash" 3 hours ago Up 3 hours centos
打包nginx
[root@docker ~]# docker export nginx -o nginx.tar
[root@docker ~]# ls
anaconda-ks.cfg nginx.tar
或者
[root@docker ~]# docker export mysql > mysql.tar
[root@docker ~]# ls
anaconda-ks.cfg mysql.tar nginx.tar
导入镜像归档文件到其他宿主机:
docker import xxx.tar 镜像名:版本号
[root@docker ~]# docker import nginx.tar nginx:1.3
sha256:317784eb22c6e05237ae198b2764d9c74d39bd09671df741758fcf4ff996bf30
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.3 317784eb22c6 6 seconds ago 140MB
镜像迁移(镜像打包)
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.3 317784eb22c6 About a minute ago 140MB
nginx latest 605c77e624dd 23 months ago 141MB
[root@docker ~]# docker save nginx:latest -o nginx.tar
[root@docker ~]# ls
anaconda-ks.cfg nginx.tar
镜像导入
为了验证 先将镜像nginx删除
[root@docker ~]# docker rmi -f nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.3 317784eb22c6 5 minutes ago 140MB
nginx1 latest 605c77e624dd 23 months ago 141MB
mysql latest 3218b38490ce 23 months ago 516MB
centos latest 5d0da3dc9764 2 years ago 231MB
导入
[root@docker ~]# docker load < nginx.tar
Loaded image: nginx:latest
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.3 317784eb22c6 5 minutes ago 140MB
nginx latest 605c77e624dd 23 months ago 141MB
nginx1 latest 605c77e624dd 23 months ago 141MB
mysql latest 3218b38490ce 23 months ago 516MB
centos latest 5d0da3dc9764 2 years ago 231MB
镜像,容器深度操作
# 显示镜像摘要
docker images --digests
# 过滤镜像
docker images [-f|--filter] "key=value"
支持:
dangling:显示标记为空的镜像,值只有true和false
label:这个是根据标签进行过滤,其中lable的值,是docker在编译的时候配置的或者在Dockerfile中配置的
before:这个是根据时间来进行过滤,其中before的value表示某个镜像构建时间之前的镜像列表
since:跟before正好相反,表示的是在某个镜像构建之后构建的镜像
reference:添加正则进行匹配
# 镜像格式化输出
docker images --format "{{.ID}}\t{{.Repository}}" ##中间也可以用-连接
.ID 镜像ID
.Repository 镜像仓库
.Tag 镜像标签[版本]
.Digest 镜像hash值
.CreatedSince 创建镜像后的时间
.CreatedAt 创建镜像的时间
.Size 镜像大小
# 容器格式化输出
docker ps --format "{{.ID}}\t{{.Command}}"
.ID 容器ID
.Image 镜像ID
.Command 执行的命令
.CreatedAt 容器创建时间
.RunningFor 运行时长
.Ports 暴露的端口
.Status 容器状态
.Names 容器名称
.Label 分配给容器的所有标签
.Mounts 容器挂载的卷
.Networks 容器所用的网络名称
利用shell脚本实现 镜像的迁移
#!/usr/bin/bash
image_save(){
for i in `docker images --filter "dangling=false" --format "{{.Repository}}:{{.Tag}}:{{.ID}}"`
do
name=$(echo $i | awk -F':' '{print $1}') # nginx
tag=$(echo $i | awk -F':' '{print $2}') # 1.16.1
id=$(echo $i | awk -F':' '{print $3}') # oaghoahohgo
echo -e "$id\t $name:$tag" >> image.log # 14525020288 nginx:1.16.1
docker save $name:$tag > $id.tar # 保存每个镜像
echo "镜像 $name:$tag 以保存" # 打印提示
done
}
image_load(){
while read line
do
# 每次循环,$line 就是文件的每一行
id=$(echo $line | awk '{print $1}')
name=$(echo $line | awk '{print $2}')
docker load < ${id}.tar
docker tag ${id} $name
echo "镜像 $name 已导入"
done < image.log
}
#
case $1 in
save)
image_save;;
load)
image_load;;
esac