Sub topic 6
Checking the result: image ls, history, and inspect
This chapter assumes you can write a Dockerfile, run docker build -t, and know that an image is a stack of layers. After a build, three commands tell you what you got: docker image ls, docker history, and docker image inspect.
List images with docker image ls
docker image ls shows the images stored on your machine. Pass a repository name to filter the list.
docker image ls myappTypical output looks like this:
REPOSITORY TAG IMAGE ID CREATED SIZE
myapp 1.0 3f2a9c1d7b44 2 minutes ago 145MB
myapp latest 3f2a9c1d7b44 2 minutes ago 145MBBoth rows share one image ID. That means 1.0 and latest are two tags on the same image, not two copies. The SIZE column is the image's logical size. Layers shared with other images are stored only once on disk, so the sum of all sizes overstates real disk use.
Read the layers with docker history
docker history lists the layers of an image, newest first, along with the instruction that created each one.
docker history myapp:1.0Each row has a creation time, the instruction, and the layer size. Read it for two things:
- Large layers. A
RUN pip installlayer of 80 MB tells you where the size comes from. ACOPY . .layer that is far larger than your source code suggests the build context included files it should not have. Check your.dockerignore. - Zero-byte layers. Instructions such as
WORKDIRandCMDonly change metadata, so they add 0 B.
The instruction text is truncated by default. Add --no-trunc to see the full command.
docker history --no-trunc myapp:1.0Examine metadata with docker image inspect
docker image inspect prints a JSON document describing the image: its ID, tags, layer digests, architecture, and the runtime configuration set by the Dockerfile.
docker image inspect myapp:1.0The full output is long. Use --format with a Go template to pull out one field.
docker image inspect --format '{{.Config.Cmd}}' myapp:1.0
docker image inspect --format '{{.Architecture}} {{.Os}}' myapp:1.0
docker image inspect --format '{{json .Config.Env}}' myapp:1.0These answer common questions after a build:
| Question | Field |
|---|---|
| What runs by default? | `.Config.Cmd` and `.Config.Entrypoint` |
| Which environment variables are baked in? | `.Config.Env` |
| Which CPU architecture is it? | `.Architecture` |
| Which directory do commands start in? | `.Config.WorkingDir` |
| Which tags point to this image? | `.RepoTags` |
A short verification routine
After a build, confirm the tag exists with image ls, look for oversized layers with history, and verify the default command and environment with inspect. This takes seconds and catches most mistakes before you push.
Common errors
No such image. The tag you typed does not match. Rundocker image lsand compare the repository and tag exactly.- Missing tag in the listing. Without a tag, Docker uses
latest. If you builtmyapp:1.0only,docker image inspect myappfails. - Surprising size. The cause is usually a large
COPYlayer or a package install that left caches behind. Usehistoryto find it.
Exercise
You run docker image ls myapp and see two rows, myapp:1.0 and myapp:latest, both with image ID 3f2a9c1d7b44. Which command shows the default command the image will run, and what does the shared ID tell you?
Show answer
Run docker image inspect --format '{{.Config.Cmd}}' myapp:1.0. If the Dockerfile uses ENTRYPOINT, also check .Config.Entrypoint. The shared ID means both tags point to the same image, so there is only one copy of its layers on disk.