其他命令
docker run -d image 后台运行镜像
常见的坑:容器使用后台运行,需要有前台进程在运行。如果发现没有应用,会自动停止。
[root@bogon ~]# docker ps -q [root@bogon ~]# docker run -d centos 9a7ef411b953d281d12d4c203e568dc0116c84a8add818255c81e61759f50786 [root@bogon ~]# docker ps -q [root@bogon ~]#
启动并确保有一个程序执行
[root@bogon ~]# docker run --name test2 -d busybox sh -c "while true; do $(echo date); sleep 1; done" 0ce9b3f3bd7825a2a6bc9f330f8b622b8dd2f68194b4c7300662273971cc7de8 [root@bogon ~]# docker ps -q 0ce9b3f3bd78
docker logs 查看日志命令
[root@bogon ~]# docker logs --help Usage: docker logs [OPTIONS] CONTAINER Fetch the logs of a container Options: --details Show extra details provided to logs -f, --follow Follow log output --since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes) -n, --tail string Number of lines to show from the end of the logs (default "all") -t, --timestamps Show timestamps --until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
[root@bogon ~]# date Sat Aug 27 18:32:33 CST 2022 [root@bogon ~]# docker logs -f --until=2s test2 Sat Aug 27 10:31:59 UTC 2022 Sat Aug 27 10:32:00 UTC 2022 Sat Aug 27 10:32:01 UTC 2022 Sat Aug 27 10:32:02 UTC 2022
[root@bogon ~]# docker logs -ft -n 5 test2 2022-08-27T10:32:57.022947598Z Sat Aug 27 10:32:57 UTC 2022 2022-08-27T10:32:58.030872018Z Sat Aug 27 10:32:58 UTC 2022 2022-08-27T10:32:59.035134636Z Sat Aug 27 10:32:59 UTC 2022 2022-08-27T10:33:00.039751246Z Sat Aug 27 10:33:00 UTC 2022 2022-08-27T10:33:01.047756171Z Sat Aug 27 10:33:01 UTC 2022
docker top 查看容器中的进程信息
[root@bogon ~]# docker top --help Usage: docker top CONTAINER [ps OPTIONS] Display the running processes of a container
查看具体容器的进程信息
[root@bogon ~]# docker top 7bd161a85be4 UID PID PPID C STIME TTY TIME CMD root 113531 113510 0 18:48 ? 00:00:00 sh -c while true; do date; sleep 1; done root 113642 113531 0 18:48 ? 00:00:00 sleep 1
docker inspect 查看正在运行的容器的信息
[root@bogon ~]# docker inspect --help Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...] Return low-level information on Docker objects Options: -f, --format string Format the output using the given Go template -s, --size Display total file sizes if the type is container --type string Return JSON for specified type
[root@bogon ~]# docker inspect 7bd161a85be4 [ { "Id": "7bd161a85be4d38b0ccff06affddfec27f42e29543c5b71ef110917dc1fb28e6", "Created": "2022-08-27T10:48:32.557989731Z", "Path": "sh", "Args": [ "-c", "while true; do date; sleep 1; done" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 113531, "ExitCode": 0, "Error": "", "StartedAt": "2022-08-27T10:48:35.096847989Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:beae173ccac6ad749f76713cf4440fe3d21d1043fe616dfbe30775815d1d0f6a", "ResolvConfPath": "/var/lib/docker/containers/7bd161a85be4d38b0ccff06affddfec27f42e29543c5b71ef110917dc1fb28e6/resolv.conf", "HostnamePath": "/var/lib/docker/containers/7bd161a85be4d38b0ccff06affddfec27f42e29543c5b71ef110917dc1fb28e6/hostname", "HostsPath": "/var/lib/docker/containers/7bd161a85be4d38b0ccff06affddfec27f42e29543c5b71ef110917dc1fb28e6/hosts", "LogPath": "/var/lib/docker/containers/7bd161a85be4d38b0ccff06affddfec27f42e29543c5b71ef110917dc1fb28e6/7bd161a85be4d38b0ccff06affddfec27f42e29543c5b71ef110917dc1fb28e6-json.log", "Name": "/test2", "RestartCount": 0, "Driver": "devicemapper", "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": { "DeviceId": "60", "DeviceName": "docker-8:3-734102-7f1a90083b206ee77ce40ac8ff6f699ece59fd720d2f0c82b637378e7ec8783b", "DeviceSize": "10737418240" }, "Name": "devicemapper" }, "Mounts": [], "Config": { "Hostname": "7bd161a85be4", "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": [ "sh", "-c", "while true; do date; sleep 1; done" ], "Image": "busybox", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "6fe095634c33c2c06abaab036db8c29ec24d1ae6368417d5a850afb32e4a8546", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/6fe095634c33", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "5b64427d598bfed705c962e9cee356e1f94e35b2462a29e8551100b2a2dff0b3", "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": "0de314c27f201ee21446146940ccf27a8a30ccfd1e59f6e30ff3e4aadc3fccb4", "EndpointID": "5b64427d598bfed705c962e9cee356e1f94e35b2462a29e8551100b2a2dff0b3", "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@bogon ~]#
docker exec 进入容器
[root@bogon ~]# docker exec --help Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in a running container Options: -d, --detach Detached mode: run command in the background --detach-keys string Override the key sequence for detaching a container -e, --env list Set environment variables --env-file list Read in a file of environment variables -i, --interactive Keep STDIN open even if not attached --privileged Give extended privileges to the command -t, --tty Allocate a pseudo-TTY -u, --user string Username or UID (format: <name|uid>[:<group|gid>]) -w, --workdir string Working directory inside the container
[root@bogon ~]# docker exec -it ea478f0fbed6 /bin/bash
[root@ea478f0fbed6 /]#
docker attach 进入容器正在执行的程序
[root@bogon ~]# docker attach --help Usage: docker attach [OPTIONS] CONTAINER Attach local standard input, output, and error streams to a running container Options: --detach-keys string Override the key sequence for detaching a container --no-stdin Do not attach STDIN --sig-proxy Proxy all received signals to the process (default true)
[root@bogon ~]# docker attach ea478f0fbed6 [root@ea478f0fbed6 home]# ls test.py
#docker exe #进入容器后重启开启一个终端,可以自行操作
#docker attach # 直接进入终端当前正在进行的终端,不会启动新的进程。
docker cp 从容器中拷贝文件到主机
[root@bogon ~]# 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 Use '-' as the source to read a tar archive from stdin and extract it to a directory destination in a container. Use '-' as the destination to stream a tar archive of a container source to stdout. Options: -a, --archive Archive mode (copy all uid/gid information) -L, --follow-link Always follow symbol link in SRC_PATH [root@bogon ~]#
## 启动容器并在容器中创建文件
[root@bogon ~]# docker run -it --name "cent1" centos /bin/bash [root@ea478f0fbed6 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@ea478f0fbed6 /]# pwd / [root@ea478f0fbed6 /]# cd /home [root@ea478f0fbed6 home]# ls [root@ea478f0fbed6 home]# touch test.py [root@ea478f0fbed6 home]# ls test.py
开始文件拷贝
[root@bogon ~]# docker cp ea478f0fbed6:/home/test.py /home/ [root@bogon ~]# ls anaconda-ks.cfg [root@bogon ~]# ls /home/ admin czb test.py