启动守护式容器(在后台运行)docker run -d+容器名
但有的容器在后台运行,必须要有一个前台进程,容器运行的命令如果不是一直挂起的命令,例如 top,tail,容器就会自动退出,
类似于nginx,启动nginx只需要 service nginx start就可以了,但如果让nginx为后台进程模式运行,前台没有实际运行的应用,容器启动后会立即自杀,docker机制认为它没事可做了。所以最好的做法是让要运行的程序以前台进程的形式运行。
用守护式进程运行centos有没有必要?没必要,因为运行一个centos是像一个开关打开了centos,直接交互就可以了,如果用 docker run -d -it centos运行的话,这样不会进容器交互窗口,也不会让centos容器被docker杀死,但这样做没有意义,因为运行了centos势必要对系统文件进行编辑,没有必要在后台运行。
docker exec -it centos的容器号 bin/bash 就可以进入了
[root@test 12345]# docker run -d centos
d22ffbc3707c44de4f8e639b9477a5e899e4d885c2a186443aa6f7d4d5ea41ce
[root@test 12345]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@test 12345]#
//以 docker run -it 7777:8080 tomcat 运行tomcat的话,会产生一个前台交互窗口
//以 docker run -d 7777:8080 tomcat 运行tomcat的话,会以守护式容器的形式在后台运行,因为tomcat容器是一直会挂起运行的,不会被当做事情做完了被docker杀掉
查看容器日志 docker logs -f -t —tail +容器id
-t 日志的时间戳 -f 循环打印 -n 3 查看倒数3行
鲸鱼背上有多个容器,用于查看每个容器的日志。
Tip: while true 表示一直执行 while循环一节中已经学过
[root@test 12345]# docker run -d centos /bin/sh -c "while true;do echo zzyy;sleep 2;done"
2df8352b191095c051094e736239dc3695a5b11dcee49ede80913c1fa6f183ed
[root@test 12345]# docker ps //只有给后台运行的容器命令运行,后台的程序才会显示在线up
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8c3e9246792b centos "/bin/sh -c 'while t…" 36 seconds ago Up 33 seconds compassionate_shannon
[root@test 12345]# docker logs -f -t -n 3 8c3e9246792b //不断循环打印最后3行带有时间戳的日志
2021-01-14T00:53:00.202896885Z zzyy
2021-01-14T00:53:02.219291052Z zzyy
2021-01-14T00:53:04.222945964Z zzyy
2021-01-14T00:53:06.226363144Z zzyy
查看容器内的进程
docker top 容器id
[root@test 12345]# docker top 8c3e9246792b
UID PID PPID C STIME TTY TIME CMD
root 53493 53303 0 16:52 ? 00:00:00 /bin/sh -c while true;do echo zzyy;sleep 2;done
root 130653 53493 0 16:56 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 2
查看容器内部的细节—后续重点掌握
docker inspect 容器id
容器像一个花卷,是一层套一层的。json串形式嵌套的
[root@test 12345]# docker inspect 8c3e9246792b
[
{
"Id": "8c3e9246792ba7f7ae5d63dca54d238d95932bbb5fbecbe4bae9548e597cc2d8",
"Created": "2021-01-14T00:52:13.377872404Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo zzyy;sleep 2;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 53493,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-01-14T00:52:15.967458266Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/8c3e9246792ba7f7ae5d63dca54d238d95932bbb5fbecbe4bae9548e597cc2d8/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/8c3e9246792ba7f7ae5d63dca54d238d95932bbb5fbecbe4bae9548e597cc2d8/hostname",
"HostsPath": "/var/lib/docker/containers/8c3e9246792ba7f7ae5d63dca54d238d95932bbb5fbecbe4bae9548e597cc2d8/hosts",
"LogPath": "/var/lib/docker/containers/8c3e9246792ba7f7ae5d63dca54d238d95932bbb5fbecbe4bae9548e597cc2d8/8c3e9246792ba7f7ae5d63dca54d238d95932bbb5fbecbe4bae9548e597cc2d8-json.log",
"Name": "/compassionate_shannon",
"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/2aa3632adb4f7f0b0529409e2c8e92ede3fde70529898ece4a1e84bedd53d314-init/diff:/var/lib/docker/overlay2/f9317558e93bbe858d58d5b53c411153adb91873bea865256ecb69a4e480e40d/diff",
"MergedDir": "/var/lib/docker/overlay2/2aa3632adb4f7f0b0529409e2c8e92ede3fde70529898ece4a1e84bedd53d314/merged",
"UpperDir": "/var/lib/docker/overlay2/2aa3632adb4f7f0b0529409e2c8e92ede3fde70529898ece4a1e84bedd53d314/diff",
"WorkDir": "/var/lib/docker/overlay2/2aa3632adb4f7f0b0529409e2c8e92ede3fde70529898ece4a1e84bedd53d314/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "8c3e9246792b",
"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 zzyy;sleep 2;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": "59e1c606725e3d930a49493a480ec13f45211a66e397b6c67a5ed610ca74b071",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/59e1c606725e",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "dc2ccfce41acab56cf19c4b334385212b26e3ef8d650657e98142cd255f41b13",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "02c19266796815c56353c974d3424fec0f0d2284cf47defa82d8c95c7c57e0c5",
"EndpointID": "dc2ccfce41acab56cf19c4b334385212b26e3ef8d650657e98142cd255f41b13",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
docker attach 容器id 进入容器不执行新的进程
CTRL+P+Q退出容器后,又要进入正在运行的容器,并以命令行交互
[root@test 12345]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest feba8d001e3f 3 weeks ago 649MB
centos latest 300e315adb2f 5 weeks ago 209MB
[root@test 12345]# docker run -it centos
[root@9a5ab0c33777 /]# CTRL+P+Q退出容器
[root@test 12345]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a5ab0c33777 centos "/bin/bash" 12 minutes ago Up 12 minutes stoic_kepler
[root@test 12345]# docker attach 9a5ab0c33777 //进入容器
[root@9a5ab0c33777 /]# ls -l /tmp
total 8
-rwx------. 1 root root 701 Dec 4 17:37 ks-script-esd4my7v
-rwx------. 1 root root 671 Dec 4 17:37 ks-script-eusq_sc5
docker exec 执行容器中的进程
docker exec -it 容器id ls -l /tmp 执行容器中的 ls -l /tmp命令,但并没进入容器,相当于隔山打牛
[root@test 12345]# docker exec -t 9a5ab0c33777 ls -l /tmp
total 8
-rwx------. 1 root root 701 Dec 4 17:37 ks-script-esd4my7v
-rwx------. 1 root root 671 Dec 4 17:37 ks-script-eusq_sc5
docker exec -it 9a5ab0c33777 /bin/bash = docker attach 9a5ab0c33777
exec 功能更为强大一些
[root@test 12345]# docker exec -t 9a5ab0c33777 /bin/bash
[root@9a5ab0c33777 /]#
复制docker中的文件到主机
docker cp 容器id:文件路径 主机路径
[root@test 12345]# docker cp 9a5ab0c33777 /tmp/ks-script-eusq_sc5 /root
"docker cp" requires exactly 2 arguments.
See 'docker cp --help'.
//容器后面没加冒号报错
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copy files/folders between a container and the local filesystem
//复制docker种的文件到主机路径下
[root@test 12345]# docker cp 9a5ab0c33777:/tmp/ks-script-eusq_sc5 /root
[root@test 12345]# ls
Desktop Documents Downloads Music Pictures Public Templates Videos
[root@test 12345]# cd /root
[root@test ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg ks-script-eusq_sc5 original-ks.cfg
[root@test ~]#