docker命令
后台启动容器
# 命令 docker run -d 镜像ID #后台运行docker
[root@172-0-0-2 /]# docker run -d centos
63130e5e23aedb0f6106995aaa38c2f3ff0ed9a79176b9d4727b2c12be3fd7e7
#常见问题:docker 容器使用后台方式运行,就必须要有一个前台进程,docker发现没有应用,就会停止运行
# nginx,容器启动后,发现自己没有提供服务,就会立即停止运行
查看日志
# docker logs --help
Usage: docker logs [OPTIONS] CONTAINER # docker logs [选项] 容器ID
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:37) or relative
(e.g. 42m for 42 minutes)
--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:37) or relative
(e.g. 42m for 42 minutes)
-tf #显示日志
--tail number #显示日志行数
[root@172-0-0-2 /]# docker run -d centos /bin/sh -c "while true;do echo hello world;sleep 1;done "#编写一段脚本执行
[root@172-0-0-2 /]# docker ps #查询容器ID
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f2b2dba9cb0 centos "/bin/sh -c 'while t…" 24 seconds ago Up 22 second
[root@172-0-0-2 /]# docker logs -tf --tail 5 1f2b2dba9cb0 #显示日志
2020-06-28T20:43:39.975592240Z hello world
2020-06-28T20:43:40.978753200Z hello world
2020-06-28T20:43:41.981909840Z hello world
2020-06-28T20:43:42.985172040Z hello world
2020-06-28T20:43:43.988394240Z hello world
2020-06-28T20:43:44.991561480Z hello world
查看进程信息
docker top 容器ID
[root@172-0-0-2 /]# docker top 1f2b2dba9cb0
UID PID PPID C STIME TTY TIME CMD
root 24196 24178 0 04:42
root 24833 24196 0 04:49
UID #用户ID
PID #副ID
PPID #进程ID
查看镜像元数据
# 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@172-0-0-2 /]# docker inspect 1f2b2dba9cb0 #指定容器的详细信息
[
{
"Id": "1f2b2dba9cb0d3a0a38d3e2e0d5c6b1f9dc6cc6e874cce053a4352ea44046c43",
"Created": "2020-06-28T20:42:53.78648092Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo hello world;sleep 1;done "
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 24196,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-06-28T20:42:54.83615884Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:831691599b88ad6cc2a4abbd0e89661a121aff14cfa289ad840fd3946f274f1f",
"ResolvConfPath": "/var/lib/docker/containers/1f2b2dba9cb0d3a0a38d3e2e0d5c6b1f9dc6cc6e874cce053a4352ea44046c43/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/1f2b2dba9cb0d3a0a38d3e2e0d5c6b1f9dc6cc6e874cce053a4352ea44046c43/hostname",
"HostsPath": "/var/lib/docker/containers/1f2b2dba9cb0d3a0a38d3e2e0d5c6b1f9dc6cc6e874cce053a4352ea44046c43/hosts",
"LogPath": "/var/lib/docker/containers/1f2b2dba9cb0d3a0a38d3e2e0d5c6b1f9dc6cc6e874cce053a4352ea44046c43/1f2b2dba9cb0d3a0a38d3e2e0d5c6b1f9dc6cc6e874cce053a4352ea44046c43-json.log",
"Name": "/stoic_nightingale",
"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,
"Capabilities": null,
"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/523c86fb02fe9ced33d55042b4a8c0e55707509001d2896f9909973b7c3b457c-init/diff:/var/lib/docker/overlay2/f73d5769d4aa2f59c5711fb6c19dd4151e2ccc004edbd6c383d8aafbb0400182/diff",
"MergedDir": "/var/lib/docker/overlay2/523c86fb02fe9ced33d55042b4a8c0e55707509001d2896f9909973b7c3b457c/merged",
"UpperDir": "/var/lib/docker/overlay2/523c86fb02fe9ced33d55042b4a8c0e55707509001d2896f9909973b7c3b457c/diff",
"WorkDir": "/var/lib/docker/overlay2/523c86fb02fe9ced33d55042b4a8c0e55707509001d2896f9909973b7c3b457c/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "1f2b2dba9cb0",
"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 hello world;sleep 1;done "
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20200611",
"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": "e116943cf0ea4843cd84868e51b48cfbb7388a323fe503f35dfabf9ce4fe27be",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/e116943cf0ea",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "f463f255a125385a79bf2d29a326c7e73382fabac87f19a2faa7234bc789c7dd",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:04",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "ada91f681d918fb768178a7157b5fc0de1924068242e994d6bf7892acb407272",
"EndpointID": "f463f255a125385a79bf2d29a326c7e73382fabac87f19a2faa7234bc789c7dd",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:04",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的容器
#命令语法
[root@172-0-0-2 /]# 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
-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@172-0-0-2 /]# docker exec -it 1f2b2dba9cb0 ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
#方式二
#命令语法
[root@172-0-0-2 /]# 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)
# docker exec #进入容器后开启一个新的终端,可以在里面进行操作(常用)
# docker attach #进入容器正在执行的终端,不会启动新的进程
从容器内拷贝文件到主机上
[root@CentOS7 /]# docker cp --help #语法帮助
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- #docker cp [options] 容器ID:Src地址 主机目的地址
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH #docker cp [options] Src地址 容器ID:主机目的地址
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@CentOS7 /]# docker ps #查询容器ID
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
c8ef48ac1d88 centos "/bin/bash" 6 minutes ago Up 6 minutes
[root@CentOS7 /]# docker cp c8ef48ac1d88:/home/f2 /
[root@CentOS7 /]# ls
bin boot dev etc f2 home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var