A few years ago I spent a Friday night chasing a bug that only existed in production. Staging was green, same commit in both, yet prod threw TLS errors on every outbound call. Both environments pulled node:latest, and prod had pulled it a week later, after the base image had moved to a new Debian release with a different OpenSSL. Same Dockerfile, two different images.
That night is a big part of why I care about the question new engineers on my team keep asking: what is a Docker image, actually, and what is inside it? Understand that properly and a whole class of “works on my machine” bugs stops being mysterious. Below: what an image is made of, how a Dockerfile becomes layers, how to build, tag and push one, and how I test the app inside it.
A Docker image is a read-only, portable package that contains everything an application needs to run: code, runtime, system libraries, dependencies and configuration. It is built from a Dockerfile as a stack of filesystem layers, and Docker uses it as the template for starting containers.
What a Docker image is
Strip away the tooling and an image is a frozen filesystem plus a small set of instructions about how to start a process on top of it. That’s it. No running process, no memory, no network. It sits on disk or in a registry until something asks for a container.
The analogy I use with juniors is a game cartridge. The cartridge holds the game and its rules, and it never changes no matter how many times you play. Plug it into ten consoles and you get ten independent sessions, each with its own save state. The image is the cartridge. The container is the session.
What’s inside an image
When you pull one of the Docker container images from Docker Hub, three kinds of things come down the wire:
- Filesystem layers. Each layer is a tar archive of changes compared with the layer below it: files added, modified or deleted. Stacked together, they form the root filesystem your process sees.
- An image config. This JSON document holds the metadata: environment variables, working directory, default user, exposed ports, and the CMD or ENTRYPOINT that runs when a container starts.
- A manifest. This ties the config and the layers together and lists each one by its SHA-256 digest, so every piece is content-addressed and verifiable.
All of this follows the image specification maintained by the Open Container Initiative, which is why an image built with Docker runs fine under containerd, Podman or Kubernetes. Because layers are addressed by digest, two images built on the same base share those base layers on disk and in the registry.
This is also the part that answers “how does Docker work” for most people. The engine unpacks those layers into a union filesystem, adds a thin writable layer on top, reads the config, and starts your process inside isolated Linux namespaces. Our guide to containerization covers why that isolation matters.
Image layers and the Dockerfile
You describe an image in a Dockerfile. If you’ve wondered what is a Dockerfile in practical terms, it’s a plain text file of instructions the builder runs top to bottom, each one producing a new layer or changing the image config. The Dockerfile reference lists them all, but most services use about ten.
Here’s a Dockerfile example close to what I’d ship for a small Node.js API:
FROM brings in the base image’s layers. COPY and RUN each add a filesystem layer. WORKDIR may add a tiny one if the directory doesn’t exist yet. ENV, EXPOSE, USER and CMD don’t add files at all, they only change the config.

Layer caching and instruction order
The builder caches every layer. On each build it checks whether an instruction and its inputs (for COPY, a checksum of the files) are unchanged, and if so reuses the cached layer. The moment one instruction misses the cache, every instruction after it rebuilds too.
That last sentence cost my team a lot of CI minutes. An early version of our Dockerfile had COPY . . straight after WORKDIR, followed by RUN npm ci. Every typo fix invalidated that layer, so npm reinstalled about 900 packages from scratch. Builds took just over four minutes. Copying package.json and the lockfile first, installing, and copying the source last brought warm builds down to around 20 seconds. Same files. Different order.
Put what changes least near the top (base image, system packages, dependency manifests) and what changes most near the bottom (source code). Watch for sneaky cache busters too. A colleague once used ARG BUILD_DATE in a LABEL near the top, and every build became a cold build for a week before anyone noticed.
CMD vs ENTRYPOINT
Both define what runs when a container starts. The Dockerfile CMD vs ENTRYPOINT difference is how easily each gets replaced.
CMD is a default that’s easy to override. Anything you put after the image name in docker run replaces it completely:
ENTRYPOINT fixes the executable, and whatever you pass to docker run is appended as arguments. When both are present, CMD becomes the default arguments for ENTRYPOINT:
I use plain CMD for application images and ENTRYPOINT plus CMD for images that behave like a single CLI tool. Either way, use the exec (JSON array) form. The shell form wraps your process in /bin/sh -c, so it never receives SIGTERM directly and your graceful shutdown code never runs.
What EXPOSE actually does (and doesn’t do)
Dockerfile EXPOSE is documentation. It records in the image config that the app expects to listen on a port, and that’s about all. It doesn’t publish anything to your host, and it doesn’t make your app listen.
Publishing happens at run time with -p:
That maps port 8080 on your machine to port 3000 in the container. The one thing EXPOSE changes mechanically is docker run -P (capital P), which publishes every exposed port to a random high port on the host.
I once spent an hour convinced EXPOSE was broken because curl localhost:8080 kept failing despite EXPOSE and -p. The app was binding to 127.0.0.1, which inside a container means only the container can reach it. Binding to 0.0.0.0 fixed it. If a published port refuses connections, check the bind address before you blame Docker.
Building, tagging and pushing an image
Once the Dockerfile is in place, the build loop is a handful of commands. From the project root:
docker history is the command I wish I’d learned on day one. When an image is mysteriously huge, it shows which instruction added the weight.
A tag is just a readable pointer to an image ID. To push, the tag must include your repository (and the registry host, outside Docker Hub), so add a second tag for the same image:
Since that Friday night, I tag every image with a version number or the Git commit SHA and never deploy latest. latest means whatever someone pushed last.

