一、帮助命令
docker version #显示docker的版本信息
docker info #显示docker的系统信息,包括镜像和容器的数量
docker --help #显示docker的所有命令
帮助文档的地址: docker build | Docker Docsdocker build | Docker Docshttps://docs.docker.com/engine/reference/commandline/build/ docker build | Docker Docs
二、镜像命令
docker images #查看本地主机上的所有镜像
解释:
REPOSITORY #镜像的仓库源
TAG #镜像的标签
IMAGE ID #镜像的ID
CREATED #镜像的创建时间
SIZE #镜像的大小
可选项:
docker images -a #显示所有的镜像
docker images -q #只显示镜像ID
docker search 镜像名 #搜索镜像
如:docker search mysql #搜索mysql的镜像
docker pull 镜像名 #下载镜像
如:docker pull mysql:8.0.19,如不给版本号,则默认下载最新版本
docker rmi -f 镜像id #删除镜像
docker rmi -f 镜像id 镜像id #删除多个容器
docker rmi -f $(docker images -aq) #按镜像删除所有镜像
三、容器命令
说明:我们有了镜像才可以创建容器,所以我们下载一个centos镜像来测试学习
docker pull centos
新建容器并启动
docker run [可选参数] image
#参数说明
--name 容器名字 给容器设置一个名字
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
#测试,启动并进行容器
docker run -it 容器名 /bin/bash
#查看容器的信息
ls
#退出容器
exit #从容器中退回主机,容器直接停止并退出
ctrl +p +q #容器不停止退出
#docker ps命令
docker ps #当前正在运行的容器
docker ps -a #当前正在运行的容器+历史运行过的容器
docker ps -n=? #显示最近创建的容器
如:docker ps -n=1 查询最近创建的第一个容器
docker ps -q #只显示容器的编号
删除容器
docker rm 容器id #删除指定的容器,不能删除正在运行的容器
docker rm -f 容器id #强制删除指定的容器
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 #杀掉容器
常用的其他命令
后台启动容器
docker run -d 镜像名 #后台启动
#问题docker ps启动后,发现容器停止了
#常见的坑:docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
查看日志
docker logs -tf 容器id #显示日志
docker logs -tf --tail 10 容器id #显示10条日志
#自己编写一段shell脚本测试
docker run -d centos /bin/bash -c "while true;do echo luohai;sleep 1;done"
查看容器中的进程信息
docker top 容器 id #查看正在运行的容器进程信息
查看镜像的元数据
docker inspect 容器id #查看容器的元数据
#测试
[root@localhost /]# docker inspect 467a9b165cd5
[
{
"Id": "467a9b165cd5a60e3af5c654e7aefb96d73845c568d1c903d7451a9c8ab010d9",
"Created": "2023-09-25T08:30:18.967178608Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4123,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-09-26T08:49:13.604580928Z",
"FinishedAt": "2023-09-25T08:36:32.500123581Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/467a9b165cd5a60e3af5c654e7aefb96d73845c568d1c903d7451a9c8ab010d9/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/467a9b165cd5a60e3af5c654e7aefb96d73845c568d1c903d7451a9c8ab010d9/hostname",
"HostsPath": "/var/lib/docker/containers/467a9b165cd5a60e3af5c654e7aefb96d73845c568d1c903d7451a9c8ab010d9/hosts",
"LogPath": "/var/lib/docker/containers/467a9b165cd5a60e3af5c654e7aefb96d73845c568d1c903d7451a9c8ab010d9/467a9b165cd5a60e3af5c654e7aefb96d73845c568d1c903d7451a9c8ab010d9-json.log",
"Name": "/infallible_wu",
"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,
"ConsoleSize": [
65,
190
],
"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",
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"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/904671d913f05ed7b230194f53cc6bbb540acdaf77f0a7aa35b4e8f4cac4ed08-init/diff:/var/lib/docker/overlay2/219f13da32ccb35221477ff31ba0dfe3a05524977dca04574ca7c1b92c6bd65e/diff",
"MergedDir": "/var/lib/docker/overlay2/904671d913f05ed7b230194f53cc6bbb540acdaf77f0a7aa35b4e8f4cac4ed08/merged",
"UpperDir": "/var/lib/docker/overlay2/904671d913f05ed7b230194f53cc6bbb540acdaf77f0a7aa35b4e8f4cac4ed08/diff",
"WorkDir": "/var/lib/docker/overlay2/904671d913f05ed7b230194f53cc6bbb540acdaf77f0a7aa35b4e8f4cac4ed08/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "467a9b165cd5",
"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": "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": "4e252b15fe1abe6591859993a45210435b3c397d72f59d03edb4c0b664422ccf",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/4e252b15fe1a",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "2651e47ccb0243e62151b86a2c98487100f8547d229ccb3febe0bbecc700476b",
"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": "fff45d658cf92bd198d6767752ea163c52b6df465afea1a9a07380ca2913c277",
"EndpointID": "2651e47ccb0243e62151b86a2c98487100f8547d229ccb3febe0bbecc700476b",
"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 /bin/bash
#测试
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
467a9b165cd5 centos "/bin/bash" 41 hours ago Up 17 hours infallible_wu
[root@localhost ~]# docker exec -it 467a9b165cd5 /bin/bash
[root@467a9b165cd5 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#方式二
docker attach 容器id
#测试
[root@localhost ~]# docker attach 467a9b165cd5
[root@467a9b165cd5 /]#
#两者的区别
# docker exec 进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach 进入容器正在执行的终端,不会启动新的进程
从容器内拷贝到宿主机上
docker cp 容器id:容器内的文件路径 宿主机的路径
#进入docker容器内部
[root@localhost /]# docker attach 467a9b165cd5
#新建测试文件
[root@467a9b165cd5 /]# touch luohai.txt
#查看新建的文件
[root@467a9b165cd5 /]# ls
bin dev etc home lib lib64 lost+found luohai.txt media mnt opt proc root run sbin srv sys tmp usr var
#退出容器
[root@467a9b165cd5 /]# exit
#执行docker拷贝,将容器内的文件拷贝到宿主机上
[root@localhost /]# docker cp 467a9b165cd5:/luohai.txt /
Successfully copied 1.54kB to /
[root@localhost /]# ls
bin boot dev etc home lib lib64 luohai.txt media mnt opt proc root run sbin srv sys tmp usr var
#以上的拷贝是一个手动操作,未来我们大量使用的是 -v数据卷的技术,实现容器内和宿主机上的文件自动同步