【docker 总结】第四篇 - 镜像和容器操作
- 2021 年 12 月 16 日
本文字数:7394 字
阅读完需:约 24 分钟
启动 docker daemon
systemctl start docker
查看镜像列表
BravedeMacBook-Pro:~ brave$ docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
myroot latest 45a3985181e6 11 hours ago 72.8MB
<none> <none> b1208d9b031a 11 hours ago 72.8MB
nginx latest f652ca386ed1 40 hours ago 141MB
ubuntu latest ba6acccedd29 7 weeks ago 72.8MB
hello-world latest feb5d9fea6a5 2 months ago 13.3kB
centos 6 5bf9684f4720 2 months ago 194MB
centos latest 5d0da3dc9764 2 months ago 231MB
查找指定镜像(与远程仓库找)
docker search centos
拉取指定镜像
// 未指定 tag,默认拉取 latest 版本
BravedeMacBook-Pro:~ brave$ docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Image is up to date for centos:latest
docker.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 ~]# exit
exit
// 查看当前正在运行中的容器
BravedeMacBook-Pro:~ brave$ docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
// 查看全部容器
BravedeMacBook-Pro:~ brave$ docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c19c768bfaa centos "/bin/bash" 10 minutes ago Exited (0) About a minute ago hopeful_euclid
b242b7d7f2be 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 /]# exit
exit
3,导出
// 指定容器 id 导出为 centos.tar
// export 导出
// -o output
BravedeMacBook-Pro:~ brave$ docker export -o centos.tar afe6e9c2ba54
// 本地多出一个 centos.tar
BravedeMacBook-Pro:~ brave$ ls
centos.tar
导入容器
先删除掉本地的 centos 镜像
先删除容器,才能删除镜像(容器依赖于镜像)
1,先删除容器
// 查看全部容器
BravedeMacBook-Pro:~ brave$ docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
afe6e9c2ba54 centos "/bin/bash" 6 minutes ago Exited (0) 5 minutes ago gracious_hawking
1c19c768bfaa centos "/bin/bash" 24 minutes ago Exited (0) 15 minutes ago hopeful_euclid
b242b7d7f2be 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)
afe6e9c2ba54
1c19c768bfaa
b242b7d7f2be
// 删除后容器就没了
BravedeMacBook-Pro:~ brave$ docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2,删除镜像
BravedeMacBook-Pro:~ brave$ docker image rmi centos
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
3,导入镜像
// 导入前,查看的本地镜像列表
BravedeMacBook-Pro:~ brave$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
myroot latest 45a3985181e6 12 hours ago 72.8MB
<none> <none> b1208d9b031a 12 hours ago 72.8MB
nginx latest f652ca386ed1 41 hours ago 141MB
ubuntu latest ba6acccedd29 7 weeks ago 72.8MB
hello-world latest feb5d9fea6a5 2 months ago 13.3kB
centos 6 5bf9684f4720 2 months ago 194MB
// 导入镜像(未指定名称)
BravedeMacBook-Pro:~ brave$ docker import centos.tar
sha256:052d7823563020ee175d8a8557814ec43b3d7960f127cb1436f1614c9d2ab060
// 导入后,查看的本地镜像列表(发现一个没有 REPOSITORY、TAG的新的镜像)
BravedeMacBook-Pro:~ brave$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 052d78235630 44 seconds ago 231MB
myroot latest 45a3985181e6 12 hours ago 72.8MB
<none> <none> b1208d9b031a 12 hours ago 72.8MB
nginx latest f652ca386ed1 41 hours ago 141MB
ubuntu latest ba6acccedd29 7 weeks ago 72.8MB
hello-world latest feb5d9fea6a5 2 months ago 13.3kB
centos 6 5bf9684f4720 2 months ago 194MB
prune 裁剪
这里导入的镜像,没有指定 REPOSITORY、TAG,是为了演示 prune 命令
prune:移除未使用的镜像,没有标记或不被任何容器引用
解释: 容器没有 tag,且没有容器引用,就会被删掉
BravedeMacBook-Pro:~ brave$ docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Deleted Images:
deleted: sha256:b1208d9b031a399015ed4192c3763e884a5f052b514e167447f91e05f2874201
deleted: sha256:052d7823563020ee175d8a8557814ec43b3d7960f127cb1436f1614c9d2ab060
deleted: sha256:443830cb33772f938c940d5c9f586331dfdc7a696e47d828141f19a7b7425938
Total reclaimed space: 231.3MB
提示,移除所有悬挂的/孤儿镜像
再看一下镜像列表,被删掉了
BravedeMacBook-Pro:~ brave$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
myroot latest 45a3985181e6 12 hours ago 72.8MB
nginx latest f652ca386ed1 41 hours ago 141MB
ubuntu latest ba6acccedd29 7 weeks ago 72.8MB
hello-world latest feb5d9fea6a5 2 months ago 13.3kB
centos 6 5bf9684f4720 2 months ago 194MB
将容器导出为一个镜像,然后再导入回来
镜像的保存 save 和加载 load
save、load(一对)
镜像操作:
save:吧镜像保存为文件
保存镜像
// 获取要导出的镜像名
BravedeMacBook-Pro:~ brave$ docker image ls
hello-world latest feb5d9fea6a5 2 months ago 13.3kB
// 指定导出的镜像 hello-world:latest 和 导出的文件名 hello-world.tar
BravedeMacBook-Pro:~ brave$ docker save -o hello-world.tar hello-world:latest
BravedeMacBook-Pro:~ brave$
加载镜像
// 删除本地镜像
BravedeMacBook-Pro:~ brave$ docker image rmi hello-world:latest
Untagged: hello-world:latest
Untagged: hello-world@sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
// 导入前,查看镜像列表
BravedeMacBook-Pro:~ brave$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
// 加载镜像
BravedeMacBook-Pro:~ brave$ docker load -i hello-world.tar
e07ee1baac5f: Loading layer 14.85kB/14.85kB
Loaded image: hello-world:latest
// 导入前,查看镜像列表
BravedeMacBook-Pro:~ brave$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-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:v1
BravedeMacBook-Pro:~ brave$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
brave/centos v1 feb5d9fea6a5 2 months ago 13.3kB
hello-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 -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f08109d1269d centos "/bin/sh -c 'while t…" 31 seconds ago Up 30 seconds hopeful_burnell
7f87bc954787 centos "/bin/sh" 4 minutes ago Exited (0) 4 minutes ago admiring_swirles
进入到容器内容:
BravedeMacBook-Pro:~ brave$ docker exec -it f08109d1269d4b30fc45d117f65282302cb8db8c10b0dd5297fe83a25d671234 /bin/bash
BravedeMacBook-Pro:~ brave$
stats 查看资源统计
查看容器的资源占用情况
BravedeMacBook-Pro:~ brave$ docker container stats f08109d1269d
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
f08109d1269d hopeful_burnell 0.01% 884KiB / 1.939GiB 0.04% 1.16kB / 0B 0B / 0B 2
top 查看容器进程
BravedeMacBook-Pro:~ brave$ dockecontainer top f08109d1269d
UID PID PPID C STIME TTY TIME CMD
root 3638 3613 0 08:26 ? 00:00:00 /bin/sh -c while true; do echo hello;sleep 1s; done
root 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 nginx
86dfeaa9638c3ff5a3a1bc37889d62cac0efd0ab0e288fb6ead90d3a25e47c4a
有端口映射的情况
BravedeMacBook-Pro:~ brave$ docker run -d -p 8090:80 nginx
d0f4ff48842712fabdcb94a44a3c01c4552a1c75613a041806365a79ec0de00b
访问容器内部的 nginx
BravedeMacBook-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 and
working. 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.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
2021/12/04 08:37:19 [notice] 1#1: using the "epoll" event method
2021/12/04 08:37:19 [notice] 1#1: nginx/1.21.4
2021/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-linuxkit
2021/12/04 08:37:19 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/12/04 08:37:19 [notice] 1#1: start worker processes
2021/12/04 08:37:19 [notice] 1#1: start worker process 33
2021/12/04 08:37:19 [notice] 1#1: start worker process 34
2021/12/04 08:37:19 [notice] 1#1: start worker process 35
2021/12/04 08:37:19 [notice] 1#1: start worker process 36
172.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 d0f4ff48842712fabdcb94a44a3c01c4552a1c75613a041806365a79ec0de00b
d0f4ff48842712fabdcb94a44a3c01c4552a1c75613a041806365a79ec0de00b
再查看,8090 的已经没有了,只剩下 80 的 nginx
BravedeMacBook-Pro:~ brave$ docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
86dfeaa9638c nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp nifty_satoshi
f08109d1269d centos "/bin/sh -c 'while t…" 17 minutes ago Up 17 minutes hopeful_burnell
start 启动容器
BravedeMacBook-Pro:~ brave$ docker start d0f4ff488427
d0f4ff488427
启动后重新查看,8090 的 nginx 回来了
BravedeMacBook-Pro:~ brave$ docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d0f4ff488427 nginx "/docker-entrypoint.…" 10 minutes ago Up 2 seconds 0.0.0.0:8090->80/tcp suspicious_ganguly
86dfeaa9638c nginx "/docker-entrypoint.…" 11 minutes ago Up 11 minutes 80/tcp nifty_satoshi
f08109d1269d centos "/bin/sh -c 'while t…" 21 minutes ago Up 21 minutes hopeful_burnell
cp 拷贝容器内的文件
将容器内部的文件拷贝到硬盘上
启动容器创建一个文件,并写入内容
BravedeMacBook-Pro:~ brave$ docker run -it ubuntu /bin/bash
root@9c68a087624c:/# touch text.txt
root@9c68a087624c:/# echo test >> text.txt
root@9c68a087624c:/# pwd
/
root@9c68a087624c:/# exit
exit
拷贝容器中的文件到本地
// 注意最后这个. 表示当前宿主机的当前目录
BravedeMacBook-Pro:~ brave$ docker container cp 9c68a087624c:/text.txt .
// 查看本地文件
BravedeMacBook-Pro:~ brave$ ls
text.txt
Brave
还未添加个人签名 2018.12.13 加入
还未添加个人简介
评论