1.管理镜像命令总结
2.获取镜像
我们先查询下ubuntu镜像
docker search ubuntu
[root@k8s-master ~]# docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 16417 [OK]
websphere-liberty WebSphere Liberty multi-architecture images … 297 [OK]
open-liberty Open Liberty multi-architecture images based… 62 [OK]
neurodebian NeuroDebian provides neuroscience research s… 104 [OK]
ubuntu-debootstrap DEPRECATED; use "ubuntu" instead 52 [OK]
ubuntu-upstart DEPRECATED, as is Upstart (find other proces… 115 [OK]
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 98
ubuntu/squid Squid is a caching proxy for the Web. Long-t… 66
ubuntu/cortex Cortex provides storage for Prometheus. Long… 4
ubuntu/apache2 Apache, a secure & extensible open-source HT… 60
ubuntu/kafka Apache Kafka, a distributed event streaming … 35
ubuntu/mysql MySQL open source fast, stable, multi-thread… 53
ubuntu/bind9 BIND 9 is a very flexible, full-featured DNS… 62
ubuntu/prometheus Prometheus is a systems and service monitori… 51
ubuntu/zookeeper ZooKeeper maintains configuration informatio… 12
ubuntu/postgres PostgreSQL is an open source object-relation… 31
ubuntu/redis Redis, an open source key-value store. Long-… 19
ubuntu/grafana Grafana, a feature rich metrics dashboard & … 9
ubuntu/memcached Memcached, in-memory keyvalue store for smal… 5
ubuntu/dotnet-aspnet Chiselled Ubuntu runtime image for ASP.NET a… 11
ubuntu/dotnet-deps Chiselled Ubuntu for self-contained .NET & A… 11
ubuntu/prometheus-alertmanager Alertmanager handles client alerts from Prom… 9
ubuntu/dotnet-runtime Chiselled Ubuntu runtime image for .NET apps… 10
ubuntu/cassandra Cassandra, an open source NoSQL distributed … 2
ubuntu/telegraf Telegraf collects, processes, aggregates & w… 4
docker pull NAME[:tag]
[root@k8s-master ~]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
445a6a12be2b: Pull complete
Digest: sha256:aabed3296a3d45cede1dc866a24476c4d7e093aa806263c27ddaadbdce3c1054
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
3.查看镜像
docker images或者docker image ls
[root@k8s-master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest c6b84b685f35 5 weeks ago 77.8MB
[root@k8s-master ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest c6b84b685f35 5 weeks ago 77.8MB
从左到右一次是:仓库信息,标签,镜像ID,创建时间(镜像最后更新时间),镜像大小
4.给镜像打标签生成新镜像
docker tag ubuntu:latest myubuntu:0922-1
[root@k8s-master ~]# docker tag ubuntu:latest myubuntu:0922-1
[root@k8s-master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu 0922-1 c6b84b685f35 5 weeks ago 77.8MB
ubuntu latest c6b84b685f35 5 weeks ago 77.8MB
查看镜像信息:docker inspect myubuntu:0922-1
[root@k8s-master ~]# docker inspect myubuntu:0922-1
[
{
"Id": "sha256:c6b84b685f35f1a5d63661f5d4aa662ad9b7ee4f4b8c394c022f25023c907b65",
"RepoTags": [
"myubuntu:0922-1",
"ubuntu:latest"
],
"RepoDigests": [
"ubuntu@sha256:aabed3296a3d45cede1dc866a24476c4d7e093aa806263c27ddaadbdce3c1054"
],
"Parent": "",
"Comment": "",
"Created": "2023-08-16T06:01:54.322918183Z",
"Container": "ccf77fc533a6d0610b8f9f15b47d1b42172a8feedf293e1c5bc8c1a37bbf2f37",
"ContainerConfig": {
"Hostname": "ccf77fc533a6",
"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:dc54ed85311a9773c0e9463d957d1c7aff8b8293627078ce7da36365dd8fba21",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.opencontainers.image.ref.name": "ubuntu",
"org.opencontainers.image.version": "22.04"
}
},
"DockerVersion": "20.10.21",
"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:dc54ed85311a9773c0e9463d957d1c7aff8b8293627078ce7da36365dd8fba21",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.opencontainers.image.ref.name": "ubuntu",
"org.opencontainers.image.version": "22.04"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 77823105,
"VirtualSize": 77823105,
"GraphDriver": {
"Data": {
"MergedDir": "/var/lib/docker/overlay2/e58440d5f2865454c841b2379a3f394a2979be15453f6c8821dc87e8a7eb33ab/merged",
"UpperDir": "/var/lib/docker/overlay2/e58440d5f2865454c841b2379a3f394a2979be15453f6c8821dc87e8a7eb33ab/diff",
"WorkDir": "/var/lib/docker/overlay2/e58440d5f2865454c841b2379a3f394a2979be15453f6c8821dc87e8a7eb33ab/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:dc0585a4b8b71f7f4eb8f2e028067f88aec780d9ab40c948a8d431c1aeadeeb5"
]
},
"Metadata": {
"LastTagTime": "2023-09-21T20:28:22.728434314-07:00"
}
}
]
5.查看镜像历史
docker history myubuntu:0922-1
[root@k8s-master ~]# docker history myubuntu:0922-1
IMAGE CREATED CREATED BY SIZE COMMENT
c6b84b685f35 5 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 5 weeks ago /bin/sh -c #(nop) ADD file:aa9b51e9f0067860c… 77.8MB
<missing> 5 weeks ago /bin/sh -c #(nop) LABEL org.opencontainers.… 0B
<missing> 5 weeks ago /bin/sh -c #(nop) LABEL org.opencontainers.… 0B
<missing> 5 weeks ago /bin/sh -c #(nop) ARG LAUNCHPAD_BUILD_ARCH 0B
<missing> 5 weeks ago /bin/sh -c #(nop) ARG RELEASE 0B
6.删除和清理镜像
docker rmi NAME:tag/{IMAGE ID}或者docker image rm NAME:tag/{IMAGE ID}
docker rmi myubuntu:0922-1
[root@k8s-master ~]# docker rmi myubuntu:0922-1
Untagged: myubuntu:0922-1
[root@k8s-master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest c6b84b685f35 5 weeks ago 77.8MB
docker image rm c6b84b685f35
[root@k8s-master ~]# docker image rm c6b84b685f35
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:aabed3296a3d45cede1dc866a24476c4d7e093aa806263c27ddaadbdce3c1054
Deleted: sha256:c6b84b685f35f1a5d63661f5d4aa662ad9b7ee4f4b8c394c022f25023c907b65
Deleted: sha256:dc0585a4b8b71f7f4eb8f2e028067f88aec780d9ab40c948a8d431c1aeadeeb5
当容器在运行需要强制删除镜像时,可以使用-f参数,但是不推荐,通常先删除依赖该镜像的容器(docker rm $container),在来删除镜像
docker rmi -f myubuntu:0922-1
7.清理镜像
一段时间过后,有些没被使用的镜像或者使用过后遗留下来的镜像文件,可以通过docker image prune来清理
docker image prune -f
[root@k8s-master ~]# docker image prune -f
Total reclaimed space: 0B
8.创建镜像
8.1 基于已有的镜像创建
我们先创建个容器,在容器内部创建一个包含hello world的test.txt文件
[root@k8s-master ~]# docker run -it ubuntu:latest /bin/bash
root@82a84790ae2d:/# echo "hello world" >test.txt
root@82a84790ae2d:/# cat test.txt
hello world
root@82a84790ae2d:/# exit
提交镜像:docker commit -m "Add a test.txt" a88076332788 test:0.1
[root@k8s-master ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a88076332788 ubuntu:latest "/bin/bash" 16 seconds ago Up 15 seconds jovial_sutherland
[root@k8s-master ~]# docker commit -m "Add a test.txt" a88076332788 test:0.1
sha256:d766b1cab14eb283049b69fe20c255ca0e44e63eb165065e0e56add2aaa67ac9
[root@k8s-master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test 0.1 d766b1cab14e 6 seconds ago 77.8MB
ubuntu latest c6b84b685f35 5 weeks ago 77.8MB
8.2 基于模板创建
我们可以从网上下载模板或者别的机器上导出模板文件
docker save -o ubuntu.tar ubuntu:latest
[root@k8s-master ~]# docker save -o ubuntu.tar ubuntu:latest
[root@k8s-master ~]# ls
anaconda-ks.cfg docker-tag.sh dockertags.sh original-ks.cfg ssh_test.sh ubuntu.tar
在导入模板docker load -i ubuntu.tar或者docker load < ubuntu.tar
[root@k8s-master ~]# docker load -i ubuntu.tar
Loaded image: ubuntu:latest
[root@k8s-master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test 0.1 d766b1cab14e 7 minutes ago 77.8MB
ubuntu latest c6b84b685f35 5 weeks ago 77.8MB
8.3 基于dockfile创建
创建Dockerfile文件
[root@k8s-master ~]# cat Dockerfile
FROM nginx:latest
RUN echo "hello world!" >/usr/share/nginx/html/index.html
运行构建命令:docker build -t nginx-test .
[root@k8s-master ~]# docker build -t nginx-test .
[+] Building 73.0s (6/6) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 173B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/nginx:latest 9.8s
=> [1/2] FROM docker.io/library/nginx:latest@sha256:32da30332506740a2f7c34d5dc70467b7f14ec67d912703568daff790ab3f755 62.5s
=> => resolve docker.io/library/nginx:latest@sha256:32da30332506740a2f7c34d5dc70467b7f14ec67d912703568daff790ab3f755 0.0s
=> => sha256:a803e7c4b030119420574a882a52b6431e160fceb7620f61b525d49bc2d58886 29.12MB / 29.12MB 24.7s
=> => sha256:8b625c47d69711d95708566cd97b72bca565679d034ee0372e2e23499a0112be 41.34MB / 41.34MB 59.7s
=> => sha256:4d3239651a63f0595b1c047313d6f5c63e1e69c834d315dce09e2c092c2fcea7 627B / 627B 25.4s
=> => sha256:b2888fc9cfe7cd9d6727aeb462d13c7c45dec413b66f2819a36c4a3cb9d4df75 1.78kB / 1.78kB 0.0s
=> => sha256:61395b4c586da2b9b3b7ca903ea6a448e6783dfdd7f768ff2c1a0f3360aaba99 8.15kB / 8.15kB 0.0s
=> => sha256:32da30332506740a2f7c34d5dc70467b7f14ec67d912703568daff790ab3f755 1.86kB / 1.86kB 0.0s
=> => extracting sha256:a803e7c4b030119420574a882a52b6431e160fceb7620f61b525d49bc2d58886 2.4s
=> => sha256:0f816efa513d909851c457ae41744fe3ff36ab19ebc2d72687d8c8f0594c93b3 959B / 959B 25.7s
=> => sha256:01d159b8db2f24da97028c26bf6622e249e162b1adab06a3644c04f1c9fe2dd3 370B / 370B 26.5s
=> => sha256:5fb9a81470f3644c474192baf0827a34749286cb6d933091d4d4463ea4f9c495 1.22kB / 1.22kB 27.4s
=> => sha256:9b1e1e7164db75ad0f64e8deeb33e771d455fa590126b2e16d25e5a75fc6f517 1.41kB / 1.41kB 27.4s
=> => extracting sha256:8b625c47d69711d95708566cd97b72bca565679d034ee0372e2e23499a0112be 2.5s
=> => extracting sha256:4d3239651a63f0595b1c047313d6f5c63e1e69c834d315dce09e2c092c2fcea7 0.0s
=> => extracting sha256:0f816efa513d909851c457ae41744fe3ff36ab19ebc2d72687d8c8f0594c93b3 0.0s
=> => extracting sha256:01d159b8db2f24da97028c26bf6622e249e162b1adab06a3644c04f1c9fe2dd3 0.1s
=> => extracting sha256:5fb9a81470f3644c474192baf0827a34749286cb6d933091d4d4463ea4f9c495 0.0s
=> => extracting sha256:9b1e1e7164db75ad0f64e8deeb33e771d455fa590126b2e16d25e5a75fc6f517 0.0s
=> [2/2] RUN echo "hello world!" >/usr/share/nginx/html/index.html 0.6s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:72e95c88b0234ef26dae885425222c05723c2e33fc4fbb5ec2725c5956e10635 0.0s
=> => naming to docker.io/library/nginx-test
9.上传镜像
docker push nginx-test:latest
[root@k8s-master ~]# docker push nginx-test:latest
The push refers to repository [docker.io/library/nginx-test]
0816e489f33d: Preparing
d26d4f0eb474: Preparing
a7e2a768c198: Preparing
9c6261b5d198: Preparing
ea43d4f82a03: Preparing
1dc45c680d0f: Waiting
eb7e3384f0ab: Waiting
d310e774110a: Waiting
denied: requested access to the resource is denied
因为本地环境不允许,后面我们自己建立个私有镜像仓库在上传