Docker | 常用命令——排错很有帮助( 二 )

2.3、删除镜像docker rmi -f [镜像名、镜像ID]
docker rmi -f [镜像名、镜像ID]docker rmi -f 1d7aba917169Untagged: mysql:5.7Untagged: mysql@sha256:d9b934cdf6826629f8d02ea01f28b2c4ddb1ae27c32664b14867324b3e5e1291Deleted: sha256:1d7aba9171693947d53f474014821972bf25d72b7d143ce4af4c8d8484623417Deleted: sha256:94ebbead5c58282fef91cc7d0fb56e4006a72434b4a6ae2cd5be98f369cb8c21Deleted: sha256:989da5efad29ec59bd536cd393d277bc777f8b9b34b8e3ad9593a4b0a83b40f4Deleted: sha256:7457ee6817c678da3cb383d27a3d79d5f3f25fbcb92958d5e8d5709e7631e23cDeleted: sha256:fe7dac53adebe33519b4e4fc577bfcddd7372cc313c35fae681fc82fb325fdc03、容器命令有了镜像才可以创建容器,下载centos镜像进行学习
docker pull centos3.1、新建容器并启动docker run [可选参数] imageName/imageId# 参数说明--name "NAME"容器名字 , tomcat01, tomcat02, 用来区分容器-d后台方式运行-i, -t#使用交互模式运行,可以进入容器查看内容-p#指定容器端口 -p 8080-p ip:主机端口:容器端口-p 3344:80-p 主机端口:容器端口(常用)-p 容器端口容器端口-P#随机指定端口3.2、启动容器 , 并进入容器[root@iZm5e----- ~]# docker run -it centos /bin/bash[root@c4a1504a9cf9 /]# ls#查看容器内的centosbindevetchomeliblib64lost+foundmediamntoptprocrootrunsbinsrvsystmpusrvar[root@c4a1504a9cf9 /]# exit # 退出容器exit[root@iZm5e----- ~]#

Docker | 常用命令——排错很有帮助

文章插图
CONTAINER ID c4a1504a9cf9
3.3、查看所有运行的容器docker ps# 查看运行的容器exit之后就停止了容器docker ps -a# 查看所有运行过的以及在运行的容器docker ps -aCONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMESc4a1504a9cf9centos"/bin/bash"7 minutes agoExited (0) 3 minutes agohungry_eulerdc0c5076048ehello-world"/hello"2 months agoExited (0) 2 months agobusy_coridocker ps -a -n=[数字]# 最近创建的容器docker ps -a -n=1CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMESc4a1504a9cf9centos"/bin/bash"11 minutes agoExited (0) 7 minutes agohungry_euler3.4、退出容器exit#容器停止并退出ctrl + p + q#容器不停止退出3.5、删除容器docker rm 容器ID# 删除指定容器不能删除正在运行的容器docker rm -f $(docker ps -aq)# 递归删除所有容器docker ps -a -q|xargs docker rm# 删除所有容器3.6、启动和停止容器docker start 容器ID # 启动容器docker restart 容器ID# 重启容器docker stop 容器ID# 停止当前正在运行的容器docker kill 容器ID# 强制停止当前容器4、常用其它命令4.1、后台运行容器docker run -d centos463f1dc43e444ff6b6d0efd3f0c5047e118ddf64deed3aea2a8b5a6c89df61bb[root@iZm5e----- ~]# docker psCONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES[root@iZm5e----- ~]#发现后台运行的容器,停止了
docker使用后台运行,就必须要有一个前台进程 。否则,docker发现没有服务 , 就会自动停止
4.2、查看日志docker logs --help#shell脚本造日志docker run -d centos /bin/sh -c "while true;do echo xiao;sleep 1;done"d5033adf13b49f537430fc19df9fe00b477301dfd3a2d9c287060c65c1a59d7b[root@iZm5e----- ~]# docker psCONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMESd5033adf13b4centos"/bin/sh -c 'while t…"5 seconds agoUp 4 secondscompetent_jang[root@iZm5e----- ~]# #显示日志docker logs -f -t --tail 10 d5033adf13b42021-09-27T07:05:08.800676670Z xiao2021-09-27T07:05:09.803226803Z xiao2021-09-27T07:05:10.806019019Z xiao2021-09-27T07:05:11.809012200Z xiao2021-09-27T07:05:12.811505073Z xiao2021-09-27T07:05:13.817109532Z xiao2021-09-27T07:05:14.819543508Z xiao2021-09-27T07:05:15.822718210Z xiao...-tf#显示日志--tail [数字]# 显示日志的条数4.3、查看容器中的进程信息 (ps - linux)docker top命令
docker top 容器IDdocker top d5033adf13b4UIDPIDPPIDCSTIMETTYTIMECMDroot2908729069015:01?00:00:00/bin/sh -c while true;do echo xiao;sleep 1;doneroot2990829087015:08?00:00:00/usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1[root@iZm5e----- ~]#4.4、查看镜像的源数据dockers inspect 命令
Docker | 常用命令——排错很有帮助

