Starting, Stopping, Inspecting and Deleting Containers
By the end of this exercise, you should be able to:
- Restart containers which have exited
- Distinguish between stopping and killing a container
- Fetch container metadata using
docker container inspect - Delete containers
Starting and Restarting Containers
Start by running a IIS web server in the background, and check that it's really running:
PS: node-0 Administrator> docker container run -d ` --name demo microsoft/nanoserver ping 8.8.8.8 -t PS: node-0 Administrator> docker container lsNote how we called the container
demofor easier identification later on.Stop the container using
docker container stop, and check that the container is indeed stopped:PS: node-0 Administrator> docker container stop demo PS: node-0 Administrator> docker container ls -a
Inspecting a Container
Start your
democontainer again, then inspect the container details usingdocker container inspect:PS: node-0 Administrator> docker container start demo PS: node-0 Administrator> docker container inspect demoYou get a JSON object describing the container's config, metadata and state.
Find the container's IP and long ID in the JSON output of
inspect. If you know the key name of the property you're looking for, try piping toselect-string:PS: node-0 Administrator> docker container inspect demo | select-string IPAddressThe output should look similar to this:
"SecondaryIPAddresses": null, "IPAddress": "", "IPAddress": "172.20.137.22",Now try to use
select-stringforCmd, the main process being run by this container.select-string's simple text search doesn't always return helpful results:PS: node-0 Administrator> docker container inspect demo | select-string Cmd "Cmd": [A more powerful way to filter this JSON is with the
--formatflag. Syntax follows Go's text/template package: http://golang.org/pkg/text/template/. For example, to find theCmdvalue we tried toselect-stringfor above, instead try:PS: node-0 Administrator> docker container inspect --format='{{.Config.Cmd}}' demo [ping 8.8.8.8 -t]This time, we get a the value of the
Config.Cmdkey from theinspectJSON.Keys nested in the JSON returned by
docker container inspectcan be chained together in this fashion. Try modifying this example to return the IP address you selected previously.Finally, we can extract all the key/value pairs for a given object using the
jsonfunction:PS: node-0 Administrator> docker container inspect --format='{{json .Config}}' demoTry adding
| jqto this command to get the same output a little bit easier to read.
Deleting Containers
Start three containers in background mode, then stop the first one.
List only exited containers using the
--filterflag we learned earlier, and the optionstatus=exited.Delete the container you stopped above with
docker container rm, and do the same listing operation as above to confirm that it has been removed:PS: node-0 Administrator> docker container rm <container ID> PS: node-0 Administrator> docker container lsNow do the same to one of the containers that's still running; notice
docker container rmwon't delete a container that's still running, unless we pass it the force flag-f. Delete the second container you started above:PS: node-0 Administrator> docker container rm -f <container ID>Try using the
docker container lsflags we learned previously to remove the last container that was run, or all stopped containers. Recall that you can pass the output of one shell commandcmd-Ainto a variable of another commandcmd-Bwith syntax likecmd-B $(cmd-A).
Conclusion
In this exercise, you saw the basics of managing the container lifecycle. Containers can be restarted when stopped, and are only truly gone once they've been removed.
Also keep in mind the docker container inspect command we saw, for examining container metadata, state and config; this is often the first place to look when trying to troubleshoot a failed container.