写点什么

【docker 总结】第四篇 - 镜像和容器操作

作者:Brave
  • 2021 年 12 月 16 日
  • 本文字数:7394 字

    阅读完需:约 24 分钟

启动 docker daemon

systemctl start docker
复制代码

查看镜像列表

BravedeMacBook-Pro:~ brave$ docker image listREPOSITORY    TAG       IMAGE ID       CREATED        SIZEmyroot        latest    45a3985181e6   11 hours ago   72.8MB<none>        <none>    b1208d9b031a   11 hours ago   72.8MBnginx         latest    f652ca386ed1   40 hours ago   141MBubuntu        latest    ba6acccedd29   7 weeks ago    72.8MBhello-world   latest    feb5d9fea6a5   2 months ago   13.3kBcentos        6         5bf9684f4720   2 months ago   194MBcentos        latest    5d0da3dc9764   2 months ago   231MB
复制代码

查找指定镜像(与远程仓库找)

docker search centos
复制代码

拉取指定镜像

// 未指定 tag,默认拉取 latest 版本BravedeMacBook-Pro:~ brave$ docker pull centosUsing default tag: latestlatest: Pulling from library/centosDigest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177Status: Image is up to date for centos:latestdocker.io/library/centos:latest
// 指定TAG标签(版本)docker pull centos:7
复制代码



容器的导入 export 和导出 import

export、import(一对)

容器操作:把容器作为归档的文件


docker run -it centos /bin/bash
复制代码


基于 centos 镜像创建容器,并且进入到容器内执行/bin/bash 脚本


-i 互动方式

-t 分配一个伪中断


相当于进入到容器中去了,可以输命令

基于镜像创建容器BravedeMacBook-Pro:~ brave$ docker run -it centos /bin/bash[root@1c19c768bfaa /]# 
1c19c768bfaa就是容器 id
进入 root 目录[root@1c19c768bfaa /]# cd /root[root@1c19c768bfaa ~]#
创建文件:root.txt[root@1c19c768bfaa ~]# touch root.txt[root@1c19c768bfaa ~]# ls anaconda-ks.cfg original-ks.cfg anaconda-post.log root.txt
// 一旦退出后,容器会自动停止[root@1c19c768bfaa ~]# exitexit
// 查看当前正在运行中的容器BravedeMacBook-Pro:~ brave$ docker container psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
// 查看全部容器BravedeMacBook-Pro:~ brave$ docker container ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1c19c768bfaa centos "/bin/bash" 10 minutes ago Exited (0) About a minute ago hopeful_euclidb242b7d7f2be ubuntu "bash" 12 hours ago Exited (0) 12 hours ago dazzling_vaughan
复制代码


导出容器


上边做的这个 ubuntu 文件太大了,找个小的 centos


1,根据镜像创建一个容器

根据镜像创建一个容器BravedeMacBook-Pro:~ brave$ docker run -it centos[root@afe6e9c2ba54 /]# 与指定 cmd 效果一致docker run -it centos /bin/bash
cmd 表示进入到容器后执行的第一个命令,如果不写,会执行镜像默认的命令centos 默认执行命令也是 /bin/bash,所以一样
复制代码

2,在容器中创建文件

[root@afe6e9c2ba54 /]# touch /root/root.txt[root@afe6e9c2ba54 /]# exitexit
复制代码

3,导出

// 指定容器 id 导出为 centos.tar// export 导出// -o outputBravedeMacBook-Pro:~ brave$ docker export -o centos.tar afe6e9c2ba54
// 本地多出一个 centos.tarBravedeMacBook-Pro:~ brave$ lscentos.tar
复制代码


导入容器


先删除掉本地的 centos 镜像

先删除容器,才能删除镜像(容器依赖于镜像)


1,先删除容器

