Docker的常用命令
帮助命令
docker version # 显示docker的版本信息
docker info # 显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help # 万能命令
帮助文档的地址: https://docs.docker.com/reference/
镜像命令
docker images 查看所有本地主机上的镜像
[root@su01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 10 months ago 13.3kB
#名词解释
REPOSITORY 仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
#可选项
-a, --all # 列出所有
-q, --quiet # 只显示镜像的id
docker search 搜索镜像
[root@su01 ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10139 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3734 [OK]
#可选项 通过搜索stars过滤
--filter=STARS=3000 # 搜索出来的镜像就是STARS大于3000的
docker pull 下载镜像
# docker pull 镜像名[:tag]
[root@su01 ~]# docker pull mysql
# 指定版本下载
[root@su01 ~]# docker pull mysql:5.7
docker rmi 删除镜像
#指定id
docker rmi -f id
#指定id,多个
docker rmi -f id1 id2 id3
#组合操作
docker rmi -f $(docker images -aq) # 先查后删
容器命令
说明: 有了镜像才可创建容器! linux, 下载一个 centos 镜像来测试学习** **(centos7里运行docker里运行centos8)
docker pull centos
新建容器并启动
docker run [可选参数] image
# 参数说明
--name='Name' #容器名字
-d #后台运行
-it #使用交互方式进行,进入容器查看内容
-p #指定容器端口 -p 8080:8080(主机端口:容器端口)
-P #随机指定端口
# 测试
[root@su01 ~]# docker run -it centos /bin/bash
[root@6a6bff5175a1 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 退出容器
[root@6a6bff5175a1 /]# exit
exit
[root@su01 ~]# cd /
[root@su01 /]# ls
bin dev home lib64 media opt proc run srv tmp var
boot etc lib lost+found mnt patch root sbin sys usr www
列出所有运行的容器
#正在运行的
[root@su01 /]# docker ps
#所有容器(正在运行+运行过的)
[root@su01 /]# docker ps -a
# 其余参数
docker ps -a n=1 #只显示一个(最近开始)
-q #只显示容器的编号
退出容器
exit #直接容器停止并退出
Ctrl+P+Q #容器不停止退出(注意大写)
删除容器
docker rm 容器id #删除指定容器,不能删除正在运行的容器,强制rm -f
docker rm -f $(docker ps -aq) #删除所有的容器
docker ps -a -q|xargs docker rm #删除所有容器
启动和停止容器的操作
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止容器
docker kill 容器id #杀死容器(容器启动状态才能杀死)
#run和start的区别:run是创建并启动容器(操作镜像),start是启动容器(操作容器id)
常用的其他命令
后台启动容器
# 命令 docker run -d 镜像
[root@su01 ~]# docker run -d centos
# 问题 docker ps 的时候发现 centos 停止了
# 常见的坑: docker 容器后台运行,就必须有一个前台进程,docker发现没有应用,就会自动停止
查看日志
[root@su01 /]# docker logs -tf --tail 显示数量 容器id
查看容器中进程信息
[root@su01 /]# docker top 33871aa9ee9a
UID PID PPID C STIME TTY TIME CMD
root 12205 12188 0 15:13 pts/0 00:00:00 /bin/bash
#名词
UID 当前用户id
PID 进程id
PPID 父进程id
查看镜像的元数据
#命令
docker inspect 容器id
[root@su01 /]# docker inspect 33871aa9ee9a
[
{
"Id": "33871aa9ee9acb223c59bd169f338c0cf438e1bd859aba2ecb1605128532987e",
"Created": "2020-11-09T07:13:17.288278093Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 12205,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-11-09T07:13:17.566282067Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",
"ResolvConfPath": "/var/lib/docker/containers/33871aa9ee9acb223c59bd169f338c0cf438e1bd859aba2ecb1605128532987e/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/33871aa9ee9acb223c59bd169f338c0cf438e1bd859aba2ecb1605128532987e/hostname",
"HostsPath": "/var/lib/docker/containers/33871aa9ee9acb223c59bd169f338c0cf438e1bd859aba2ecb1605128532987e/hosts",
"LogPath": "/var/lib/docker/containers/33871aa9ee9acb223c59bd169f338c0cf438e1bd859aba2ecb1605128532987e/33871aa9ee9acb223c59bd169f338c0cf438e1bd859aba2ecb1605128532987e-json.log",
"Name": "/laughing_chebyshev",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/8f39c856104fe11cffee6f80d128e4e619ccf2ebd72538479d6fe5519dc17d7d-init/diff:/var/lib/docker/overlay2/3e49b5228da5b28429451483109faa40e20be5c4a64b9427e647974af81ea035/diff",
"MergedDir": "/var/lib/docker/overlay2/8f39c856104fe11cffee6f80d128e4e619ccf2ebd72538479d6fe5519dc17d7d/merged",
"UpperDir": "/var/lib/docker/overlay2/8f39c856104fe11cffee6f80d128e4e619ccf2ebd72538479d6fe5519dc17d7d/diff",
"WorkDir": "/var/lib/docker/overlay2/8f39c856104fe11cffee6f80d128e4e619ccf2ebd72538479d6fe5519dc17d7d/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "33871aa9ee9a",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20200809",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "4f276548a86564d625671959fea0cfc0427e0ec2bbc271505b8e497b12351611",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/4f276548a865",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "7036acf9d4fe1920dc3c43fe03685458cdcfe69a9d4e6b5bd56c4cb50d10d6db",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "0c1ed7ac516d57adbbe5d75b0edee145cd3056921acd3e2af8c0294910173a63",
"EndpointID": "7036acf9d4fe1920dc3c43fe03685458cdcfe69a9d4e6b5bd56c4cb50d10d6db",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的容器
# 我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
# 方式一
# 命令
docker exec -it 容器id bashShell
# 测试
[root@su01 /]# docker exec -it 33871aa9ee9a /bin/bash
[root@33871aa9ee9a /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@33871aa9ee9a /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 07:13 pts/0 00:00:00 /bin/bash
root 14 0 0 07:42 pts/1 00:00:00 /bin/bash
root 28 14 0 07:42 pts/1 00:00:00 ps -ef
# 方式二
# 命令
docker attach 容器id
# 测试
[root@su01 /]# docker attach 33871aa9ee9a
正在执行当前的任务...
# exec和attach对比
exec 进入容器后开启一个新的终端,可以在里面操作(常用)
attach 进入容器正在执行的终端,不会启动新的进程
从容器内拷贝文件到宿主机上
docker cp 容器id:容器内路径 目的主机路径
# 今日容器内,新建文件
[root@su01 home]# docker attach f0ed56123ff5
[root@f0ed56123ff5 /]# cd /home
[root@f0ed56123ff5 home]# touch test.py
[root@f0ed56123ff5 home]# ls
test.py
# 退出容器,将容器内的文件拷贝到主机
[root@f0ed56123ff5 home]# exit
exit
[root@su01 home]# docker cp f0ed56123ff5:/home/test.py /home
[root@su01 home]# ls
redis test.py www
# 拷贝是一个手动过程,未来我们使用 -v 卷的技术,可以实现自动同步
小结
attach # 当前shell下attach连接指定运行的容器
build # 通过Dockerfile定制镜像
commit # 提交当前容器为新的镜像
cp # 从容器中拷贝指定文件或者目录到宿主机上
create # 创建一个新的容器,同run,但不启动
diff # 查看docker容器变化
events # 从docker服务器获取容器实时事件
exec # 在已存在的容器上运行命令
export # 导出容器的内容流作为一个tar归档文件(对应import)
history # 展示一个镜像的形成历史
images # 列出系统当前镜像
import # 从tar包中的内容创建一个新的文件系统映像(对应export)
info # 显示系统相关信息
inspect # 查看容器详细信息
kill # kill指定正在运行的docker容器
load # 从一个tar包中加载一个镜像(对应save)
login # 注册或登录一个docker源服务器
logout # 从当前Docker registry退出
logs # 输出当前容器日志信息
port # 查看映射端口对应的容器内部源端口
pause # 暂停容器
ps # 列出容器列表
pull # 从docker镜像源服务器拉取指定镜像或库镜像
push # 推动指定镜像或库镜像到docker源服务器
restart # 重启运行的容器
rm # 移除一个/多个容器
rmi # 移除一个/多个镜像(无容器使用该镜像才可删除)
run # 创建一个容器并运行一个命令
save # 保存一个镜像为一个tar包(对应load)
search # 在docker hub中搜索镜像
start # 启动容器
stop # 停止容器
tag # 给仓库源中的镜像打标签
top # 查看容器中运行的进程信息
unpause # 取消暂停容器
version # 查看docker版本号
wait # 截取容器停止时的退出状态值