这里docker常用的命令用法。
Clone镜像到本地文件系统
docker pull image_name
示例:
docker pull training/webapp
运行结果信息输出:
Using default tag: latest
latest: Pulling from training/webapp
e190868d63f8: Pull complete
909cd34c6fd7: Pull complete
0b9bfabab7c1: Pull complete
a3ed95caeb02: Pull complete
10bbbc0fc0ff: Pull complete
fca59b508e9f: Pull complete
e7ae2541b15b: Pull complete
9dd97ef58ce9: Pull complete
a4c1b0cb7af7: Pull complete
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for training/webapp:latest
docker run -d -P training/webapp python app.py
Usage:
* -d daemon 后台运行模式
* -P 将容器内部使用的网络端口映射到我们使用的主机上。
查看docker进程
docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c47561fa4f2b training/webapp "python app.py" 7 seconds ago Up 6 seconds 0.0.0.0:32768->5000/tcp agitated_ardinghelli
这里是container id是之后进行操作的唯一标示;这里的PORTS是本地32768端口映射到来docker里面的5000端口。这里可以访问浏览器http://localhost:32768 ,可以看到hello world 的信息:
docker stop container_id
停掉container_id
docker start container_id
启动container_id
docker port container_id
查看container 的端口映射, docker port –> local system port,信息输出:
5000/tcp -> 0.0.0.0:32768
docker logs -f container_id
查看container的日志,示例结果输出:
docker logs -f c47561fa4f2b
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
172.17.0.1 - - [21/Jan/2018 09:29:16] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [21/Jan/2018 09:29:16] "GET /favicon.ico HTTP/1.1" 404 -
172.17.0.1 - - [21/Jan/2018 09:29:16] "GET /apple-touch-icon-precomposed.png HTTP/1.1" 404 -
172.17.0.1 - - [21/Jan/2018 09:29:16] "GET /apple-touch-icon.png HTTP/1.1" 404 -
查看inspect的信息,
docker inspect container_id
$ docker inspect c47561fa4f2b
[
{
"Id": "c47561fa4f2b99634405bf9f9b62936d81259e90807ec013a24de069ac5bbfa2",
"Created": "2018-01-21T09:26:48.053246343Z",
"Path": "python",
"Args": [
"app.py"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2124,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-01-21T09:26:48.72844003Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:6fae60ef344644649a39240b94d73b8ba9c67f898ede85cf8e947a887b3e6557",
"ResolvConfPath": "/var/lib/docker/containers/c47561fa4f2b99634405bf9f9b62936d81259e90807ec013a24de069ac5bbfa2/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/c47561fa4f2b99634405bf9f9b62936d81259e90807ec013a24de069ac5bbfa2/hostname",
"HostsPath": "/var/lib/docker/containers/c47561fa4f2b99634405bf9f9b62936d81259e90807ec013a24de069ac5bbfa2/hosts",
"LogPath": "/var/lib/docker/containers/c47561fa4f2b99634405bf9f9b62936d81259e90807ec013a24de069ac5bbfa2/c47561fa4f2b99634405bf9f9b62936d81259e90807ec013a24de069ac5bbfa2-json.log",
"Name": "/agitated_ardinghelli",
"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,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": true,
"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,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/529d5b9ab091439c3e0a10e645f2843983fdacb01cc5676068ba701c47d7e173-init/diff:/var/lib/docker/overlay2/97a5d28119c333ab972f0e2407c72a3ae7c2551fcef3f77c455064ca05096bbb/diff:/var/lib/docker/overlay2/4204ba49e7b302c81898c0fa4a2364cd1452fff2a3f47d43d74efb2877f432f7/diff:/var/lib/docker/overlay2/9049cc3f2ea4036d75ff28bc9c1bddbc774668e357e14570a68c64e30f9b2fc7/diff:/var/lib/docker/overlay2/009b2f106ec45c65b5196bf7bf7d3dc57ae2403d212c0c45712d3ffd0163d72d/diff:/var/lib/docker/overlay2/797d22e35947fff35b4189525dea31127e9ed83c351d7c777ec760eb759a5e40/diff:/var/lib/docker/overlay2/b10e5681aca4593f2cdf4cba6d687552fc8a13c4d32baba1755b5b0353ac94e3/diff:/var/lib/docker/overlay2/3b66779ffd10e58e7a9bfc2e8be80a1a3793f0b96a5eace087b82ecaed85af34/diff:/var/lib/docker/overlay2/42bc4603ace3030ff9f9ddc7acf9692ce08f2102b1f83334e6c86e94e6de981a/diff:/var/lib/docker/overlay2/9e0c54dc045b56511f22a6100b12e9b631beefb32325992d30726acf56647bee/diff:/var/lib/docker/overlay2/d0becac715a5b158f8f85c215829273c022ecf9e6379674a728e3c004f30765c/diff:/var/lib/docker/overlay2/c4d6f26ccc195b164850ea52aec7af87144013efb408daa6d2a735c6695d2e55/diff:/var/lib/docker/overlay2/21864ebd5bbc8612875741441fc6d365532d8bce22fc61c63119bb72bc9515b5/diff:/var/lib/docker/overlay2/29d1eff1f8a990b3d8ae19a9f42b767673f39812e62f70628017beec5e87a387/diff",
"MergedDir": "/var/lib/docker/overlay2/529d5b9ab091439c3e0a10e645f2843983fdacb01cc5676068ba701c47d7e173/merged",
"UpperDir": "/var/lib/docker/overlay2/529d5b9ab091439c3e0a10e645f2843983fdacb01cc5676068ba701c47d7e173/diff",
"WorkDir": "/var/lib/docker/overlay2/529d5b9ab091439c3e0a10e645f2843983fdacb01cc5676068ba701c47d7e173/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "c47561fa4f2b",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"5000/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"python",
"app.py"
],
"Image": "training/webapp",
"Volumes": null,
"WorkingDir": "/opt/webapp",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "4161bc0112c1594a3ef3c702cfe9abb6861299352fdf2452f39d325dfcdbbcf2",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"5000/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "32768"
}
]
},
"SandboxKey": "/var/run/docker/netns/4161bc0112c1",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "b7c11e07fd1614316b731de10172e05148788590315c1b0c6b1cfe836d9fdf9a",
"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": "f0f67fb4dc9c7af347d3334c7ed4b329ad514223ad366751a2f416ebe7fa1f55",
"EndpointID": "b7c11e07fd1614316b731de10172e05148788590315c1b0c6b1cfe836d9fdf9a",
"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
}
}
}
}
]
查看镜像列表
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
learn/ping latest 693c965e5e61 40 minutes ago 140MB
training/webapp latest 6fae60ef3446 2 years ago 349MB
learn/tutorial latest a7876479f1aa 4 years ago 128MB
删除image
docker rmi docker_name
docker rmi training/webapp
Untagged: training/webapp:latest
Untagged: training/webapp@sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Deleted: sha256:6fae60ef344644649a39240b94d73b8ba9c67f898ede85cf8e947a887b3e6557
Deleted: sha256:875bde2b9e2d99e7c1362993645a474fe621475c6fc1b1623c9ed5312b7bdeae
Deleted: sha256:bbdb5ee3757ef8f2633694016df5840fc3410422b37c22f98c0300e295ce75cc
Deleted: sha256:d718446240e3f48a904ad4bbf2a1f61737c5d70df35b8210d674a9517cdc9803
Deleted: sha256:a890440f4933412f9aafb056eb2f07f2276ed756631a81e960d4a8a6de5857a3
Deleted: sha256:68a74799a9e67953725058ef21a530f100025088943446aa60c73fba7beebd47
Deleted: sha256:b23e4b6b440d0e9ab4ffd7852fbf81edd6d5eb606e24d4950d83502e14af2856
Deleted: sha256:f115b0453c71fb4d21fdb6f579201984bd5033ae28ed5908978576a19282418b
Deleted: sha256:b0da82df3229cd06a2992449f2310caaa42f09fdfb088f4a98c5ea587ea85c7e
Deleted: sha256:f6f162dad6e64715d3d07e21d4574733860a557f2f89228d07909c1f6f04e882
Deleted: sha256:088f9eb16f16713e449903f7edb4016084de8234d73a45b1882cf29b1f753a5a
Deleted: sha256:799115b9fdd1511e8af8a8a3c8b450d81aa842bbf3c9f88e9126d264b232c598
Deleted: sha256:3549adbf614379d5c33ef0c5c6486a0d3f577ba3341f573be91b4ba1d8c60ce4
Deleted: sha256:1154ba695078d29ea6c4e1adb55c463959cd77509adf09710e2315827d66271a