文章插图
docker inspect d5033adf13b4[{"Id": "d5033adf13b49f537430fc19df9fe00b477301dfd3a2d9c287060c65c1a59d7b","Created": "2021-09-27T07:01:42.833109605Z","Path": "/bin/sh","Args": ["-c","while true;do echo xiao;sleep 1;done"],"State": {"Status": "running","Running": true,"Paused": false,"Restarting": false,"OOMKilled": false,"Dead": false,"Pid": 29087,"ExitCode": 0,"Error": "","StartedAt": "2021-09-27T07:01:43.288158158Z","FinishedAt": "0001-01-01T00:00:00Z"},"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6","ResolvConfPath": "/var/lib/docker/containers/d5033adf13b49f537430fc19df9fe00b477301dfd3a2d9c287060c65c1a59d7b/resolv.conf","HostnamePath": "/var/lib/docker/containers/d5033adf13b49f537430fc19df9fe00b477301dfd3a2d9c287060c65c1a59d7b/hostname","HostsPath": "/var/lib/docker/containers/d5033adf13b49f537430fc19df9fe00b477301dfd3a2d9c287060c65c1a59d7b/hosts","LogPath": "/var/lib/docker/containers/d5033adf13b49f537430fc19df9fe00b477301dfd3a2d9c287060c65c1a59d7b/d5033adf13b49f537430fc19df9fe00b477301dfd3a2d9c287060c65c1a59d7b-json.log","Name": "/competent_jang","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,"CgroupnsMode": "host","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/9aec4e083e254075a863c62c5c54f7ad2d7321750b4384ff1e092e25c5dc257e-init/diff:/var/lib/docker/overlay2/d6e4be0166eac21b08ff7e34bb088bd7488086c5fd04603879424af35f8e6190/diff","MergedDir": "/var/lib/docker/overlay2/9aec4e083e254075a863c62c5c54f7ad2d7321750b4384ff1e092e25c5dc257e/merged","UpperDir": "/var/lib/docker/overlay2/9aec4e083e254075a863c62c5c54f7ad2d7321750b4384ff1e092e25c5dc257e/diff","WorkDir": "/var/lib/docker/overlay2/9aec4e083e254075a863c62c5c54f7ad2d7321750b4384ff1e092e25c5dc257e/work"},"Name": "overlay2"},"Mounts": [],"Config": {"Hostname": "d5033adf13b4","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","while true;do echo xiao;sleep 1;done"],"Image": "centos","Volumes": null,"WorkingDir": "","Entrypoint": null,"OnBuild": null,"Labels": {"org.label-schema.build-date": "20210915","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": "182c5e5a4f65a8c8ae6fffc733ef2ff02b9c7ea0e374331017cb651077fb77c6","HairpinMode": false,"LinkLocalIPv6Address": "","LinkLocalIPv6PrefixLen": 0,"Ports": {},"SandboxKey": "/var/run/docker/netns/182c5e5a4f65","SecondaryIPAddresses": null,"SecondaryIPv6Addresses": null,"EndpointID": "7dd5c90916bf7c472a88dc308498f198b59c9606c144c6d413b77ecc03e05cc1","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": "feafa30d4051f24353508959bd420fd163ad0c98d6b30ec8ff13b59a59552bb1","EndpointID": "7dd5c90916bf7c472a88dc308498f198b59c9606c144c6d413b77ecc03e05cc1","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}}}}][root@iZm5e----- ~]#

推荐阅读