In this article, I am going to list few commands that I find useful when working with Docker. These are bare minimums that you need to know.
Note: If you have missed my previous articles on Docker, you can find them here.
Application deployment models evolution.
Getting started with Docker.
Docker file and images.
Publishing images to Docker Hub and re-using them
Get a shell in the container
PS C:\WINDOWS\system32> docker container exec -it webfromrepo bash root@943e0c971d14:/usr/local/apache2# ls bin build cgi-bin conf error htdocs icons include logs modules
The top command
PS C:\WINDOWS\system32> docker container top webfromrepo
PID USER TIME COMMAND
7016 root 0:00 httpd -DFOREGROUND
7051 bin 0:00 httpd -DFOREGROUND
7052 bin 0:00 httpd -DFOREGROUND
7053 bin 0:00 httpd -DFOREGROUND
View container logs
PS C:\WINDOWS\system32> docker container logs webfromrepo
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Fri Jul 24 07:32:58.752512 2020] [mpm_event:notice] [pid 1:tid 139846529811584] AH00489: Apache/2.4.43 (Unix) configured -- resuming normal operations
Starting and stopping containers
PS C:\WINDOWS\system32> docker container stop webfromrepo
webfromrepo
PS C:\WINDOWS\system32> docker container start webfromrepo
webfromrepo
Inspecting a container
PS C:\WINDOWS\system32> docker container inspect webfromrepo
[
{
"Id": "943e0c971d1460dcc66532912ee944220197cadb8256f58e8bc2e580a998a299",
"Created": "2020-07-24T07:32:58.210858605Z",
"Path": "httpd-foreground",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 28404,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-07-24T08:24:16.794858604Z",
"FinishedAt": "2020-07-24T08:22:19.15504905Z"
},
"Image": "sha256:47c1b5e7dfc16922eb3121fdea4402fe1c05313fbd45221216e69f9f2f738602",
"ResolvConfPath": "/var/lib/docker/containers/943e0c971d1460dcc66532912ee944220197cadb8256f58e8bc2e580a998a299/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/943e0c971d1460dcc66532912ee944220197cadb8256f58e8bc2e580a998a299/hostname",
"HostsPath": "/var/lib/docker/containers/943e0c971d1460dcc66532912ee944220197cadb8256f58e8bc2e580a998a299/hosts",
"LogPath": "/var/lib/docker/containers/943e0c971d1460dcc66532912ee944220197cadb8256f58e8bc2e580a998a299/943e0c971d1460dcc66532912ee944220197cadb8256f58e8bc2e580a998a299-json.log",
"Name": "/webfromrepo",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "80"
}
]
},
"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": [
55,
237
],
"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/a297ec90c265b81ce63181fb7f48cd0e1ec3ea7921ca3a94cd3aeee8efce1e7c-init/diff:/var/lib/docker/overlay2/2b7442367bd4561f4b2716c119351f88fef7add06cd45489ca282939a35dcf6c/diff:/var/lib/docker/overlay2/37d62ed53515d8e9bf95316f0dee6205ccab0c04b03b77ddafe017bd58a585ed/diff:/var/lib/docker/overlay2/1432be8296e341317033ee100de06cf480f4fd3203b2d137c845bee239735fcf/diff:/var/lib/docker/overlay2/a96969490d6d299d036e7d2034bf16a722656fe6df163e8679bed2e5079c0249/diff:/var/lib/docker/overlay2/61af245b01464dd504c22cbfcc7663a83480ca3058e340e42c71540b9924c3d2/diff:/var/lib/docker/overlay2/0ea1875701a177445f58d04a36dbb46a0b547ea693c670bc65a646bd3b6780c8/diff",
"MergedDir": "/var/lib/docker/overlay2/a297ec90c265b81ce63181fb7f48cd0e1ec3ea7921ca3a94cd3aeee8efce1e7c/merged",
"UpperDir": "/var/lib/docker/overlay2/a297ec90c265b81ce63181fb7f48cd0e1ec3ea7921ca3a94cd3aeee8efce1e7c/diff",
"WorkDir": "/var/lib/docker/overlay2/a297ec90c265b81ce63181fb7f48cd0e1ec3ea7921ca3a94cd3aeee8efce1e7c/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "943e0c971d14",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"tach",
"PATH=/usr/local/apache2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"HTTPD_PREFIX=/usr/local/apache2",
"HTTPD_VERSION=2.4.43",
"HTTPD_SHA256=a497652ab3fc81318cdc2a203090a999150d86461acff97c1065dc910fe10f43",
"HTTPD_PATCHES="
],
"Cmd": [
"httpd-foreground"
],
"Image": "wizardonwire/mycoolwebsite:latest",
"Volumes": null,
"WorkingDir": "/usr/local/apache2",
"Entrypoint": null,
"OnBuild": null,
"Labels": {},
"StopSignal": "SIGWINCH"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "99f619051c8c069f7dcf3be7ebab5525331de214dd4562264cc68e2b8ae2283a",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "80"
}
]
},
"SandboxKey": "/var/run/docker/netns/99f619051c8c",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "d14ee1cae15db6ee1dd6862fdeb71dfa90b0cf913f0f6e370baf33ecc976ee48",
"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": "3bcd202a7763bf80b704ba56721dae5174ed8b00c6601d304a0a849c4bce2a07",
"EndpointID": "d14ee1cae15db6ee1dd6862fdeb71dfa90b0cf913f0f6e370baf33ecc976ee48",
"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
}
}
}
}
]
Inspect gives JSON dump of container data. To view a particular field
PS C:\WINDOWS\system32> docker inspect webfromrepo --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $INSTANCE_ID
172.17.0.2
Get container statististics
PS C:\WINDOWS\system32> docker container stats webfromrepo
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
943e0c971d14 webfromrepo 0.00% 7.391MiB / 3.826GiB 0.19% 2.77kB / 924B 0B / 0B 109
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
943e0c971d14 webfromrepo 0.00% 7.391MiB / 3.826GiB 0.19% 2.77kB / 924B 0B / 0B 109
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
943e0c971d14 webfromrepo 0.00% 7.391MiB / 3.826GiB 0.19% 2.77kB / 924B 0B / 0B 109
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
943e0c971d14 webfromrepo 0.00% 7.391MiB / 3.826GiB 0.19% 2.77kB / 924B 0B / 0B 109
A good cheatsheet of docker commands can be found here
That's all Folks!!
Comments