When to use docker build –no-cache
The cache only looks at instruction text and copied files, not the outside world. RUN apt-get update && apt-get install -y curl will happily reuse a layer from three months ago, missing every security patch released since.
--no-cache rebuilds every layer, and --pull fetches the newest version of the base image tag instead of the copy you already have. I use the pair before releases that need OS patches, when CI and my laptop disagree and I suspect a stale layer, and in a nightly job.
Why docker commit exists and why a Dockerfile is better
docker commit snapshots a container’s writable layer into a new image:
It exists for exploration and forensics, like capturing a misbehaving staging container’s exact state to inspect later.
As a way to build images you ship, it’s a trap. I once inherited a “golden image” a former teammate had built by hand and committed. No Dockerfile, roughly 3 GB of mystery. Nobody could rebuild it, so nobody dared patch it. A Dockerfile is reviewable, diffable, rebuildable and cacheable. A committed image is none of those things.
Image vs container: what is the difference
Most Docker vs container confusion comes from people saying “Docker” when they mean “a container”. The difference between a Docker image and a container is the same as the difference between a program file and a running process.
| Image | Container | |
|---|---|---|
| What it is | A read-only template made of layers plus config | A running (or stopped) instance created from an image |
| Mutability | Immutable. Any change produces a new image | Has a thin writable layer. Changes are lost when it’s removed |
| Storage | Local image store or a registry, with shared layers deduplicated | A writable layer on the host, stacked on the image’s layers |
| Lifecycle | Built or pulled, tagged, pushed, kept until you delete or prune it | Created, started, stopped, restarted, removed |
| Command to create | docker build (or docker pull) |
docker run (or docker create) |

One image, many containers
Because the image is read-only, you can start as many containers from it as you like. Each one gets its own writable layer, process tree and network identity:
Both containers share the image’s layers on disk, so the second one costs almost nothing extra in storage. Write a file inside api-a and api-b never sees it. That’s how orchestrators scale a service. For where Docker ends and an orchestrator begins, see our comparison of Kubernetes vs Docker walks through it, and for running several containers together locally, our guide to Docker Compose.

Image best practices
Most of what I know about keeping docker images healthy, I learned by shipping bad ones first.
Start from a small base. Alpine, slim Debian variants and distroless images carry far fewer packages than full distributions. That means fewer CVEs to triage and faster pulls.
Use multi-stage builds. The biggest single win I’ve had was on a TypeScript API whose image had grown to 1.2 GB. It shipped the full Node image, dev dependencies and the TypeScript compiler to production. A multi-stage build took it to about 140 MB without changing a line of application code:
The second stage starts clean and copies only the compiled output across with COPY --from=build.
Add a .dockerignore. Without one, COPY . . sends node_modules, .git and your local .env to the builder. Mine usually starts like this:
Pin versions. Use a specific base tag like node:24.11-alpine rather than node:latest, and for builds that must be byte-for-byte reproducible, pin the image digest with @sha256:.
Never bake secrets into layers. I once found an npm token in a public image because someone had done COPY .npmrc . and then RUN rm .npmrc a few lines later. The file was gone from the final view but still sat in the earlier layer, extractable by anyone who pulled the image. Use a build secret instead, mounted only for that one instruction:
Scan every image. Run docker scout cves orders-api:1.4.0 or the open-source trivy image orders-api:1.4.0 in CI so a vulnerable base never reaches the registry unnoticed.
Testing the app inside the image
Here’s the gap nobody warns you about. Unit tests run against source code, not the image. And the image is where things break: a base image bump changes OpenSSL, a native module behaves differently on Alpine’s musl libc, or a file you excluded in .dockerignore turns out to be needed at runtime. Every build is a new artifact, and its API can change in ways no unit test catches.
I want to hit the actual container with realistic requests and compare responses with what it returned before. That’s the approach I use to test Dockerized apps with Keploy. You run the container once under Keploy in record mode and send it real traffic. Keploy captures each API call as a test case, and it also records the calls your app makes to its dependencies (Postgres, Redis, other HTTP services) as mocks. In test mode, Keploy starts the new image, replays the recorded requests, serves the mocked dependency responses, and diffs what comes back against what was recorded.
Install Keploy and create the network the app container will join:
Record against the image you just built:
While that runs, exercise the API with curl or Postman, or point some real traffic at it:
Stop recording with Ctrl+C. Keploy writes the test cases and mocks into a keploy/ directory in your project, which you commit next to the Dockerfile.
Then, in CI, after every new build:
The --delay flag gives the container a few seconds to boot before replay starts, so raise it if your app is slow to come up. If the 1.5.0 image returns a different status code, drops a field or changes a payload, the run fails and shows you the diff. Timestamps and generated IDs can be marked as noise in keploy.yml. Recording once and replaying on every build catches exactly the base image drift that cost me that Friday night.

FAQs
Is a Docker image the same as a container?
No. An image is a read-only template made of filesystem layers and configuration, while a container is a running instance created from that image with its own writable layer. You can delete a container without touching the image, and start new containers from the image at any time.
Where are Docker images stored?
On Linux, Docker keeps images in its local image store under /var/lib/docker by default, or under containerd’s directory if your engine uses the containerd image store. On macOS and Windows, Docker Desktop stores them inside the disk of its Linux VM. To share images, you push them to a registry such as Docker Hub or a private registry.
What is the difference between a Dockerfile and an image?
A Dockerfile is a text file of build instructions, and an image is the artifact docker build produces from those instructions. Think of it as source code versus a compiled binary. You commit the Dockerfile to Git and push the image to a registry.
Can one image run many containers?
Yes. Every docker run against the same image creates a new, independent container that shares the image’s read-only layers and gets its own writable layer. That’s how services scale horizontally: one image, many identical containers.
If your services already ship as images, the next step is making sure every new build still serves the same API. Keploy records real traffic once and turns it into tests and dependency mocks you can replay against each image in CI, without writing test code by hand. See how it fits your pipeline in our guide to API testing with Keploy.

