Docker 基本命令
一、帮助命令
帮助文档地址:https://docs.docker.com/reference/
docker version # 显示docker的版本信息 docker info # 显示docker的系统信息,包括镜像和容器的数量 dockers 命令 --help # 帮助命令
二、镜像命令
1、查看镜像
查看所有本地的主机上的镜像
docker images
#可选项
-a, --all # 列出所有镜像
-q, --quiet # 只显示镜像的id
#解释
REPOSITORY # 镜像的仓库源
TAG # 镜像的标签
IMAGE ID # 镜像的id
CREATED # 镜像的创建时间
SIZE # 镜像的大小
2、搜索镜像
docker search <镜像名>
# 可选项 通过搜索来过滤
--filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的
--filter=STARRS=5000 #搜索出来的镜像就是STARS大于5000的
3、下载镜像
docker pull <镜像名> docker pull 镜像名[:tag] #tag 版本号 # 下载镜像 # 等价与它 docker pull mysql docker pull docker.io/library/mysql:latest
指定版本下载
docker pull mysql:5.7
4、删除镜像
docker rmi <镜像ID> OR <镜像名>
docker rmi -f a3d35804fa37 # 删除指定的镜像
docker rmi -f 镜像id 镜像id 镜像id # 删除多个镜像
docker rmi -f $(docker images -aq) # 删除全部镜像
三、容器命令
有了镜像才能创建容器,下载centos测试学习
1、新建容器并启动
docker pull centos
docker run [可选参数] image #参数说明 --name=<容器名> 容器名 tomcat01 tomact02 ,用来区分容器 -d 后台方式运行 -it 使用交互方式运行,进入容器查看内容 -p 指定容器的端口 -p -p ip:主机端口:容器端口 -p 主机端口:容器端口(常用) -p 容器端口 容器端口 -P(大写) 随机指定端口
2、启动并进入容器
docker run -it centos /bin/bash
3、列出所有的运行的容器
docker ps [参数] # 列出当前正在运行的容器 -a # 列出当前正在运行的容器加带出历史运行过的容器 -n=<数字> # 显示最近创建的n个容器 -q # 只显示容器的编号
4、退出容器
exti # 直接停止并退出容器 Ctrl + p + q # 容器不停止退出
5、删除容器
docker rm <容器id>OR<容器名> # 删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -f docker rm -f $(docker ps -aq) # 删除所有的容器 docker ps -a -q|xargs docker rm # 删除所有的容器
6、启动和停止容器
docker start <容器id>OR<容器名> # 启动容器 docker restarrt <容器id>OR<容器名> # 重启容器 docker stop <容器id>OR<容器名> # 停止当前正在运行的容器 docker kill <容器id>OR<容器名> # 强制停止当前容器
四、常用其他命令
1、后台启动容器
docker run -d <镜像名>OR<容器名>
docker ps 发现CentOS停止了
常见的坑 Docker 容器使用后台运行,就必须要有一个前台进程,Docker发现没有应用,就会自动停止。
例:Nginx 容器启动后,发现在即没有提供服务,就会立刻停止,就是没有程序了。
2、查看日志
docker logs [参数1 参数2 ..] <容器id> -tf # 显示日志 --tail <n> # 显示当前最新日志的条数
编写shell脚本测试 docker run -d centos /bin/sh -c "while true;do echo liang;sleep 1;done"
显示日志
docker logs -tf --tail 10 373e92edc826 # 显示当前最新10条日志
3、进入当前正在运行的容器
我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置。
方式一:
docker exec -it <容器id>OR<容器名> /bin/bash
方式二:
docker attach <容器id>OR<容器名>
区别:
docker exec # 进入容器后开启一个新的终端,可以在里面进行操作(常用)
docker attach # 进入容器正在执行的终端,不会启动新的进程
4、从容器内拷贝文件到主机
docker cp <容器id>:<容器内路径> <目的主机路径>
说明:
拷贝是一个手动过程,未来我们使用 -v 卷技术,可以实现自动同步。
5、查看容器中的进程信息
docker top <容器id>
6、查看镜像的元数据
docker inspect <容器id>
[root@docker ~]# docker inspect 373e92edc826 [ { "Id": "373e92edc826ef0b62d2a61dce26ef06aaf49c86c6b762d2d84920576176b68e", "Created": "2022-05-18T01:47:10.851259487Z", "Path": "/bin/sh", "Args": [ "-c", "while true;do echo liang;sleep 1;done" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 7700, "ExitCode": 0, "Error": "", "StartedAt": "2022-05-18T01:47:11.255556991Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6", "ResolvConfPath": "/var/lib/docker/containers/373e92edc826ef0b62d2a61dce26ef06aaf49c86c6b762d2d84920576176b68e/resolv.conf", "HostnamePath": "/var/lib/docker/containers/373e92edc826ef0b62d2a61dce26ef06aaf49c86c6b762d2d84920576176b68e/hostname", "HostsPath": "/var/lib/docker/containers/373e92edc826ef0b62d2a61dce26ef06aaf49c86c6b762d2d84920576176b68e/hosts", "LogPath": "/var/lib/docker/containers/373e92edc826ef0b62d2a61dce26ef06aaf49c86c6b762d2d84920576176b68e/373e92edc826ef0b62d2a61dce26ef06aaf49c86c6b762d2d84920576176b68e-json.log", "Name": "/nervous_keldysh", "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/389441b3281eb117bfb1ba69ea1f4031e21cd2950079dd071ad407c32af9ef7c-init/diff:/var/lib/docker/overlay2/dc68c40ac76be778527691427161aac43091109b380a4768bc4cb05e4a149ae7/diff", "MergedDir": "/var/lib/docker/overlay2/389441b3281eb117bfb1ba69ea1f4031e21cd2950079dd071ad407c32af9ef7c/merged", "UpperDir": "/var/lib/docker/overlay2/389441b3281eb117bfb1ba69ea1f4031e21cd2950079dd071ad407c32af9ef7c/diff", "WorkDir": "/var/lib/docker/overlay2/389441b3281eb117bfb1ba69ea1f4031e21cd2950079dd071ad407c32af9ef7c/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "373e92edc826", "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 liang;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": "1854ce0f309dc126dd70fce69d03a89fea5fcd3d71c23687078ff197ba2c2453", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/1854ce0f309d", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "39eb37285deb1ca13d90e46ffa653cffa8ba18245b9b6749d182849781de4abd", "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": "e09f0d9554910436eb78d7d7408b92870c8eb86481a5c1749c413753c9725b3c", "EndpointID": "39eb37285deb1ca13d90e46ffa653cffa8ba18245b9b6749d182849781de4abd", "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 } } } } ]