docker基础及命令
Posted Jasx_双刃_亮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker基础及命令相关的知识,希望对你有一定的参考价值。
1.启动docker
sudo systemctl start docker
sudo systemctl restart docker
sudo systemctl stop docker
sudo systemctl status docker
2.查看docker信息
sudo docker info
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 2
Server Version: 17.03.1-ce
Storage Driver: devicemapper
Pool Name: vgdocker-thinpool
Pool Blocksize: 524.3 kB
Base Device Size: 10.74 GB
Backing Filesystem: xfs
Data file:
Metadata file:
Data Space Used: 2.708 GB
Data Space Total: 20.4 GB
Data Space Available: 17.69 GB
Metadata Space Used: 1.917 MB
Metadata Space Total: 213.9 MB
Metadata Space Available: 212 MB
Thin Pool Minimum Free Space: 2.039 GB
Udev Sync Supported: true
Deferred Removal Enabled: true
Deferred Deletion Enabled: true
Deferred Deleted Device Count: 0
Library Version: 1.02.135-RHEL7 (2016-11-16)
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 4ab9917febca54791c5f071a9d1f404867857fcc
runc version: 54296cf40ad8143b62dbcaa1d90e520a2136ddfe
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-123.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.69 GiB
Name: wgl
ID: 2GXU:7CL7:ETAC:BTYX:M4V6:JSMV:3CIJ:4OZC:KKKX:D5IV:DVBX:T3KV
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
3.运行第一个容器
sudo docker run --name wgl_centos -i -t centos /bin/bash
sudo docker rename oldername newname
-i 保证容器中stdin是开启的,尽管我们并没有附着到容器中,是交互式shell的半边天;-t 是另一半边天,他告诉docker为要创建的容器分配一个伪tty终端。如要在命令行下创建一个我们能与之经行交互的容器,而不是一个运行后台服务的容器,这两个是基本的了。
4.查看宿主机容器列表
sudo docker ps -a #显示全部容器
sudo docker ps -l #列出最后一个运行的容器,也可以--format进一步控制显示
sudo docker ps -n x #显示最后x个容器。
5.启动,停止容器
sudo docker start(restart,stop) 容器ID(容器name)
6,附着到容器上
sudo docker attach 容器
例如
sudo docker start dreamy_knuth 先启动(启动后会沿用run命令时指定的参数运行)
sudo docker attach dreamy_knuth 后附着到容器,【及进入交互界面】
相当于
sudo docker start dreamy_knuth
sudo docker exec -i -t dreamy_knuth /bin.bash(可用于打开一个守护式容器)
7,创建守护式容器
sudo docker run --name wgl_centos_daemon -d centos /bin/sh -c "while true;do echo hello world;sleep 3;done"
在后台运行,只返回容器ID。
-d 在后台运行
8.获取守护式容器的日志
sudo docker logs 容器 #输出最后几条后返回
sudo docker logs -f 容器 #监控日志
sudo docker logs --tail 10 容器 #获取容器日志最后10条
sudo docker logs --tail 0 -f 容器 #跟踪最新日志
sudo docker logs -ft 容器 #监控日志+时间戳
9.dockers日志驱动
sudo docker run --log-driver="syslog" --name wgl_centos_daemon -d centos /bin/sh -c "while true;do echo hello world;sleep 3;done"
--log-driver=
syslog 将容器日志重新定向到Syslog,禁用dockers logs命令
json-file 默认,用dockers logs命令查看
none 禁用所有容器中的日志,禁用dockers logs命令
10.查看容器进程
sudo docker top wgl_centos_daemon
11.docker统计信息
sudo docker stats [容器1 容器2 容器3 ...]
12.在容器内部运行进程
后台式任务与交互式任务
后台式任务
sudo docker exec -d 53fc4852ce08 touch /opt/wgl.log # -u 为新启动的进程指定属主
通过docker exec 后台命令,可维护,监控,管理正在运行的容器
进入容器(不论是守护式还是交互式)的交互式界面
sudo docker exec -it 53fc4852ce08 /bin/bash
13,自动重启容器
sudo docker run --restart=always --name wgl_centos_daemon -d centos /bin/sh -c "while true;do echo hello world;sleep 3;done"
--restart会检查日期的退出代码
always 不认退出代码是什么,都重启
on-failure 退出代码非0才重启,还可以设置重启次数:--restart=on-failure:5
14.深入容器,容器详情
sudo docker inspect 容器
[dwsoft@wgl ~]$ sudo docker inspect wgl_centos_daemon
[
{
"Id": "53fc4852ce081422068efa8bdcbad05d8b51b4fe2ba0a4389c91508dc3fbd50a",
"Created": "2017-05-28T05:35:00.155823247Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo hello world;sleep 3;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 6450,
"ExitCode": 0,
"Error": "",
"StartedAt": "2017-05-28T05:35:01.363485741Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:8140d0c64310d4e290bf3938757837dbb8f806acba0cb3f6a852558074345348",
"ResolvConfPath": "/var/lib/docker/containers/53fc4852ce081422068efa8bdcbad05d8b51b4fe2ba0a4389c91508dc3fbd50a/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/53fc4852ce081422068efa8bdcbad05d8b51b4fe2ba0a4389c91508dc3fbd50a/hostname",
"HostsPath": "/var/lib/docker/containers/53fc4852ce081422068efa8bdcbad05d8b51b4fe2ba0a4389c91508dc3fbd50a/hosts",
"LogPath": "/var/lib/docker/containers/53fc4852ce081422068efa8bdcbad05d8b51b4fe2ba0a4389c91508dc3fbd50a/53fc4852ce081422068efa8bdcbad05d8b51b4fe2ba0a
4389c91508dc3fbd50a-json.log", "Name": "/wgl_centos_daemon",
"RestartCount": 0,
"Driver": "devicemapper",
"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,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"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": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Name": "devicemapper",
"Data": {
"DeviceId": "76",
"DeviceName": "docker-253:1-277138-721bd5780ec0a2800c9db53413e77636e96e787a4b330b9d665c2cf289c01c43",
"DeviceSize": "10737418240"
}
},
"Mounts": [],
"Config": {
"Hostname": "53fc4852ce08",
"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 3;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"build-date": "20170510",
"license": "GPLv2",
"name": "CentOS Base Image",
"vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "dc56c0446f06b8b53b914574c34ec35c09fa38bc9d43d70ba5750aef60915597",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/dc56c0446f06",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "0da7299f69c100fce911e1c6b12f333e56ff30e3ff7a2f799ee595af89265fc8",
"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": "23e919387bcf6493c7bdcd91e31b8995132fb7f081cfbac7778e855c6a4f6d58",
"EndpointID": "0da7299f69c100fce911e1c6b12f333e56ff30e3ff7a2f799ee595af89265fc8",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
}
}
]
也可用-f或--format,查看选定的结果,可查看多个容器,多个结果。
15,删除容器
sudo docker rm 容器
sudo docker rm -f 容器 #删除正在运行的容器
- sudo docker rm `sudo docker ps -a -q` # -q只返回容器ID
以上是关于docker基础及命令的主要内容,如果未能解决你的问题,请参考以下文章