Docker image and container relationship¶
The relationship between Docker images and containers is fundamental to the Docker architecture, functioning similarly to the relationship between object-oriented programming classes and instances^[600-developer__docker__docker.md].
Core concepts¶
Images are read-only templates (鏡像、模版) used to create containers^[600-developer__docker__docker.md]. Containers are the runnable instances (實例) created from these images^[600-developer__docker__docker.md]. To visualize the relationship: an image acts as a blueprint or class definition, while the container is the actual running object generated from that blueprint^[600-developer__docker__docker.md].
Lifecycle and interaction¶
When a user executes a command like docker run, the Docker engine retrieves the required image (often from a Docker registry) and initializes a container^[600-developer__docker__docker.md]. A single image can spawn multiple containers, each operating as an isolated process with its own filesystem and namespace, yet sharing the same underlying kernel and image resources^[600-developer__docker__docker.md].
Dependency management¶
Due to this dependency, specific operational constraints exist. For example, you cannot remove an image using docker rmi if it is currently being used by a container, even if that container is stopped^[600-developer__docker__docker.md]. The container must be removed (docker rm) before the image can be deleted^[600-developer__docker__docker.md].
Related concepts¶
Sources¶
^[600-developer__docker__docker.md]