An image is a read-only template; a container is a running (or stopped) instance created from that image. The relationship is similar to a class and an object, or a program file and a running process. Docker image - A packaged, immutable snapshot of everything needed to run an application: base operating system files, runtime (for example the .NET or Java runtime), application code, dependencies and default configuration (environment variables, command to run). - Built from a Dockerfile with docker build. Each instruction creates a layer; layers are cached and shared between images, which saves space and speeds up builds. - Stored in registries (Docker Hub, Azure Container Registry, Amazon ECR) and identified by name and tag (orders-api:1.4.2) or by digest (sha256:...). - Images do not run and do not change; to modify one, you build a new image. Docker container - A runnable instance of an image: docker run creates a container with its own isolated filesystem view, process space, network interface and resource limits (implemented with Linux namespaces and cgroups). - Adds a thin writable layer on top of the image layers. Changes made inside the container (new files, logs) go to this layer and are lost when the container is removed — unless you store them in volumes. - Has a lifecycle: created, running, paused, stopped, removed. - You can run many containers from the same image at the same time, each isolated from the others. Commands docker build -t orders-api:1.0 . # create an image docker images # list images docker run -d --name api1 -p 8080:8080 orders-api:1.0 # create and start a container docker run -d --name api2 -p 8081:8080 orders-api:1.0 # another container from the same image docker ps -a # list containers docker stop api1 && docker rm api1 # stop and remove a container docker rmi orders-api:1.0 # remove the image (only if no containers use it) Key differences - State: images are immutable; containers have a writable layer and runtime state. - Purpose: images are for building and distribution; containers are for execution. - Storage: images are stored in registries and local caches; containers exist on a host. - Quantity: one image can produce many containers. Best practices that follow from this - Treat containers as disposable: do not patch running containers; rebuild and redeploy a new image. - Keep persistent data in volumes or external services (databases, blob storage). - Tag images with versions or commit SHAs rather than relying on "latest", and scan images for vulnerabilities. Interview summary: "An image is the immutable blueprint built from a Dockerfile; a container is a live, isolated instance of that image with its own writable layer. You build images once and run as many containers from them as you need."
Ravindra Wadile · · 0 likes