镜像命令
说明:有了镜像才会有容器
1、下载容器
docker pull centos
2、新建容器并启动
docker run [参数] image
启动并进入容器,基础镜像很多命令都完善的
退出:exit直接容器停止并退出、ctrl+p+q容器不停止退出
3、查看启动的容器
docker ps
4、删除容器
docker rm 容器ID:删除指定的容器,不能删除正在运行的容器,需要加 -f 参数
docker rm -rf $(docker ps -aq):删除全部容器
docker ps -a -q | xargs docker rm:删除全部容器
5、启动和停止容器的操作
docker start 容器ID:启动
docker restart 容器ID:重启
docker stop 容器ID:停止
docker kill 容器:强制停止
docker run -d 镜像名:发现停止了,docker容器使后台运行,必须要有一个前台进程,docker发现没有应用了,就会自动停止容器,例如安装nginx,容器启动后发现没有提供服务,就会自己停止
1、查看日志命令
docker logs -f -t -n --tail 容器ID
2、查看容器中的进程信息
docker top 容器ID
3、查看镜像的元数据
docker inspect 容器ID
[
{
"Id": "839732d7b8242996992e80f0d09657d125e85ad13c0fc65ce5219301f127405c",
"Created": "2021-12-11T06:42:41.384471712Z",
"Path": "/entrypoint.sh",
"Args": [
"mysqld"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2241,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-12-16T11:30:35.321792888Z",
"FinishedAt": "2021-12-13T14:08:12.220366833Z",
"Health": {
"Status": "healthy",
"FailingStreak": 0,
"Log": [
{
"Start": "2021-12-16T14:46:37.348469638Z",
"End": "2021-12-16T14:46:37.416738721Z",
"ExitCode": 0,
"Output": "mysqld is alive\n"
},
{
"Start": "2021-12-16T14:47:07.497470263Z",
"End": "2021-12-16T14:47:07.56773618Z",
"ExitCode": 0,
"Output": "mysqld is alive\n"
},
{
"Start": "2021-12-16T14:47:37.65155168Z",
"End": "2021-12-16T14:47:37.710534305Z",
"ExitCode": 0,
"Output": "mysqld is alive\n"
},
{
"Start": "2021-12-16T14:48:07.793479013Z",
"End": "2021-12-16T14:48:07.859810096Z",
"ExitCode": 0,
"Output": "mysqld is alive\n"
},
{
"Start": "2021-12-16T14:48:37.935498722Z",
"End": "2021-12-16T14:48:38.002074013Z",
"ExitCode": 0,
"Output": "mysqld is alive\n"
}
]
}
},
"Image": "sha256:087c6f86492cf3e450b2a029c0155c46fca92244037affefa19ae62feda13f79",
"ResolvConfPath": "/var/lib/docker/containers/839732d7b8242996992e80f0d09657d125e85ad13c0fc65ce5219301f127405c/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/839732d7b8242996992e80f0d09657d125e85ad13c0fc65ce5219301f127405c/hostname",
"HostsPath": "/var/lib/docker/containers/839732d7b8242996992e80f0d09657d125e85ad13c0fc65ce5219301f127405c/hosts",
"LogPath": "/var/lib/docker/containers/839732d7b8242996992e80f0d09657d125e85ad13c0fc65ce5219301f127405c/839732d7b8242996992e80f0d09657d125e85ad13c0fc65ce5219301f127405c-json.log",
"Name": "/mysql",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"3306/tcp": [
{
"HostIp": "",
"HostPort": "3306"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "private",
"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": null,
"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/43b4c46a6e7f9d96af29faac02322429b3060df6359c80d4e626197cbc736eec-init/diff:/var/lib/docker/overlay2/b622e2d9709a3a1e87c183789a1a86e023887a94bc5b4855ad2d425b7304bbc9/diff:/var/lib/docker/overlay2/07b950211f35d22dae0b191e9fba485cd0f12ff8bf68f6244c1c44988574117c/diff:/var/lib/docker/overlay2/de1a9d1f7eb9a52d802ffbca9ff580bd2297e0532dae3e45f4cb6ee1daa1d8cc/diff:/var/lib/docker/overlay2/91434ddbdd4de70e58d68f514a2d10bc162f31ef5230f206bb64cb179c0e0897/diff:/var/lib/docker/overlay2/af86b037eab2a9dc213b95ace81b6cd4be5e13c70723beef16b73dc7e668815c/diff:/var/lib/docker/overlay2/40c739f7875bb531a9929cffa3d125c01edca633ab706052334e270bba012f16/diff:/var/lib/docker/overlay2/afdd3b6330502077f613c286af31c159c1db383700981ba8a6fbd401f456424d/diff",
"MergedDir": "/var/lib/docker/overlay2/43b4c46a6e7f9d96af29faac02322429b3060df6359c80d4e626197cbc736eec/merged",
"UpperDir": "/var/lib/docker/overlay2/43b4c46a6e7f9d96af29faac02322429b3060df6359c80d4e626197cbc736eec/diff",
"WorkDir": "/var/lib/docker/overlay2/43b4c46a6e7f9d96af29faac02322429b3060df6359c80d4e626197cbc736eec/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "839732d7b824",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"3306/tcp": {},
"33060/tcp": {},
"33061/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"MYSQL_ROOT_PASSWORD=123456",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"MYSQL_UNIX_PORT=/var/lib/mysql/mysql.sock"
],
"Cmd": [
"mysqld"
],
"Healthcheck": {
"Test": [
"CMD-SHELL",
"/healthcheck.sh"
]
},
"Image": "mysql/mysql-server",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/entrypoint.sh"
],
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "aec0d51e01996e9eb0452f8897086e593a7589d0408876be85e42a5d2b1f4e79",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "3306"
}
],
"33060/tcp": null,
"33061/tcp": null
},
"SandboxKey": "/var/run/docker/netns/aec0d51e0199",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "8537b8f765850be343d8bb1f933972c7617ced81851ae5b0d2c924474ffd2b5f",
"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": "29ba535e1950ba283d300a1879c4279b2c1f672a203ae07864fc257aa4fbee1d",
"EndpointID": "8537b8f765850be343d8bb1f933972c7617ced81851ae5b0d2c924474ffd2b5f",
"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
}
}
}
}
]
3、进入当前容器
需要进入容器修改配置
docker exec -it 容器ID bashShell
docker attach 容器ID :正在执行当前的代码
exec 进入容器后开启一个新的终端,可以在里面操作,想进入容器正在执行的终端就使用attach
4、拷贝容器中的文件到宿主机
docker cp 容器ID:容器内路径 宿主机路径