// 查看全部容器BravedeMacBook-Pro:~ brave$ docker container ps -aCONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMESafe6e9c2ba54   centos    "/bin/bash"   6 minutes ago    Exited (0) 5 minutes ago              gracious_hawking1c19c768bfaa   centos    "/bin/bash"   24 minutes ago   Exited (0) 15 minutes ago             hopeful_euclidb242b7d7f2be   ubuntu    "bash"        12 hours ago     Exited (0) 12 hours ago               dazzling_vaughan
// 删除单个容器docker container rm 镜像 id// 删除全部容器BravedeMacBook-Pro:~ brave$ docker container rm $(docker container ps -a -q)afe6e9c2ba541c19c768bfaab242b7d7f2be
// 删除后容器就没了BravedeMacBook-Pro:~ brave$ docker container ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
复制代码

2,删除镜像

BravedeMacBook-Pro:~ brave$ docker image rmi centosUntagged: centos:latestUntagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
复制代码

3,导入镜像

// 导入前,查看的本地镜像列表BravedeMacBook-Pro:~ brave$ docker image lsREPOSITORY    TAG       IMAGE ID       CREATED        SIZEmyroot        latest    45a3985181e6   12 hours ago   72.8MB<none>        <none>    b1208d9b031a   12 hours ago   72.8MBnginx         latest    f652ca386ed1   41 hours ago   141MBubuntu        latest    ba6acccedd29   7 weeks ago    72.8MBhello-world   latest    feb5d9fea6a5   2 months ago   13.3kBcentos        6         5bf9684f4720   2 months ago   194MB
// 导入镜像(未指定名称)BravedeMacBook-Pro:~ brave$ docker import centos.tarsha256:052d7823563020ee175d8a8557814ec43b3d7960f127cb1436f1614c9d2ab060
// 导入后,查看的本地镜像列表(发现一个没有 REPOSITORY、TAG的新的镜像)BravedeMacBook-Pro:~ brave$ docker image lsREPOSITORY TAG IMAGE ID CREATED SIZE<none> <none> 052d78235630 44 seconds ago 231MBmyroot latest 45a3985181e6 12 hours ago 72.8MB<none> <none> b1208d9b031a 12 hours ago 72.8MBnginx latest f652ca386ed1 41 hours ago 141MBubuntu latest ba6acccedd29 7 weeks ago 72.8MBhello-world latest feb5d9fea6a5 2 months ago 13.3kBcentos 6 5bf9684f4720 2 months ago 194MB
复制代码

prune 裁剪


这里导入的镜像,没有指定 REPOSITORY、TAG,是为了演示 prune 命令

prune:移除未使用的镜像,没有标记或不被任何容器引用

解释: 容器没有 tag,且没有容器引用,就会被删掉


BravedeMacBook-Pro:~ brave$ docker image pruneWARNING! This will remove all dangling images.Are you sure you want to continue? [y/N] yDeleted Images:deleted: sha256:b1208d9b031a399015ed4192c3763e884a5f052b514e167447f91e05f2874201deleted: sha256:052d7823563020ee175d8a8557814ec43b3d7960f127cb1436f1614c9d2ab060deleted: sha256:443830cb33772f938c940d5c9f586331dfdc7a696e47d828141f19a7b7425938
Total reclaimed space: 231.3MB
复制代码

提示,移除所有悬挂的/孤儿镜像


再看一下镜像列表,被删掉了

BravedeMacBook-Pro:~ brave$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZEmyroot latest 45a3985181e6 12 hours ago 72.8MBnginx latest f652ca386ed1 41 hours ago 141MBubuntu latest ba6acccedd29 7 weeks ago 72.8MBhello-world latest feb5d9fea6a5 2 months ago 13.3kBcentos 6 5bf9684f4720 2 months ago 194MB
复制代码


将容器导出为一个镜像,然后再导入回来


镜像的保存 save 和加载 load

save、load(一对)

镜像操作:


save:吧镜像保存为文件

保存镜像


// 获取要导出的镜像名BravedeMacBook-Pro:~ brave$ docker image lshello-world   latest    feb5d9fea6a5   2 months ago   13.3kB
// 指定导出的镜像 hello-world:latest 和 导出的文件名 hello-world.tarBravedeMacBook-Pro:~ brave$ docker save -o hello-world.tar hello-world:latestBravedeMacBook-Pro:~ brave$
复制代码

