这里的容器可以理解为,以镜像为模板,生成的各种容器。
5.1 启动容器
docker run [可选参数] image
# 参数
-- name="容器名称"
-d # 以后台方式运行
-it # 以交互式方式运行
-p # 端口
# 使用格式: 主机端口:容器端口
-P # 随机指定端口(大写)
5.2 退出容器
exit # 停止并退出容器
Ctrl+p+q # 退出但不停止容器
╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░────────────────────────────────────────────────────░▒▓ ✔ took 4s at 16:04:59 ▓▒░
╰─ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c338b3c19b4d centos "/bin/bash" 9 seconds ago Up 7 seconds youthful_villani
5.3 查看运行的容器
╰─ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
╭─░▒▓ ~
╰─ docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs
-s, --size Display total file sizes
╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ ✔ at 16:01:26 ▓▒░
╰─
如果携带参数 a 则列出历史运行容器
╰─ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2ee64876800b centos "/bin/bash" 9 minutes ago Exited (0) About a minute ago hopeful_roentgen
╰─ docker ps -aq
2ee64876800b
5.4 删除容器
docker rm centos
╰─ docker rm -f c338b3c19b4d
c338b3c19b4d
# 删除所有容器
docker rm -f $(docker ps -aq)
╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ ✔ at 16:06:56 ▓▒░
╰─ docker rm -f $(docker ps -aq)
2ee64876800b
docker ps -a -q|xargs docker rm
5.5 启动和停止容器
docker start 容器id # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止
docker kill 容器id # 强制停止
5.6 后台启动容器
╰─ docker run -d centos
4b303d957914ddc63436af5148641b88fcf64b5d62bf5ff05d9a5e0f28ed66ee
╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ ✔ at 16:11:09 ▓▒░
╰─ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ ✔ at 16:11:15 ▓▒░
╰─ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b303d957914 centos "/bin/bash" 9 seconds ago Exited (0) 8 seconds ago ecstatic_hermann
╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ ✔ at 16:11:18 ▓▒░
╰─
启动后发现立马停掉,因为docker使用后台运行,必须要有一个前台进程,否则docker会停止。
5.7 查看日志
docker logs
╰─ 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)
╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ ✔ at 16:13:11 ▓▒░
╰─
docker -tf --tail 10 容器id # 持续输出查看前10条日志内容
5.8 容器进程信息
docker top
╰─ docker top 6e956ce062dc
UID PID PPID C STIME TTY TIME CMD
root 3084 3058 0 08:22 ? 00:00:00 /bin/bash
╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ ✔ at 16:24:24 ▓▒░
╰─
╰─ 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 ╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ ✔ at 16:25:36 ▓▒░ ╰─
```shell
╰─ docker inspect 6e956ce062dc
[
{
"Id": "6e956ce062dce8f0be90c8db602cf4dc15e41660ba0f598e4053a6e832b0f0a0",
"Created": "2021-12-19T08:22:49.678266479Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3084,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-12-19T08:22:50.056486062Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/6e956ce062dce8f0be90c8db602cf4dc15e41660ba0f598e4053a6e832b0f0a0/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/6e956ce062dce8f0be90c8db602cf4dc15e41660ba0f598e4053a6e832b0f0a0/hostname",
"HostsPath": "/var/lib/docker/containers/6e956ce062dce8f0be90c8db602cf4dc15e41660ba0f598e4053a6e832b0f0a0/hosts",
"LogPath": "/var/lib/docker/containers/6e956ce062dce8f0be90c8db602cf4dc15e41660ba0f598e4053a6e832b0f0a0/6e956ce062dce8f0be90c8db602cf4dc15e41660ba0f598e4053a6e832b0f0a0-json.log",
"Name": "/sad_leavitt",
"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/29da248d6fde28ec644a3807a807ed4d135133fd197026680ea94b5b2886535f-init/diff:/var/lib/docker/overlay2/456b7971b92c2b3ffe6e4c08d26b11a1548b019b60d104e610d8ad8aa4eb56ad/diff",
"MergedDir": "/var/lib/docker/overlay2/29da248d6fde28ec644a3807a807ed4d135133fd197026680ea94b5b2886535f/merged",
"UpperDir": "/var/lib/docker/overlay2/29da248d6fde28ec644a3807a807ed4d135133fd197026680ea94b5b2886535f/diff",
"WorkDir": "/var/lib/docker/overlay2/29da248d6fde28ec644a3807a807ed4d135133fd197026680ea94b5b2886535f/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "6e956ce062dc",
"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": "f5b179f43036a825f58a3341e0576a9f49b4cd420d34669e3afd2f258342f6ff",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/f5b179f43036",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "83546bf6fd730460cb5cbf7b094a57d1685242546f38eb2e97729b0d4f7fb698",
"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": "f57fe035f9f08f9159a0f177cdd0a590dcb9607d7a37b47861f52906fcd02463",
"EndpointID": "83546bf6fd730460cb5cbf7b094a57d1685242546f38eb2e97729b0d4f7fb698",
"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
}
}
}
}
]
5.10 进入当前正在运行的容器
# 进入正在后台运行的容器
docker exec -it 容器id /bin/bash
╰─ docker exec -it 6e956ce062dc /bin/bash
[root@6e956ce062dc /]#
docker attach 容器id
╰─ docker attach 6e956ce062dc
[root@6e956ce062dc /]#
exec 和 attach区别
- exec 进入容器后开启一个新的会话
- attach 继续当前会话
5.11 容器拷贝文件到主机
docker cp 容器id:容器路径 主机路径
docker cp
╰─ 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
╭─░▒▓ ~/Downloads/darwin_amd64 ▓▒░──────────────────────────────────────────────────────────────░▒▓ ✔ at 16:34:30 ▓▒░
╰─
╭─░▒▓ ∅ /home ▓▒░─────────────────────────────────────────────────────────────────────────────░▒▓ 1 ✘ at 16:38:16 ▓▒░
╰─ docker cp 7834712eefa5:/home/log.log ~/Documents/
╭─░▒▓ ∅ /home ▓▒░───────────────────────────────────────────────────────────────────────────────░▒▓ ✔ at 16:39:28 ▓▒░
╰─ cd ~/Documents
╭─░▒▓ ~/Documents ▓▒░───────────────────────────────────────────────────────────────────────────░▒▓ ✔ at 16:39:37 ▓▒░
╰─ ls
21-05-12-14-52-35_1.gif google-extensions themes
21级 install.sh vnote导出
21级.zip iterm2Theme 草稿纸-81.jpg
Adobe leanote.dockerfile 未命名.gif
Apifox log.log 数学重点
Axure lzfblog-cn-nginx-0728230452.zip 考前突击(模拟卷).xmind
MWebLibrary mbadolato-iTerm2-Color-Schemes-7f73027 笛卡尔积.key
MuMu共享文件夹 mysql.opml 线性代数.pdf
Profie mysql.svg 软件工程.xmind
SiYuan mysql基础.xmind 期末知识点.xmind
ToDesk quickstart-app 毕业登记表p1.pdf
docs score.sql 专升本数学考点.xmind
fonts scripts
╭─░▒▓ ~/Documents ▓▒░───────────────────────────────────────────────────────────────────────────░▒▓ ✔ at 16:39:38 ▓▒░
╰─