Difference between a Docker image and a container
Understanding the difference between a Docker image and a container is fundamental to working effectively with Docker:
Docker Image
-
Definition:
- Docker Image: A Docker image is a read-only template containing instructions for creating a Docker container. It includes everything needed to run an application - the code, runtime, libraries, environment variables, and configuration files.
-
Purpose:
- Images are used to package and distribute applications and their dependencies in a portable format.
- They serve as a blueprint for creating containers, defining what the container will be and do when it runs.
-
Characteristics:
- Immutable: Once built, images are immutable and do not change during container runtime.
- Layered: Images are composed of multiple layers (e.g., base image layer, application layer, dependencies layer) that are stacked on top of each other.
- Versioned: Images can have different versions (tags) to differentiate between different builds or versions of an application.
-
Example:
- Pulling an image:
docker pull nginx
- Building an image:
docker build -t myapp .
- Viewing local images:
docker images
- Pulling an image:
Docker Container
-
Definition:
- Docker Container: A Docker container is a runnable instance of a Docker image. It encapsulates the application and its dependencies, along with an execution environment (OS, system libraries), and runs as an isolated process on the host machine.
-
Lifecycle:
- Containers are created from images using the
docker run
command. - They can be started, stopped, deleted, and their state can be inspected (
docker ps
,docker inspect
).
- Containers are created from images using the
-
Characteristics:
- Runnable: Containers are the actual instances of images that run applications in an isolated environment.
- Lightweight: Containers share the host system's kernel and resources, making them lightweight and efficient.
- Mutable: Containers can be modified during runtime (e.g., by writing to the filesystem), but changes are lost when the container is removed unless committed to a new image.
-
Example:
- Running a container:
docker run -d --name mynginx nginx
- Viewing running containers:
docker ps
- Inspecting container details:
docker inspect mynginx
- Running a container:
Key Differences
-
Purpose: Images are used for packaging and distribution, while containers are the runtime instances of those images.
-
Mutability: Images are immutable (unchangeable), while containers can be modified during runtime (though changes are not persisted unless committed to a new image).
-
Lifecycle: Images persist until deleted, while containers have a lifecycle that includes creation, running, stopping, and deletion.
Published on: Jul 01, 2024, 08:16 AM