加载镜像


// 删除本地镜像BravedeMacBook-Pro:~ brave$ docker image rmi hello-world:latestUntagged: hello-world:latestUntagged: hello-world@sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
// 导入前,查看镜像列表BravedeMacBook-Pro:~ brave$ docker image lsREPOSITORY TAG IMAGE ID CREATED SIZE
// 加载镜像BravedeMacBook-Pro:~ brave$ docker load -i hello-world.tare07ee1baac5f: Loading layer 14.85kB/14.85kBLoaded image: hello-world:latest
// 导入前,查看镜像列表BravedeMacBook-Pro:~ brave$ docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEhello-world latest feb5d9fea6a5 2 months ago 13.3kB
复制代码


import、export 与 save、load 的区别


export 导出容器到文件.tar

import 把文件导入为镜像

容器没有名字和标签,所以导出时没有名字,导入时可以指定名字


save 把镜像导出为文件.tar

load 把文件导入为镜像;load 时不能修改名字和标签,因为导出的镜像是有名字和标签的;


docker image prune 裁剪



tag


tag 标记镜像,将其归入某一个仓库


BravedeMacBook-Pro:~ brave$ docker tag hello-world:latest brave/centos:v1BravedeMacBook-Pro:~ brave$ docker image lsREPOSITORY     TAG       IMAGE ID       CREATED        SIZEbrave/centos   v1        feb5d9fea6a5   2 months ago   13.3kBhello-world    latest    feb5d9fea6a5   2 months ago   13.3kB
复制代码



容器

attach 和 exec

attach 进入一个正在运行中的程序,老版本用法,在容器中退出,会导致容器停止

新版本用 exec,进入一个正在运行中的容器脚本


基于 centos 镜像,在后台启动一个容器,进入到容器内部,执行/bin/sh


docker run -d centos /bin/sh -c "while true; do echo hello;sleep 1s; done"

死循环,后台容器,不断执行输出 hello,不会终止,就永远退步出来,不会终止

BravedeMacBook-Pro:~ brave$ docker run -d centos /bin/sh -c "while true; do echo hello;sleep 1s; done"f08109d1269d4b30fc45d117f65282302cb8db8c10b0dd5297fe83a25d671234
查看正在运行中的容器BravedeMacBook-Pro:~ brave$ docker container ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf08109d1269d centos "/bin/sh -c 'while t…" 31 seconds ago Up 30 seconds hopeful_burnell7f87bc954787 centos "/bin/sh" 4 minutes ago Exited (0) 4 minutes ago admiring_swirles
进入到容器内容:BravedeMacBook-Pro:~ brave$ docker exec -it f08109d1269d4b30fc45d117f65282302cb8db8c10b0dd5297fe83a25d671234 /bin/bashBravedeMacBook-Pro:~ brave$
复制代码

stats 查看资源统计


查看容器的资源占用情况

BravedeMacBook-Pro:~ brave$ docker container stats f08109d1269d
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDSf08109d1269d hopeful_burnell 0.01% 884KiB / 1.939GiB 0.04% 1.16kB / 0B 0B / 0B 2
复制代码

top 查看容器进程

BravedeMacBook-Pro:~ brave$ dockecontainer top f08109d1269dUID                 PID                 PPID                C                   STIME               TTY                 TIME                CMDroot                3638                3613                0                   08:26               ?                   00:00:00            /bin/sh -c while true; do echo hello;sleep 1s; doneroot                4103                3638                0                   08:33               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1s
复制代码

update 更新容器配置


更新一个或多个容器配置

可以限定一个容器占用资源的情况:设定内存、cpu 等上限


port 查看端口映射


