一.镜像操作
1.搜索官方仓库镜像
(1)示例
(2)可选参数
(3)各列参数含义
2.拉取镜像
(1)docker pull从docker镜像仓库获取镜像,添加-a参数表示拉去所有匹配的镜像
(2)标记本地镜像,将它放进某一仓库
(3)查看镜像层数
3.查看当前主机镜像列表
(1)查看,默认只显示顶层
(2)显示没有仓库名,没有标签的“虚悬镜像”,这些镜像可以放心删除
(3)显示包括中间层镜像在内的所有镜像,中间层镜像不能删除
(4)查看镜像的详细信息
(5)其他可选参数
4.导出镜像
5.删除镜像
(1)指定拉取时的名称,也可以通过ID删除
(2)可选参数
6.导入镜像
二.容器操作
1.启动容器
(1)docker run 镜像名,container可省略
(2)可选参数
2.关闭容器
3.激活关闭的容器
(1)docker start/restart 容器名
(2)可选参数
4.查看容器详细信息
5.测试容器并查看日志
6.对运行中的容器执行操作
(1)docker exec 容器名
(2)可选参数
7.删除容器
(1)docker kill 容器名
(2)docker rm (-f)容器名,(强制)删除容器
(3)docker rm -f `docker ps -a -q`,强制删除所有容器
(4)docker container prune,删除处于终止状态的容器
8.管理容器内数据
(1)创建数据卷
(2)查看指定数据卷的信息
(3) 启动一个挂载数据卷的容器
(4)删除无主数据卷
三.查看镜像、容器、数据卷占用空间
[root@localhost ~]# docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 4512 [OK]
clearlinux/httpd httpd HyperText Transfer Protocol (HTTP) ser… 5
paketobuildpacks/httpd 0
vulhub/httpd 0
jitesoft/httpd Apache httpd on Alpine linux. 0
openquantumsafe/httpd Demo of post-quantum cryptography in Apache … 0
wodby/httpd 0
avenga/httpd-static 0
dockette/httpdump 0
betterweb/httpd 0
dockette/apache Apache / HTTPD 1 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 45
manageiq/httpd Container with httpd, built on CentOS for Ma… 1 [OK]
centos/httpd-24-centos8 1
dockerpinata/httpd 1
19022021/httpd-connection_test This httpd image will test the connectivity … 0
publici/httpd httpd:latest 1 [OK]
centos/httpd 36 [OK]
httpdocker/kubia 0
e2eteam/httpd 0
manasip/httpd 0
httpdss/archerysec ArcherySec repository 0 [OK]
solsson/httpd-openidc mod_auth_openidc on official httpd image, ve… 2 [OK]
patrickha/httpd-err 0
hypoport/httpd-cgi httpd-cgi 2 [OK]
--automated | 只列出automated build类型的镜像 |
--no-trunc | 显示完整的镜像描述 |
-f <匹配条件> | 只列出收藏数不小于条件的镜像 |
NAME | 镜像名称 |
DESCRIPTION | 镜像说明 |
STARS | 点赞数量,越多越受欢迎 |
OFFICIAL | OK表示是docker官方发布的 |
AUTOMATED | 是否是自动构建的 |
[root@localhost ~]# docker pull nginx:1.14-alpine #alpine表示构建容器小镜像的发行版本
1.14-alpine: Pulling from library/nginx
bdf0201b3a05: Pull complete
3d0a573c81ed: Pull complete
8129faeb2eb6: Pull complete
3dc99f571daf: Pull complete
Digest: sha256:485b610fefec7ff6c463ced9623314a04ed67e3945b9c08d7e53a47f6d108dc7
Status: Downloaded newer image for nginx:1.14-alpine
docker.io/library/nginx:1.14-alpine
[root@localhost ~]# docker tag nginx:1.14-alpine /data/nginx:v1 #tag指定镜像标签
[root@localhost ~]# docker history nginx:1.14-alpine
IMAGE CREATED CREATED BY SIZE COMMENT
8a2fb25a19f5 4 years ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
4 years ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B
4 years ago /bin/sh -c #(nop) EXPOSE 80 0B
4 years ago /bin/sh -c #(nop) COPY file:ebf4f0eb33621cc0… 1.09kB
4 years ago /bin/sh -c #(nop) COPY file:4c82b9f10b84c567… 643B
4 years ago /bin/sh -c GPG_KEYS=B0F4253373F8F6F510D42178… 10.5MB
4 years ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.14.2 0B
4 years ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B
4 years ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
4 years ago /bin/sh -c #(nop) ADD file:2e3a37883f56a4a27… 5.53MB
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.14-alpine 8a2fb25a19f5 4 years ago 16MB
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.14-alpine 8a2fb25a19f5 4 years ago 16MB
[root@localhost ~]# docker image ls nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.14-alpine 8a2fb25a19f5 4 years ago 16MB
[root@localhost ~]# docker image --help
Usage: docker image COMMAND
Manage images
Commands:
build Build an image from a Dockerfile
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Display detailed information on one or more images
load Load an image from a tar archive or STDIN
ls List images
prune Remove unused images
pull Download an image from a registry
push Upload an image to a registry
rm Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
REPOSITORY | 镜像的仓库源 |
TAG | 镜像的标签 |
IMAGE ID | 镜像ID |
CREATED | 镜像创建时间 |
SIZE | 镜像大小 |
镜像大小说明:docker image ls显示的是镜像拉去到本地后展开的大小(即展开后各层占空间总合),不是在docker hub中的压缩状态的大小,而且这个大小并非四=是实际消耗的硬盘空间大小。
[root@localhost ~]# docker image ls -f dangling=true
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]# docker image prune #删除虚悬镜像
[root@localhost ~]# docker image ls -a
[root@localhost ~]# docker image inspect nginx:1.14-alpine
-q | 只显示镜像ID |
--no-trunc | 显示完整的镜像信息 |
--f <匹配条件> | 显示满足条件的镜像 |
使用“>”或“-o”参数指定导出镜像位置,需要导出的镜像可以为多个,在指定导出的文件名后缀为“.tar.gz”时可以让文件直接压缩
[root@localhost ~]# docker image save nginx > docker-nginx.tar.gz
[root@localhost ~]# ll
total 16860
-rw-r--r-- 1 root root 0 Jul 23 09:59 abc
-rw-------. 1 root root 1386 Jul 23 09:02 anaconda-ks.cfg
-rw-r--r-- 1 root root 17258496 Aug 20 20:25 docker-nginx.tar.gz
[root@localhost ~]# docker image rm nginx:1.14-alpine
#rm或rmi
-f | 强制删除 |
-l | 删除容器间的网络连接,并非容器本身 |
-v | 删除与容器关联的卷 |
-i指定先前导出的镜像文件,可加“-q”参数精简输出信息
[root@localhost ~]# docker image load -i docker-nginx.tar.gz
[root@localhost ~]# docker container --help
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
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
exec Execute a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Create and run a new container from an image
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
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
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.
[root@localhost ~]# docker container run -d -p 80:80 nginx:1.14-alpine
d2a903c987ac122186f950b55fc5716564aec096f5ad2376e285853638ce718c
[root@localhost ~]# docker ps #查询容器运行状态
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2a903c987ac nginx:1.14-alpine "nginx -g 'daemon of…" 10 seconds ago Up 9 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp dreamy_cerf
[root@localhost ~]# docker port dreamy_cerf #查看docker端口映射,这个名字在启动时没有指定时会自动创建一个,通过docker ps查询
80/tcp -> 0.0.0.0:80
80/tcp -> [::]:80
注:容器内的第一个进程必须一致处于运行状态,否则容器将退出
-t | 打开一个终端来使用容器 |
-i | 交互式访问 |
-d | 后台运行容器,返回容器ID,不后台运行则会一直占用终端 |
-p | 端口映射,将容器内服务的端口映射在宿主机的指定端口,“宿主机端口:容器端口” |
-P | 随机端口映射,容器内部端口随机映射到主机的端口 |
--name | 容器名字 |
--network | 指定网络 |
--rm | 容器停止即自动删除 |
docker stop/kill 容器名,kill可以像Linux杀进程一样使用信号控制(-s参数 信号)
-a | 附加到当前终端 |
-i | 交互式激活进入 |
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2a903c987ac nginx:1.14-alpine "nginx -g 'daemon of…" 24 minutes ago Up 24 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp dreamy_cerf
[root@localhost ~]# docker inspect dreamy_cerf
[root@localhost ~]# docker inspect dreamy_cerf | grep -i address
"LinkLocalIPv6Address": "",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"GlobalIPv6Address": "",
"IPAddress": "172.17.0.2",
"MacAddress": "02:42:ac:11:00:02",
"IPAddress": "172.17.0.2",
"GlobalIPv6Address": "",
"MacAddress": "02:42:ac:11:00:02",
[root@localhost ~]# curl 172.17.0.2
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@localhost ~]# docker logs dreamy_cerf
172.17.0.1 - - [20/Aug/2023:13:32:50 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
172.17.0.1 - - [20/Aug/2023:13:33:10 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
172.17.0.1 - - [20/Aug/2023:13:34:53 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
172.17.0.1 - - [20/Aug/2023:13:44:23 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
[root@localhost ~]# docker exec -it dreamy_cerf /bin/sh #指定运行环境
/ # ls /
bin etc lib mnt proc run srv tmp var
dev home media opt root sbin sys usr
/ # exit #exit退出
[root@localhost ~]# docker exec dreamy_cerf echo hello
hello
-d | 在后台运行命令 |
-e | 设置环境变量 |
-i | 交互式 |
-t | 打开一个终端 |
数据卷——类似于Linux文件系统中目录的挂载,docker镜像中被指定为挂载点的目录里的文件会隐藏,只显示挂载着数据卷。数据卷在容器间共享,可重用,数据卷的修改动态会立即生效,其更新不会影响到镜像。
[root@localhost ~]# docker volume create vol1
vol1
[root@localhost ~]# docker volume ls
DRIVER VOLUME NAME
local vol1
[root@localhost ~]# docker volume inspect vol1
[
{
"CreatedAt": "2023-08-20T21:58:06+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/vol1/_data",
"Name": "vol1",
"Options": null,
"Scope": "local"
}
]
[root@localhost ~]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 1 1 16.03MB 0B (0%)
Containers 5 2 14B 0B (0%)
Local Volumes 1 1 1.149kB 0B (0%)
Build Cache 0 0 0B 0B
[root@localhost ~]# docker run -d -P --name web -v vol1:/usr/share/nginx/html nginx:1.14-alpine
4de8823f02ebc9b2e9e687e4d2e9888ac66c97ad014e4b40fff18f9be5bfcf84
#通过-v指定数据卷
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4de8823f02eb nginx:1.14-alpine "nginx -g 'daemon of…" 37 seconds ago Up 37 seconds 0.0.0.0:32768->80/tcp, :::32768->80/tcp web
[root@localhost ~]# docker rm -f web #先删除容器再删除数据卷,也可以加“-v”参数在删除容器同时删除数据卷
[root@localhost ~]# docker volume rm my-vol
[root@localhost ~]# docker volume prune
[root@localhost ~]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 1 1 16.03MB 0B (0%) #镜像
Containers 5 2 14B 0B (0%) #容器
Local Volumes 1 1 1.149kB 0B (0%) #数据卷
Build Cache 0 0 0B 0B