1 ,Docker命令帮助
官方文档:
https://docs.docker.com/reference/
https://docs.docker.com/engine/reference/commandline/cli/
root@ubuntu2204:~# docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker
context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
root@ubuntu2204:~#
docker [OPTIONS] COMMAND
COMMAND分为
Management Commands #指定管理的资源对象类型,较新的命令用法,将命令按资源类型进行分类,方便使
用
Commands #对不同资源操作的命令不分类,使用容易产生混乱
查看Docker版本
root@ubuntu2204:~# docker version
查看Docker详细信息
root@ubuntu2204:~# docker info
Client:
Debug Mode: false #client 端是否开启 debug
group-swap-limit-capabilities
Server:
Containers: 2 #当前主机运行的容器总数
Running: 0 #有几个容器是正在运行的
Paused: 0 #有几个容器是暂停的
Stopped: 2 #有几个容器是停止的
Images: 4 #当前服务器的镜像数
Server Version: 19.03.5 #服务端版本
Storage Driver: overlay2 #正在使用的存储引擎
Backing Filesystem: extfs #后端文件系统,即服务器的磁盘文件系统
Supports d_type: true #是否支持 d_type
Native Overlay Diff: true #是否支持差异数据存储
Logging Driver: json-file #日志类型,每个容器的标准输出以日志存放
在/var/lib/docker/containers/<CONTAINER ID>/<CONTAINER ID>-json.log
Cgroup Driver: cgroupfs #Cgroups 类型
Plugins: #插件
Volume: local #卷
Network: bridge host ipvlan macvlan null overlay # overlay 跨主机通信
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk
syslog # 日志类型
Swarm: inactive #是否支持 swarm
Runtimes: runc #已安装的容器运行时
Default Runtime: runc #默认使用的容器运行时
Init Binary: docker-init #初始化容器的守护进程,即 pid 为 1 的进程
containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339 #版本
runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657 #runc 版本
init version: fec3683 #init 版本
Security Options: #安全选项
apparmor #安全模块,https://docs.docker.com/engine/security/apparmor/
seccomp #安全计算模块,即制容器操作,
https://docs.docker.com/engine/security/seccomp/
Profile: default #默认的配置文件
Kernel Version: 4.15.0-29-generic #宿主机内核版本
Operating System: Ubuntu 18.04.1 LTS #宿主机操作系统
OSType: linux #宿主机操作系统类型
Architecture: x86_64 #宿主机架构
CPUs: 1 #宿主机 CPU 数量
Total Memory: 962MiB #宿主机总内存
Name: ubuntu1804.wang.org #宿主机 hostname
ID: IZHJ:WPIN:BRMC:XQUI:VVVR:UVGK:NZBM:YQXT:JDWB:33RS:45V7:SQWJ #宿主机 ID
Docker Root Dir: /var/lib/docker #宿主机关于docker数据的保存目录,建议使用独立SSD的磁
盘,保证性能和空间
Debug Mode: false #server 端是否开启 debug
Registry: https://index.docker.io/v1/ #仓库路径
Labels:
Experimental: false #是否测试版
Insecure Registries:
127.0.0.0/8 : #非安全的镜像仓库
Registry Mirrors:
https://si7y70hh.mirror.aliyuncs.com/ #镜像仓库
Live Restore Enabled: false #是否开启活动重启 (重启docker-daemon 不关闭容器 )
WARNING: No swap limit support #系统警告信息 (没有开启 swap 资源限制
解决上述SWAP报警提示
官方文档: https://docs.docker.com/install/linux/linux-postinstall/#your-kernel-does-not-support-c
2 ,镜像管理
云原生入门 技能树:https://edu.csdn.net/skill/cloud_native?utm_source=AI_act_sixin_beginner_cloud_native&utm_medium=notify.im.rec_skill_tree.rec_skill_tree-9ba66ce2e43f4bf2b661a7d984d0c340.a&username=weixin_51943889
2.1 镜像加速器
默认从官方下载,官方下载比较慢,所以需要使用镜像加速器,从国内的镜像仓库下载。
#网易云: http://hub-mirror.c.163.com/
#中科大: https://docker.mirrors.ustc.edu.cn
#腾讯云: https://mirror.ccs.tencentyun.com
#七牛云: https://reg-mirror.qiniu.com
阿里官网介绍:https://developer.aliyun.com/article/29941?spm=5176.21213303.J_6704733920.7.72d353c9fdSjX9&scm=20140722.S_community%40%40%E6%96%87%E7%AB%A0%40%4029941..ID_community%40%40%E6%96%87%E7%AB%A0%40%4029941-RL%E9%95%9C%E5%83%8F%E5%8A%A0%E9%80%9F%E5%99%A8-LOC_main-OR_ser-V_2-P0_0
root@ubuntu2204:~# docker info
Client:
Context: default
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.12
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version:
runc version:
init version:
Security Options:
apparmor
seccomp
Profile: default
cgroupns
Kernel Version: 5.15.0-43-generic
Operating System: Ubuntu 22.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.8GiB
Name: ubuntu2204
ID: EPZP:DN6N:66M7:XSIF:DHXM:FQL5:L4NE:42JP:YGZ7:YZ2R:DJX5:DQ57
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/ #默认从官方下载镜像
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
root@ubuntu2204:~#
配置阿里云镜像加速器
登录阿里云–>控制台–>容器镜像服务–>镜像工具–>镜像加速器
执行如下:
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://uietgfqt.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
或
修改Docke镜像配置文件
root@ubuntu2204:~# vim /etc/docker/daemon.json
#添加如下内容
{
"registry-mirrors": ["https://uietgfqt.mirror.aliyuncs.com"]
}
root@ubuntu2204:~# sudo mkdir -p /etc/docker
root@ubuntu2204:~# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://uietgfqt.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://uietgfqt.mirror.aliyuncs.com"]
}
root@ubuntu2204:~# sudo systemctl daemon-reload
root@ubuntu2204:~# sudo systemctl restart docker
root@ubuntu2204:~# docker info
Client:
Context: default
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.12
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version:
runc version:
init version:
Security Options:
apparmor
seccomp
Profile: default
cgroupns
Kernel Version: 5.15.0-43-generic
Operating System: Ubuntu 22.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.8GiB
Name: ubuntu2204
ID: EPZP:DN6N:66M7:XSIF:DHXM:FQL5:L4NE:42JP:YGZ7:YZ2R:DJX5:DQ57
Docker Root Dir: /var/lib/docker #存放Docker的数据路径,后续所有的Docker信息都在这个路径下,包括镜像、容器等;生产环境的话建议高速磁盘的一个空间并且容量不能太小来存放。当然这个目录也可以改变的,
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries: ###在这里可以添加公司内部的镜像
127.0.0.0/8
Registry Mirrors: ##### #阿里云docker镜像加速
https://uietgfqt.mirror.aliyuncs.com/
Live Restore Enabled: false #这里建议改为true,改了之后docker服务重启不会影响容器服务的工作,不改会默认docker服务重启容器就重启
root@ubuntu2204:~#
2.2 Docker镜像组成
AUFS (AnotherUnionFS) 是一种 Union FS, 简单来说就是支持将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem)的文件系统, 更进一步地, AUFS支持为每一个成员目录(AKA branch)设定’readonly’, ‘readwrite’ 和 ‘whiteout-able’ 权限, 同时AUFS里有一个类似分层的概念, 对 readonly 权限的branch可以逻辑上进行修改(增量地, 不影响readonly部分的)。通常 Union FS有两个用途, 一方面可以实现不借助 LVM, RAID 将多个disk和挂在到一个目录下, 另一个更常用的就是将一个readonly的branch和一个writeable的branch联合在一起,Live CD正是基于此可以允许在 OS image 不变的基础上允许用户在其上进行一些写操作。Docker在AUFS上构建的container image也正是如此,接下来我们从启动container中的linux为例介绍docker在AUFS特性的运用。
典型的Linux启动到运行需要两个FS - bootfs + rootfs (从功能角度而非文件系统角度)。
bootfs (boot file system) 主要包含 bootloader 和 kernel, bootloader主要是引导加载kernel, 当boot成功后 kernel 被加载到内存中后 bootfs就被umount了.
rootfs (root file system) 包含的就是典型 Linux 系统中的 /dev, /proc, /bin, /etc 等标准目录和文件。
由此可见对于不同的linux发行版, bootfs基本是一致的, rootfs会有差别, 因此不同的发行版可以公用bootfs ,如下:
在docker中,起初也是将 rootfs 以readonly方式加载并检查,然而接下来利用 union mount 的将一个 readwrite 文件系统挂载在 readonly 的rootfs之上,并且允许再次将下层的 file system设定为readonly 并且向上叠加, 这样一组readonly和一个writeable的结构构成一个container的运行目录, 每一个被称作一个Layer。
得益于AUFS的特性, 每一个对readonly层文件/目录的修改都只会存在于上层的writeable层中。这样由于不存在竞争, 多个container可以共享readonly的layer。
所以docker将readonly的层称作 “image” - 对于container而言整个rootfs都是read-write的,但事实上所有的修改都写入最上层的writeable层中,
image不保存用户状态,可以用于模板、重建和复制。
上层的image依赖下层的image,因此docker中把下层的image称作父image,没有父image的image称作base image
因此想要从一个image启动一个container,docker会先加载其父image直到base image,用户的进程运行在writeable的layer中。所有parent image中的数据信息以及ID、网络和lxc管理的资源限制等具体container的配置,构成一个docker概念上的container。
先找一个基础镜像如Centos、Ubuntu、Debian;在做一些简单的初始化和安装常用的一些命令(就跟我们虚拟机最小安装之后初始化类似),生成下一级镜像;再去安装一些需要的组件如JDK,生成下一级镜像;再去安装如Tomacat这样的应用程序;下一级镜像是由上一级镜像添加“数据”累计而来,可见Docker镜像是一个分层镜像累计而成的一个镜像。
镜像最上层可读可写,下层镜像被屏蔽只读。
从图可以看出,多个镜像可以共享base image存储,base image可以避免多次拷贝,所以节省存储空间、内存更省和快速部署;
所有镜像都没有内核,镜像利用宿主机的操作系统内核,那么镜像简单理解就是让一个软件运行起来所需除kernel之外的如rootfs、lib、主程序、配置文件、数据文件的打包集合。
查看镜像的分层结构:
root@ubuntu2204:~# docker pull nginx #拉取nginx镜像
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
root@ubuntu2204:~# docker images history nginx
"docker images" requires at most 1 argument.
See 'docker images --help'.
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
root@ubuntu2204:~# docker image history nginx #查看镜像分层历史
IMAGE CREATED CREATED BY SIZE COMMENT
605c77e624dd 12 months ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
<missing> 12 months ago /bin/sh -c #(nop) STOPSIGNAL SIGQUIT 0B
<missing> 12 months ago /bin/sh -c #(nop) EXPOSE 80 0B
<missing> 12 months ago /bin/sh -c #(nop) ENTRYPOINT ["/docker-entr… 0B
<missing> 12 months ago /bin/sh -c #(nop) COPY file:09a214a3e07c919a… 4.61kB
<missing> 12 months ago /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7… 1.04kB
<missing> 12 months ago /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0… 1.96kB
<missing> 12 months ago /bin/sh -c #(nop) COPY file:65504f71f5855ca0… 1.2kB
<missing> 12 months ago /bin/sh -c set -x && addgroup --system -… 61.1MB
<missing> 12 months ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~bullseye 0B
<missing> 12 months ago /bin/sh -c #(nop) ENV NJS_VERSION=0.7.1 0B
<missing> 12 months ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.21.5 0B
<missing> 12 months ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B
<missing> 12 months ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 12 months ago /bin/sh -c #(nop) ADD file:09675d11695f65c55… 80.4MB
root@ubuntu2204:~#
2.3 镜像操作
官网:
http://hub.docker.com
http://dockerhub.com
一个Docker 镜像(image)包含了程序代码和程序运行所依赖的所有环境。 Docker 镜像一般存放在镜像仓库服务(Image Registry)里,默认的镜像仓库服务是Docker Hub。 用户可以制作、构建镜像、将镜像上传到镜像仓库服务,从而可以在其他地方从镜像仓库服务里搜索、拉取镜像。
2.3.1 搜索镜像
在官方的docker 仓库中搜索指定名称的docker镜像,也会有很多三方镜像。
执行docker search命令进行搜索
root@ubuntu2204:~# docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
root@ubuntu2204:~#
说明:
OFFICIAL: 官方
AUTOMATED: 使用第三方docker服务来帮助编译镜像,可以在互联网上面直接拉取到镜像,减少了繁琐的
编译过程
root@ubuntu2204:~# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 17883 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 181
bitnami/nginx Bitnami nginx Docker Image 149 [OK]
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 71
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 22 [OK]
rancher/nginx-ingress-controller 11
kasmweb/nginx An Nginx image based off nginx:alpine and in… 4
ibmcom/nginx-ingress-controller Docker Image for IBM Cloud Private-CE (Commu… 4
bitnami/nginx-ldap-auth-daemon 3
bitnami/nginx-exporter 3
circleci/nginx This image is for internal use 2
rancher/nginx 2
rancher/nginx-ingress-controller-defaultbackend 2
vmware/nginx 2
rapidfort/nginx RapidFort optimized, hardened image for NGINX 2
bitnami/nginx-intel 1
wallarm/nginx-ingress-controller Kubernetes Ingress Controller with Wallarm e… 1
vmware/nginx-photon 1
rancher/nginx-conf 0
ibmcom/nginx-ingress-controller-ppc64le Docker Image for IBM Cloud Private-CE (Commu… 0
rapidfort/nginx-official RapidFort optimized, hardened image for NGIN… 0
ibmcom/nginx-ppc64le Docker image for nginx-ppc64le 0
rancher/nginx-ssl 0
rapidfort/nginx-ib RapidFort optimized, hardened image for NGIN… 0
continuumio/nginx-ingress-ws 0
#搜索点赞100个以上的镜像
#旧语法
root@ubuntu2204:~# docker search -s 1000 centos
unknown shorthand flag: 's' in -s
See 'docker search --help'.
#新语法
root@ubuntu2204:~# docker search --filter=stars=1000 centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos DEPRECATED; The official build of CentOS. 7448 [OK]
root@ubuntu2204:~#
2.3.2 下载镜像
root@ubuntu2204:~# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform capable
-q, --quiet Suppress verbose output
root@ubuntu2204:~#
NAME: 是镜像名,一般的形式 仓库服务器:端口/项目名称/镜像名称
:TAG: 即版本号,如果不指定:TAG,则下载最新版镜像
root@ubuntu2204:~# docker image pull library/hello-world
Using default tag: latest #默认下载最新版本
latest: Pulling from library/hello-world
2db29710123e: Pull complete #分层下载
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest #下载的完整地址
root@ubuntu2204:~#
#docker pull = docker image pull
镜像下载保存的路径:/var/lib/docker/overlay2/镜像ID
镜像下载完成后,会自动解压缩,比官网显示的可能会大很多。
root@ubuntu2204:~# ll /var/lib/docker/overlay2/
total 40
drwx--x--- 10 root root 4096 Dec 31 11:00 ./
drwx--x--- 13 root root 4096 Dec 31 07:42 ../
drwx--x--- 4 root root 4096 Dec 31 10:30 047fe8bd1187fe444b2212dd99c1a9ae780710f968bb550679e99334f6443f72/
drwx--x--- 4 root root 4096 Dec 31 10:30 32d74c26c3916805c7067bcaff2a0ca7981088c9d7cc4795d26ad5a720892281/
drwx--x--- 4 root root 4096 Dec 31 10:30 401fb3d55cfb4a6e021578a05b60cb587672e88f2f938a9719b236c842f29373/
drwx--x--- 4 root root 4096 Dec 31 10:30 639084468c6a4ac9e13fd22548f532cf6df5c20cd6abacdf04c8e06733726ac9/
drwx--x--- 3 root root 4096 Dec 31 11:00 655354c546a0f86bf6032164c909fb035e606794017842c94c479fb4fa825eaf/
drwx--x--- 3 root root 4096 Dec 31 10:30 abe8bfcf5ffaa63005b616a8a38507bcc9626416c16af32bb6c4e157f6141412/
drwx--x--- 4 root root 4096 Dec 31 10:30 b144f38d712dc76408923bc762b4ec215dad4b2e847826090bfb9f74030c2cf2/
drwx------ 2 root root 4096 Dec 31 11:00 l/
root@ubuntu2204:~# ls /var/lib/docker/overlay2/
047fe8bd1187fe444b2212dd99c1a9ae780710f968bb550679e99334f6443f72 655354c546a0f86bf6032164c909fb035e606794017842c94c479fb4fa825eaf
32d74c26c3916805c7067bcaff2a0ca7981088c9d7cc4795d26ad5a720892281 abe8bfcf5ffaa63005b616a8a38507bcc9626416c16af32bb6c4e157f6141412
401fb3d55cfb4a6e021578a05b60cb587672e88f2f938a9719b236c842f29373 b144f38d712dc76408923bc762b4ec215dad4b2e847826090bfb9f74030c2cf2
639084468c6a4ac9e13fd22548f532cf6df5c20cd6abacdf04c8e06733726ac9 l
root@ubuntu2204:~#
Alpine 操作系统是一个面向安全的轻型 Linux 发行版。它不同于通常 Linux 发行版,Alpine 采用了musl libc 和 busybox 以减小系统的体积和运行时资源消耗,但功能上比 busybox 又完善的多,因此得到开源社区越来越多的青睐。
注意:下载特定版本的镜像得先到 hub.docker.com查到指定版本的DIGEST。
root@ubuntu2204:~# du -sh /var/lib/docker/overlay2
150M /var/lib/docker/overlay2
root@ubuntu2204:~# docker search alpine
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
alpine A minimal Docker image based on Alpine Linux… 9549 [OK]
alpinelinux/docker-cli Simple and lightweight Alpine Linux image wi… 6
alpinelinux/gitlab-runner Alpine Linux gitlab-runner (supports more ar… 4
alpinelinux/alpine-gitlab-ci Build Alpine Linux packages with Gitlab CI 3
alpinelinux/gitlab-runner-helper Helper image container gitlab-runner-helper … 2
grafana/alpine Alpine Linux with ca-certificates package in… 2
alpinelinux/gitlab Alpine Linux based Gitlab image 2
alpinelinux/darkhttpd 1
alpinelinux/package-builder Container to build packages for a repository 1
rancher/alpine-git 1
alpinelinux/golang Build container for golang based on Alpine L… 1
alpinelinux/docker-alpine 0
alpinelinux/apkbuild-lint-tools Tools for linting APKBUILD files in a CI env… 0
alpinelinux/build-base Base image suitable for building packages wi… 0
alpinelinux/docker-abuild Dockerised abuild 0
alpinelinux/unbound 0
alpinelinux/mqtt-exec 0
alpinelinux/docker-compose docker-compose image based on Alpine Linux 0
alpinelinux/git-mirror-syncd 0
alpinelinux/mirror-status 0
alpinelinux/ansible Ansible in docker 0
alpinelinux/alpine-docker-gitlab Gitlab running on Alpine Linux 0
alpinelinux/alpine-drone-ci Build Alpine Linux packages with drone CI 0
alpinelinux/aports-qa-bot A Gitlab bot that gives feedback on aports m… 0
alpinelinux/rsyncd 0
root@ubuntu2204:~# docker pull alpine:3.16.2
3.16.2: Pulling from library/alpine
213ec9aee27d: Pull complete
Digest: sha256:65a2763f593ae85fab3b5406dc9e80f744ec5b449f269b699b5efd37a07ad32e
Status: Downloaded newer image for alpine:3.16.2
docker.io/library/alpine:3.16.2
root@ubuntu2204:~# du -sh /var/lib/docker/overlay2
156M /var/lib/docker/overlay2
root@ubuntu2204:~#
2.3.3 查看本地镜像
root@ubuntu2204:~# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet Only show image IDs
root@ubuntu2204:~#
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine 3.16.2 9c6f07244728 4 months ago 5.54MB
nginx latest 605c77e624dd 12 months ago 141MB
hello-world latest feb5d9fea6a5 15 months ago 13.3kB
root@ubuntu2204:~#
#执行结果的显示信息说明:
REPOSITORY #镜像所属的仓库名称
TAG #镜像版本号(标识符),默认为latest
IMAGE ID #镜像唯一ID标识,如果ID相同,说明是同一个镜像有多个名称
CREATED #镜像在仓库中被创建时间
VIRTUAL SIZE #镜像的大小
#显示完整的ImageID
root@ubuntu2204:~# docker images --no-trunc
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine 3.16.2 sha256:9c6f0724472873bb50a2ae67a9e7adcb57673a183cea8b06eb778dca859181b5 4 months ago 5.54MB
nginx latest sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85 12 months ago 141MB
hello-world latest sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412 15 months ago 13.3kB
#只查看指定REPOSITORY的镜像
root@ubuntu2204:~# docker images nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 12 months ago 141MB
#查看指定镜像的详细信息
root@ubuntu2204:~# docker image inspect nginx
2.3.4 镜像导出
利用docker save命令可以将从本地镜像导出为一个打包 tar文件,然后复制到其他服务器进行导入使用
root@ubuntu2204:~# docker save --help
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
-o, --output string Write to a file, instead of STDOUT
root@ubuntu2204:~#
#说明:
Docker save 使用IMAGE ID导出,在导入后的镜像没有REPOSITORY和TAG,显示为<none>
#导出为tar格式
docker save -o /path/file.tar IMAGE1 IMAGE2 ...
docker save IMAGE1 IMAGE2 ... > /path/file.tar
#导出为压缩格式
docker save IMAGE1 IMAGE2 ... | gzip > /path/file.tar.gz
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine 3.16.2 9c6f07244728 4 months ago 5.54MB
nginx latest 605c77e624dd 12 months ago 141MB
hello-world latest feb5d9fea6a5 15 months ago 13.3kB
root@ubuntu2204:~# docker save alpine:3.16.2 -o ./myimages.tar
root@ubuntu2204:~# ls
myimages.tar snap
root@ubuntu2204:~#
将所有镜像导入到一个文件中,此方法导入后可以看REPOSITORY和TAG
root@ubuntu2204:~# docker image save `docker image ls --format "{{.Repository}}:{{.Tag}}"` -o all.tar
root@ubuntu2204:~# ls
all.tar myimages.tar snap
root@ubuntu2204:~# docker image ls --format "{{.Repository}}:{{.Tag}}"
alpine:3.16.2
nginx:latest
hello-world:latest
root@ubuntu2204:~#
2.3.5 镜像导入
利用docker load命令可以将镜像导出的打包或压缩文件再导入
root@ubuntu2204:~# docker load --help
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
root@ubuntu2204:~#
#常见用法
docker load -i /path/file.tar
docker load < /path/file.tar
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine 3.16.2 9c6f07244728 4 months ago 5.54MB
root@ubuntu2204:~# ls
all.tar myimages.tar snap
root@ubuntu2204:~# docker load -i ./all.tar
Loaded image: alpine:3.16.2
2edcec3590a4: Loading layer [==================================================>] 83.86MB/83.86MB
e379e8aedd4d: Loading layer [==================================================>] 62MB/62MB
b8d6e692a25e: Loading layer [==================================================>] 3.072kB/3.072kB
f1db227348d0: Loading layer [==================================================>] 4.096kB/4.096kB
32ce5f6a5106: Loading layer [==================================================>] 3.584kB/3.584kB
d874fd2bc83b: Loading layer [==================================================>] 7.168kB/7.168kB
Loaded image: nginx:latest
e07ee1baac5f: Loading layer [==================================================>] 14.85kB/14.85kB
Loaded image: hello-world:latest
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine 3.16.2 9c6f07244728 4 months ago 5.54MB
nginx latest 605c77e624dd 12 months ago 141MB
hello-world latest feb5d9fea6a5 15 months ago 13.3kB
root@ubuntu2204:~#
2.3.6 删除镜像
docker rmi 命令可以删除本地镜像
root@ubuntu2204:~# docker rmi --help
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
root@ubuntu2204:~#
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine 3.16.2 9c6f07244728 4 months ago 5.54MB
nginx latest 605c77e624dd 12 months ago 141MB
hello-world latest feb5d9fea6a5 15 months ago 13.3kB
root@ubuntu2204:~# docker rmi -f nginx
Untagged: nginx:latest
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine 3.16.2 9c6f07244728 4 months ago 5.54MB
hello-world latest feb5d9fea6a5 15 months ago 13.3kB
root@ubuntu2204:~#
删除所有镜像:
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine 3.16.2 9c6f07244728 4 months ago 5.54MB
hello-world latest feb5d9fea6a5 15 months ago 13.3kB
root@ubuntu2204:~# docker rmi -f `docker images -q`
Untagged: alpine:3.16.2
Untagged: alpine@sha256:65a2763f593ae85fab3b5406dc9e80f744ec5b449f269b699b5efd37a07ad32e
Deleted: sha256:9c6f0724472873bb50a2ae67a9e7adcb57673a183cea8b06eb778dca859181b5
Deleted: sha256:994393dc58e7931862558d06e46aa2bb17487044f670f310dffe1d24e4d1eec7
Untagged: hello-world:latest
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
root@ubuntu2204:~#
2.3.7 镜像打标签
docker tag 可以给镜像打标签,类似于起别名,但通常要遵守一定的命名规范,才可以上传到指定的仓库
root@ubuntu2204:~# docker tag --help
Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
root@ubuntu2204:~#
#TARGET_IMAGE[:TAG]格式一般形式
仓库主机FQDN或IP[:端口]/项目名(或用户名)/image名字:版本
TAG默认为latest
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
root@ubuntu2204:~# ls
all.tar myimages.tar snap
root@ubuntu2204:~# docker load -i ./all.tar
994393dc58e7: Loading layer [==================================================>] 5.827MB/5.827MB
Loaded image: alpine:3.16.2
2edcec3590a4: Loading layer [==================================================>] 83.86MB/83.86MB
e379e8aedd4d: Loading layer [==================================================>] 62MB/62MB
b8d6e692a25e: Loading layer [==================================================>] 3.072kB/3.072kB
f1db227348d0: Loading layer [==================================================>] 4.096kB/4.096kB
32ce5f6a5106: Loading layer [==================================================>] 3.584kB/3.584kB
d874fd2bc83b: Loading layer [==================================================>] 7.168kB/7.168kB
Loaded image: nginx:latest
e07ee1baac5f: Loading layer [==================================================>] 14.85kB/14.85kB
Loaded image: hello-world:latest
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine 3.16.2 9c6f07244728 4 months ago 5.54MB
nginx latest 605c77e624dd 12 months ago 141MB
hello-world latest feb5d9fea6a5 15 months ago 13.3kB
root@ubuntu2204:~# docker tag alpine alpine:3.11
Error response from daemon: No such image: alpine:latest
root@ubuntu2204:~# docker tag alpine:3.16.2 alpine:3.11
root@ubuntu2204:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine 3.11 9c6f07244728 4 months ago 5.54MB
alpine 3.16.2 9c6f07244728 4 months ago 5.54MB
nginx latest 605c77e624dd 12 months ago 141MB
hello-world latest feb5d9fea6a5 15 months ago 13.3kB
root@ubuntu2204:~#
2.3.8 Docker镜像命令总结
docker image 操作(1): 从Docker Hub 拉取已有镜像
docker image pull library/hello-world
docker image 操作(2): 列举所有镜像
docker image ls
docker image 操作(3): 查看某个镜像的配置信息
docker image inspect hello-world
docker image 操作(4): 移除镜像
docker image ls
docker image rm 名称/ID
使用Linux命令批量删除:docker ps -a | grep “Exited” | awk ‘{print $1 }’|xargs docker rm
docker image 操作(5): 查看所有的镜像操作命令
docker image
docker search centos #搜索指定镜像
docker pull alpine #下载指定镜像
docker images #查看所有本地镜像
docker save > /opt/centos.tar #centos #导出镜像
docker load -i /opt/centos.tar #导入本地镜像
docker rmi 镜像ID/镜像名称 #删除指定ID的镜像,此镜像对应容器正启动镜像不能被删除,除非将容器
全部关闭