没有端口映射的情况BravedeMacBook-Pro:~ brave$ docker run -d nginx86dfeaa9638c3ff5a3a1bc37889d62cac0efd0ab0e288fb6ead90d3a25e47c4a
有端口映射的情况BravedeMacBook-Pro:~ brave$ docker run -d -p 8090:80 nginxd0f4ff48842712fabdcb94a44a3c01c4552a1c75613a041806365a79ec0de00b
访问容器内部的 nginxBravedeMacBook-Pro:~ brave$ curl http://localhost:8090<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>html { color-scheme: light dark; }body { width: 35em; margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif; }</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p>
<p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p></body></html>
复制代码

logs 查看容器内的日志

进入容器内查看 nginx 日志(主要不要用 attach,一旦退出会导致容器停掉,使用 exec)BravedeMacBook-Pro:~ brave$ docker container logs d0f4ff48842712fabdcb94a44a3c01c4552a1c75613a041806365a79ec0de00b/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.sh10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf10-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 up2021/12/04 08:37:19 [notice] 1#1: using the "epoll" event method2021/12/04 08:37:19 [notice] 1#1: nginx/1.21.42021/12/04 08:37:19 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 2021/12/04 08:37:19 [notice] 1#1: OS: Linux 5.10.76-linuxkit2021/12/04 08:37:19 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:10485762021/12/04 08:37:19 [notice] 1#1: start worker processes2021/12/04 08:37:19 [notice] 1#1: start worker process 332021/12/04 08:37:19 [notice] 1#1: start worker process 342021/12/04 08:37:19 [notice] 1#1: start worker process 352021/12/04 08:37:19 [notice] 1#1: start worker process 36172.17.0.1 - - [04/Dec/2021:08:38:25 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.64.1" "-"
复制代码

kill 停止容器(stop、kill 区别)

stop 是终止容器 发送 SIGTERM 容器内部可以忽略

kill 终止容器 发送 SIGKILL 容器不能忽略 必死

BravedeMacBook-Pro:~ brave$ docker kill d0f4ff48842712fabdcb94a44a3c01c4552a1c75613a041806365a79ec0de00bd0f4ff48842712fabdcb94a44a3c01c4552a1c75613a041806365a79ec0de00b
再查看,8090 的已经没有了,只剩下 80 的 nginxBravedeMacBook-Pro:~ brave$ docker container psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES86dfeaa9638c nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp nifty_satoshif08109d1269d centos "/bin/sh -c 'while t…" 17 minutes ago Up 17 minutes hopeful_burnell
复制代码

start 启动容器

BravedeMacBook-Pro:~ brave$ docker start d0f4ff488427d0f4ff488427
启动后重新查看,8090 的 nginx 回来了BravedeMacBook-Pro:~ brave$ docker container psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd0f4ff488427 nginx "/docker-entrypoint.…" 10 minutes ago Up 2 seconds 0.0.0.0:8090->80/tcp suspicious_ganguly86dfeaa9638c nginx "/docker-entrypoint.…" 11 minutes ago Up 11 minutes 80/tcp nifty_satoshif08109d1269d centos "/bin/sh -c 'while t…" 21 minutes ago Up 21 minutes hopeful_burnell
复制代码

cp 拷贝容器内的文件

将容器内部的文件拷贝到硬盘上


启动容器创建一个文件,并写入内容

BravedeMacBook-Pro:~ brave$ docker run -it ubuntu /bin/bashroot@9c68a087624c:/# touch text.txtroot@9c68a087624c:/# echo test >> text.txt
root@9c68a087624c:/# pwd/
root@9c68a087624c:/# exitexit
复制代码

拷贝容器中的文件到本地


// 注意最后这个. 表示当前宿主机的当前目录BravedeMacBook-Pro:~ brave$ docker container cp 9c68a087624c:/text.txt .
// 查看本地文件BravedeMacBook-Pro:~ brave$ lstext.txt
复制代码


用户头像

Brave

关注

还未添加个人签名 2018.12.13 加入

还未添加个人简介

评论

发布
暂无评论
【docker 总结】第四篇 - 镜像和容器操作