Docker常用命令

帮助命令

docker version # 显示docker的版本信息 docker info # 显示docker的系统信息,包括镜像和容器的数量 docker --help #万能命令 帮助
帮助文档的地址:https://docs.docker.com/engine/reference/commandline/

镜像命令

docker images 查看所有本地的主机上的镜像

 [root@VM-8-13-centos ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 10 months ago 13.3kB 
# 解释
REPOSITORY 镜像的仓库源
TAG	 镜像的标签
IMAGE ID	 镜像的id
CREATED 镜像的创建时间
SIZE 		 镜像的大小
# 可选项
-a -all # 列出所有镜像
-q --quiet #只显示镜像的id

docker search 搜索镜像

 [root@VM-8-13-centos ~]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 12956 [OK] mariadb MariaDB Server is a high performing open sou… 4963 [OK] phpmyadmin phpMyAdmin - A web interface for MySQL and M… 592 [OK] percona Percona Server is a fork of the MySQL relati… 582 [OK] bitnami/mysql Bitnami MySQL Docker Image 72 [OK] 
# 可选项 ,通过收藏来过滤
--filter=starts=3000 搜索出来的镜像就是STARS大于3000的
 [root@VM-8-13-centos ~]# docker search mysql --filter=STARS=3000 NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 12960 [OK] mariadb MariaDB Server is a high performing open sou… 4965 [OK] 

docker pull 下载镜像

#下载镜像 docker pull 镜像名[:tag] tag就是版本 [root@VM-8-13-centos ~]# docker pull mysql Using default tag: latest # 如果不写tag,默认就是lastest latest: Pulling from library/mysql 72a69066d2fe: Pull complete # 分层下载,docker image的核心 93619dbc5b36: Pull complete 99da31dd6142: Pull complete 626033c43d70: Pull complete 37d5d7efb64e: Pull complete ac563158d721: Pull complete d2ba16033dad: Pull complete 688ba7d5c01a: Pull complete 00e060b6d11d: Pull complete 1c04857f594f: Pull complete 4d7cfa90e6ea: Pull complete e0431212d27d: Pull complete Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 # 签名 Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest #真实地址 #等价于它 docker pull mysql docker pull docker.io/library/mysql:latest 指定版本下载 docker pull mysql:5.7

Docker常用命令_第1张图片

docker rmi 删除镜像

[root@VM-8-13-centos ~]# docker rmi -f c20987f18b13 # 删除指定的容器 [root@VM-8-13-centos ~]# docker rmi -f $(docker images -aq) # 删除全部的容器

容器命令

说明:我们有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习
docker pull centos

新建容器并启动

docker run [可选参数] image # 参数说明 --name=“Name” 容器名字tomcat01 tomcat02 用来区分容器 -d 后台方式运行 -it 使用交互方式运行,进入容器查看内容 -p 指定容器的端口 -p 8080:8080 -p 主机端口:容器端口 -p 容器端口 -P 随机指定端口 # 测试,启动并进入容器 [root@VM-8-13-centos ~]# docker run -it centos /bin/bash [root@19ffcfddce56 /]# ls # 查看容器内的centos,基础版本,很多命令都是不完善的! bin etc lib lost+found mnt proc run srv tmp var dev home lib64 media opt root sbin sys usr [root@19ffcfddce56 /]# exit #退出容器 exit [root@VM-8-13-centos ~]

列出所有运行的容器

#列出当前运行的容器 [root@VM-8-13-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES # 列出当前+历史运行过的容器 [root@VM-8-13-centos ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 19ffcfddce56 centos “/bin/bash” 2 minutes ago Exited (0) About a minute ago sad_ganguly 65099bc65358 feb5d9fea6a5 “/hello” 7 days ago Exited (0) 7 days ago happy_haibt -n显示最近创建的容器 [root@VM-8-13-centos ~]# docker ps -n=1 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 19ffcfddce56 centos “/bin/bash” 4 minutes ago Exited (0) 2 minutes ago sad_ganguly -q 只显示容器的编号 [root@VM-8-13-centos ~]# docker ps -aq 19ffcfddce56 65099bc65358

退出容器

exit # 直接容器停止并退出 ctrl + p + q #容器不停止退出

删除容器

docker rm 容器id # 删除指定的容器,不能删除正在运行的容器,如果要强制删除, rm -f docker rm -f $(docker ps -aq) # 删除所有的容器 [root@VM-8-13-centos ~]# docker rm -f $(docker ps -aq) 136cd7f8b112 19ffcfddce56 65099bc65358 docker ps -aq | xargs docker rm #删除所有的容器

启动和停止容器的操作

docker start 容器id # 启动容器 docker restart 容器id # 重启容器 docker stop 容器id # 停止当前正在运行的容器 docker kill 容器id # 强制停止当前容器

常用其他命令

后台启动容器

#命令 docker run -d 镜像名! [root@VM-8-13-centos ~]# docker run -d centos # 问题 docker ps,发现centos停止了 # 常见的坑,docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止。 # nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了。
查看日志

docker logs -f -t --tail 容器没有日志 # 自己编写一段shell脚本 [root@VM-8-13-centos ~]# docker run -d centos /bin/sh -c “while true;do echo liming;sleep 1;done” [root@VM-8-13-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 56628b772430 centos “/bin/bash” 2 minutes ago Up 2 minutes mystifying_nobel # 显示日志 # -tf # 显示日志 # --tail number #要显示日志条数 [root@VM-8-13-centos ~]# docker logs -tf --tail 10 9160cb851905 2022-08-02T13:57:53.278630628Z liming 2022-08-02T13:57:54.280092736Z liming 2022-08-02T13:57:55.281597575Z liming 2022-08-02T13:57:56.283150074Z liming 2022-08-02T13:57:57.284534016Z liming 2022-08-02T13:57:58.285964706Z liming 2022-08-02T13:57:59.287500563Z liming 2022-08-02T13:58:00.289041811Z liming 2022-08-02T13:58:01.290569372Z liming

查看容器中进程信息ps

#命令 docker top 容器id [root@VM-8-13-centos ~]# docker top 9160cb851905 UID PID PPID C STIME TTY TIME CMD root 28458 28437 0 21:56 ? 00:00:00 /bin/sh -c while true;do echo liming;sleep 1;done root 29959 28458 0 22:01 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1

查看镜像的元数据

#命令 docker inspect 容器id [root@VM-8-13-centos ~]# docker inspect 9160cb851905 [ { “Id”: “9160cb85190523b731ec5541f17bef1ad4e81880118775de6a736a2a682e7188”, “Created”: “2022-08-02T13:56:59.936604379Z”, “Path”: “/bin/sh”, “Args”: [ “-c”, “while true;do echo liming;sleep 1;done” ], “State”: { “Status”: “running”, “Running”: true, “Paused”: false, “Restarting”: false, “OOMKilled”: false, “Dead”: false, “Pid”: 28458, “ExitCode”: 0, “Error”: “”, “StartedAt”: “2022-08-02T13:57:00.200423328Z”, “FinishedAt”: “0001-01-01T00:00:00Z” }, “Image”: “sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6”, “ResolvConfPath”: “/var/lib/docker/containers/9160cb85190523b731ec5541f17bef1ad4e81880118775de6a736a2a682e7188/resolv.conf”, “HostnamePath”: “/var/lib/docker/containers/9160cb85190523b731ec5541f17bef1ad4e81880118775de6a736a2a682e7188/hostname”, “HostsPath”: “/var/lib/docker/containers/9160cb85190523b731ec5541f17bef1ad4e81880118775de6a736a2a682e7188/hosts”, “LogPath”: “/var/lib/docker/containers/9160cb85190523b731ec5541f17bef1ad4e81880118775de6a736a2a682e7188/9160cb85190523b731ec5541f17bef1ad4e81880118775de6a736a2a682e7188-json.log”, “Name”: “/determined_ganguly”, “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/114dc5b2003b0d85f6359408074467b098831cb125d8ece02fc7665e4e43c8ca-init/diff:/var/lib/docker/overlay2/6f6367e35be28bcc21ade2cd3f267a773395b0b8237b8826d72dad451514fded/diff”, “MergedDir”: “/var/lib/docker/overlay2/114dc5b2003b0d85f6359408074467b098831cb125d8ece02fc7665e4e43c8ca/merged”, “UpperDir”: “/var/lib/docker/overlay2/114dc5b2003b0d85f6359408074467b098831cb125d8ece02fc7665e4e43c8ca/diff”, “WorkDir”: “/var/lib/docker/overlay2/114dc5b2003b0d85f6359408074467b098831cb125d8ece02fc7665e4e43c8ca/work” }, “Name”: “overlay2” }, “Mounts”: [], “Config”: { “Hostname”: “9160cb851905”, “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 liming;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”: “3db36d215d9eee6fcb72167c911ff1e0526363404473597ba0627f8b7bb4d9a2”, “HairpinMode”: false, “LinkLocalIPv6Address”: “”, “LinkLocalIPv6PrefixLen”: 0, “Ports”: {}, “SandboxKey”: “/var/run/docker/netns/3db36d215d9e”, “SecondaryIPAddresses”: null, “SecondaryIPv6Addresses”: null, “EndpointID”: “acb7c152174d241bb8ed121e8c16dc39ac8089800301810bd80f3363ebdcff6e”, “Gateway”: “172.17.0.1”, “GlobalIPv6Address”: “”, “GlobalIPv6PrefixLen”: 0, “IPAddress”: “172.17.0.3”, “IPPrefixLen”: 16, “IPv6Gateway”: “”, “MacAddress”: “02:42:ac:11:00:03”, “Networks”: { “bridge”: { “IPAMConfig”: null, “Links”: null, “Aliases”: null, “NetworkID”: “88d490c91ba56f36b607dd360da8da4e060c94efda412b11302d0e35c67932d9”, “EndpointID”: “acb7c152174d241bb8ed121e8c16dc39ac8089800301810bd80f3363ebdcff6e”, “Gateway”: “172.17.0.1”, “IPAddress”: “172.17.0.3”, “IPPrefixLen”: 16, “IPv6Gateway”: “”, “GlobalIPv6Address”: “”, “GlobalIPv6PrefixLen”: 0, “MacAddress”: “02:42:ac:11:00:03”, “DriverOpts”: null } } } } ]

进入当前正在运行的容器

#我们通常容器都是使用后台方式运行的,需要进入该容器,修改一些配置 # 命令 docker exec -it 容器 id bashShell [root@VM-8-13-centos ~]# docker exec -it dce2305b43 /bin/bash # 方式二 docker attach 容器id #测试 [root@VM-8-13-centos ~]# docker attach dce2305b43 正在执行当前的代码 # docker exec # 进入容器后开启一个新的终端,可以在里面操作 # docker attach # 进入容器正在执行的终端,不会启动新的进程。
从容器拷贝文件到主机上
docker cp 容器id:容器内路径 目的主机路径 [root@VM-8-13-centos home]# docker cp 5c970de73851:/home/test.java /home # 拷贝是一个手动过程,未来我们是用 -v 卷的技术,可以实现自动同步
Docker常用命令_第2张图片

你可能感兴趣的:(docker,容器,运维)