Docker入门第五章
常用其他命令
后台启动容器
#命令 docker run -d 镜像名!
[root@lvshihao /]# docker run -d centos
757173133e8e73985f024dc7e0506f4ac773ce6ba1fbbce18584eda80dbc7603
[root@lvshihao /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
757173133e8e centos "/bin/bash" 9 seconds ago Exited (0) 8 seconds ago optimistic_banach
#问题docker ps,返现centos已经停止了
#常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会停止
查看容器日志
docker logs -f -t --tail 显示的条数 容器ID #发现centos没有日志
#自己编写一段shell脚本,让centos在后台一直每一秒执行着输出lvshihao的操作,这样centos就不会立刻挂掉
docker run -d centos /bin/bash -c "while true;do echo lvshihao;sleep 1;done"
[root@lvshihao /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3ab34a21e1cc centos "/bin/bash -c 'while…" About a minute ago Up About a minute sharp_yonath
#显示日志
-tf #显示日志
--tail number #要显示日志条数
[root@lvshihao /]# docker logs -tf --tail 3 3ab34a21e1cc
2021-08-14T01:52:57.239363667Z lvshihao
2021-08-14T01:52:58.243463010Z lvshihao
2021-08-14T01:52:59.249260645Z lvshihao
查看容器中进程信息
#命令 docker top 容器id
[root@lvshihao /]# docker top 3ab34a21e1cc
UID PID PPID C STIME TTY TIME CMD
root 70857 70839 0 09:49 ? 00:00:00 /bin/bash -c while true;do echo lvshihao;sleep 1;done
root 76932 70857 0 09:56 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看镜像的元素据
#命令
docker inspect 容器id
#测试
[root@lvshihao /]# docker inspect 3ab34a21e1cc
[
{
"Id": "3ab34a21e1cce570adf734bb9cea5b1df197bbafee806dd137cdf839c9f5af32",
"Created": "2021-08-14T01:49:57.957598671Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true;do echo lvshihao;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 70857,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-08-14T01:49:58.36755625Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/3ab34a21e1cce570adf734bb9cea5b1df197bbafee806dd137cdf839c9f5af32/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/3ab34a21e1cce570adf734bb9cea5b1df197bbafee806dd137cdf839c9f5af32/hostname",
"HostsPath": "/var/lib/docker/containers/3ab34a21e1cce570adf734bb9cea5b1df197bbafee806dd137cdf839c9f5af32/hosts",
"LogPath": "/var/lib/docker/containers/3ab34a21e1cce570adf734bb9cea5b1df197bbafee806dd137cdf839c9f5af32/3ab34a21e1cce570adf734bb9cea5b1df197bbafee806dd137cdf839c9f5af32-json.log",
"Name": "/sharp_yonath",
"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/fd530145a93bd0d4db9acd20db467c650621cc30e5f605bdeb7e282ee380cc14-init/diff:/var/lib/docker/overlay2/3bdf7ab69d622c811093a3a63304f2742982927912dc4299e5178f5318e5f5e8/diff",
"MergedDir": "/var/lib/docker/overlay2/fd530145a93bd0d4db9acd20db467c650621cc30e5f605bdeb7e282ee380cc14/merged",
"UpperDir": "/var/lib/docker/overlay2/fd530145a93bd0d4db9acd20db467c650621cc30e5f605bdeb7e282ee380cc14/diff",
"WorkDir": "/var/lib/docker/overlay2/fd530145a93bd0d4db9acd20db467c650621cc30e5f605bdeb7e282ee380cc14/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "3ab34a21e1cc",
"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/bash",
"-c",
"while true;do echo lvshihao;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201204",
"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": "7af8de99d2031e7131411d3fee641b0c0d0b2ecc240603f83a786bedf877894d",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/7af8de99d203",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "aa1b52291a367be48b38a3ab5b7615f7a415e88f757e4f82b8bfa096d0a28019",
"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": "04da5e0f80d775440a2afa1f93fd7a2ddd829ee3c504c9d9e75305865509a305",
"EndpointID": "aa1b52291a367be48b38a3ab5b7615f7a415e88f757e4f82b8bfa096d0a28019",
"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 bashShell
#测试
[root@bogon ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3ab34a21e1cc centos "/bin/bash -c 'while…" 10 hours ago Up 5 seconds sharp_yonath
[root@bogon ~]# docker exec -it 3ab34a21e1cc /bin/bash
[root@3ab34a21e1cc /]# ls
[root@lvshihao ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3ab34a21e1cc centos "/bin/bash -c 'while…" 10 hours ago Up 6 minutes sharp_yonath
[root@lvshihao ~]# docker attach 3ab34a21e1cc
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@3ab34a21e1cc /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 1 11:41 ? 00:00:00 /bin/bash -c while true;do echo lvshihao;sleep 1;done
root 40 0 0 11:41 pts/0 00:00:00 /bin/bash
root 62 1 0 11:42 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root 63 40 2 11:42 pts/0 00:00:00 ps -ef
#方式二
docker attach 容器id
#测试
[root@lvshihao ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3ab34a21e1cc centos "/bin/bash -c 'while…" 10 hours ago Up 6 minutes sharp_yonath
[root@lvshihao ~]# docker attach 3ab34a21e1cc
lvshihao
lvshihao
正在执行当前的代码...
#docker exec 进入容器后开启一个新的终端,可以在里面操作(常用)
#docker attach 进入容器正在执行的终端,不会启动新的进程!
从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的主机路径
[root@lvshihao ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3ab34a21e1cc centos "/bin/bash -c 'while…" 10 hours ago Up 16 minutes sharp_yonath
#进入正在运行的容器
[root@lvshihao ~]# docker exec -it 3ab34a21e1cc /bin/bash
#在home目录创建一个lvshihao66.txt
[root@3ab34a21e1cc /]# cd /home/
[root@3ab34a21e1cc home]# touch lvshihao66.txt
#退回到主机上
[root@lvshihao ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3ab34a21e1cc centos "/bin/bash -c 'while…" 10 hours ago Up 17 minutes sharp_yonath
#将容器的文件copy到主机上面
[root@lvshihao ~]# docker cp 3ab34a21:/home/lvshihao66.txt /home
#在主机上面查看是否copy成功
[root@lvshihao ~]# cd /home/
[root@lvshihao home]# ls
lvshihao lvshihao66.txt
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
作者:吕世昊
个性签名:学习如逆水行舟,不进则退!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!