一、 后台启动容器
# 命令 docker run -d 镜像名
[root@localhost ~]# docker run -d centos
# 问题docker ps 发现 centos 停止了
# 常见的坑 docker 容器使用后台运行 就必须要有一个前台进程, docker 发现没有应用了 就会自动停止
# nginx, 容器启动后, 发现自己没有提供服务, 就会立刻停止 ,就是没有程序了
二、 查看日志
docker logs -f -t --tail 容器,没有日志
# 显示日志
-tf # 显示全部日志
--tail number # 要显示的日志条数
[root@localhost ~]# docker logs -tf --tail 10 容器id
三、 查看容器中进程信息
[root@localhost ~]# docker top 7f0ad55ec1e0
UID PID PPID C STIME TTY TIME CMD
root 110821 110797 0 20:35 pts/0 00:00:00 /bin/bash
[root@localhost ~]#
四、 查看镜像的元数据
# 命令
docker inspect 容器id
# 测试
[root@localhost ~]# docker inspect 7f0ad55ec1e0
[
{
"Id": "7f0ad55ec1e0ed1389e99c452b6882f6bc6de11f1d8ace6bac2cdd8618bbedac",
"Created": "2022-04-24T12:35:23.562758041Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 110821,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-04-24T12:35:23.907525511Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/7f0ad55ec1e0ed1389e99c452b6882f6bc6de11f1d8ace6bac2cdd8618bbedac/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/7f0ad55ec1e0ed1389e99c452b6882f6bc6de11f1d8ace6bac2cdd8618bbedac/hostname",
"HostsPath": "/var/lib/docker/containers/7f0ad55ec1e0ed1389e99c452b6882f6bc6de11f1d8ace6bac2cdd8618bbedac/hosts",
"LogPath": "/var/lib/docker/containers/7f0ad55ec1e0ed1389e99c452b6882f6bc6de11f1d8ace6bac2cdd8618bbedac/7f0ad55ec1e0ed1389e99c452b6882f6bc6de11f1d8ace6bac2cdd8618bbedac-json.log",
"Name": "/gracious_chaplygin",
"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/cb13d1ad8fe7e727e8f983434071369b35006ac76e7393def5c22217853ec3ea-init/diff:/var/lib/docker/overlay2/7de1532b2684cbcd8804ed749ae5d6cfc63838378bcd459b6786ea7f3bff6d4a/diff",
"MergedDir": "/var/lib/docker/overlay2/cb13d1ad8fe7e727e8f983434071369b35006ac76e7393def5c22217853ec3ea/merged",
"UpperDir": "/var/lib/docker/overlay2/cb13d1ad8fe7e727e8f983434071369b35006ac76e7393def5c22217853ec3ea/diff",
"WorkDir": "/var/lib/docker/overlay2/cb13d1ad8fe7e727e8f983434071369b35006ac76e7393def5c22217853ec3ea/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "7f0ad55ec1e0",
"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": "01c1639b255aaad4b9aaafea45dcd0e8f4374cba898567662fcd52804f732a51",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/01c1639b255a",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "3ef7136919ae4a7f9ddeba57019f48cad63ec317425039a93e798d6be6f2a575",
"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": "4b4a05c20a03870495c05983343e9a766d87928908ac59e102cf584405370872",
"EndpointID": "3ef7136919ae4a7f9ddeba57019f48cad63ec317425039a93e798d6be6f2a575",
"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@localhost ~]#
五、 进入当前正在运行的容器文章来源:https://www.toymoban.com/news/detail-662518.html
# 我们通常容器都是使用后台方式运行的,需要进入容器 修改一些配置
# 命令
docker exec -it 容器id bashShell (/bin/bash)
# 测试
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
374fee09f8f4 centos "/bin/bash" 5 minutes ago Up 5 minutes youthful_shamir
[root@localhost ~]# docker exec -it 374fee09f8f4 /bin/bash
[root@374fee09f8f4 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@374fee09f8f4 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 02:13 pts/0 00:00:00 /bin/bash
root 15 0 0 02:20 pts/1 00:00:00 /bin/bash
root 30 15 0 02:20 pts/1 00:00:00 ps -ef
[root@374fee09f8f4 /]#
# 方式二
docker attach 容器id
# 测试
[root@localhost ~]# docker attach 374fee09f8f4
[root@374fee09f8f4 /]#
# docker exec -it # 进入容器后开启一个新的终端,可以在里面操作
# docker attach # 进入容器正在执行而定终端,不会启动新的进程!
六、 从容器内拷贝文件到主机上
文章来源地址https://www.toymoban.com/news/detail-662518.html
docker cp 容器id:容器内路径 目的主机的路径
# 进入docker容器内部
[root@localhost home]# docker run -it centos /bin/bash
WARNING: IPv4 forwarding is disabled. Networking will not work.
[root@b177dc96f111 /]# cd /home
[root@b177dc96f111 home]# ls
# 在容器内新建一个文件
[root@b177dc96f111 home]# touch sha.java
[root@b177dc96f111 home]# exit
[root@localhost home]# docker ps -a -n1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b177dc96f111 centos "/bin/bash" 35 seconds ago Exited (0) 14 seconds ago strange_gates
# 将这文件拷贝出来到主机上
[root@localhost home]# docker cp b177dc96f111:/home/sha.java /home
[root@localhost home]# ls
sha sha.java
[root@localhost home]#
# 拷贝是一个手动过程,未来我们使用 -v 卷的技术,可以实现
到了这里,关于docker其他常用命令(kuangshenstudy)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!