Docker 常用命令
查看docker的事件
# 命令:docker events
2020-10-17T20:54:40.000000000+08:00 c7096ee78e8c8ea8f7656ab713c5e9db428560e5d15b84c67fd1948cb3b75ed1: (from centos) die
2020-10-17T20:55:00.000000000+08:00 a2cd3339a0a8a7634883d0cd1d22af375107f91cb56207c80db07cb168baae19: (from centos) create
2020-10-17T20:55:00.000000000+08:00 a2cd3339a0a8a7634883d0cd1d22af375107f91cb56207c80db07cb168baae19: (from centos) start
运行容器
docker run [options] image
#参数说明
--name NAME 容器名称
-d 后台运行
-it 交互凡是运行
-p 指定容器端口
-P 随机指定端口
列出运行的容器
# 命令:docker ps
# 退出容器的终端
exit
CTRL + P + Q # 退出不停止
删除容器
docker rm 容器id # -f 强制删除运行中的容器
docker rm $(docker ps -qa) # 删除全部的容器
docker ps -qa | xargs docker rm -f
启动和停止容器
docker start containerID # 开始容器
docker restart containerID # 重启容器
docker stop containerID # 停止容器
docker kill containerID # 强制停止容器
常用的其他命令
后台启动容器
# 命令 docker run -d image_name
# docker ps, 发现centos 停止了
# docker容器是后台运行,必须要有一个前台进程,当docker发现没有应用时,就会自动停止
查看日志
# 命令:docker logs [option] containerID
-f # follow log output
-t # show timestamps
--tail # number of lines to show
查看容器中的进程
# 命令 docker top containerID
UID PID PPID C STIME TTY TIME CMD
root 63767 43602 0 20:12 pts/2 00:00:00 /bin/bash
# PPID 父进程PID 即是宿主机运行docker的进程号
查看容器的详细的信息
# 命令 docker inspect containerID
[
{
"Id": "db16e35acf96776d2e00ee2159cebdd22df82595513c37dc60d288f8040fdb17",
"Created": "2020-10-17T12:00:21.288693732Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true;do echo hello_world;sleep 3;done;"
],
"State": {
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 137,
"Error": "",
"StartedAt": "2020-10-17T12:03:29.483814936Z",
"FinishedAt": "2020-10-17T12:19:30.750265748Z"
},
"Image": "375e2d4d2a24f3a34d30cf124af257868ddb060d87bb917e673d109a65db1ec0",
"NetworkSettings": {
"Bridge": "",
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"HairpinMode": false,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"MacAddress": "",
"NetworkID": "",
"PortMapping": null,
"Ports": null,
"SandboxKey": "",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null
},
"ResolvConfPath": "/var/lib/docker/containers/db16e35acf96776d2e00ee2159cebdd22df82595513c37dc60d288f8040fdb17/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/db16e35acf96776d2e00ee2159cebdd22df82595513c37dc60d288f8040fdb17/hostname",
"HostsPath": "/var/lib/docker/containers/db16e35acf96776d2e00ee2159cebdd22df82595513c37dc60d288f8040fdb17/hosts",
"LogPath": "/var/lib/docker/containers/db16e35acf96776d2e00ee2159cebdd22df82595513c37dc60d288f8040fdb17/db16e35acf96776d2e00ee2159cebdd22df82595513c37dc60d288f8040fdb17-json.log",
"Name": "/mad_franklin",
"RestartCount": 0,
"Driver": "devicemapper",
"ExecDriver": "native-0.2",
"MountLabel": "",
"ProcessLabel": "",
"Volumes": {},
"VolumesRW": {},
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LxcConf": [],
"Memory": 0,
"MemorySwap": 0,
"CpuShares": 0,
"CpuPeriod": 0,
"CpusetCpus": "",
"CpusetMems": "",
"CpuQuota": 0,
"BlkioWeight": 0,
"OomKillDisable": false,
"Privileged": false,
"PortBindings": {},
"Links": null,
"PublishAllPorts": false,
"Dns": null,
"DnsSearch": null,
"ExtraHosts": null,
"VolumesFrom": null,
"Devices": [],
"NetworkMode": "bridge",
"IpcMode": "",
"PidMode": "",
"UTSMode": "",
"CapAdd": null,
"CapDrop": null,
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"SecurityOpt": null,
"ReadonlyRootfs": false,
"Ulimits": null,
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"CgroupParent": ""
},
"Config": {
"Hostname": "db16e35acf96",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"PortSpecs": null,
"ExposedPorts": null,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash",
"-c",
"while true;do echo hello_world;sleep 3;done;"
],
"Image": "centos",
"Volumes": null,
"VolumeDriver": "",
"WorkingDir": "",
"Entrypoint": null,
"NetworkDisabled": false,
"MacAddress": "",
"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"
}
}
}
]
进入当前运行的容器
# 命令1:docker exec -it containerID CMD # 进入容器后开启一个新的终端
# 命令2:docker attach containerID # 进入容器执行当前正在执行的终端
从容器内拷贝文件到主机上
# 命令:docker cp containerID:/DIR/FILE /HOSTDIR
提交一个自定义的镜像
# 命令:docker commit -a AUTH -m "COMMENT" containerID IMAGE_NAME:TAG
docker commit -a "pigeast" -m "add webapps app"