Docker
Container
Image
DevOps
Software Development

In Docker, what's the difference between a container and an image?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Understanding the Difference Between Docker Containers and Images

Docker has revolutionized the way we think about deploying software, largely because of its core components: images and containers. Although often mentioned together, containers and images serve different roles in containerization technology. In this article, we will delve into the differences between a Docker image and a Docker container, accompanied by technical explanations and examples to better understand each concept.


Docker Image: The Blueprint

A Docker image can be viewed as the blueprint or template for creating Docker containers. It is essentially a stack of layers built from a series of instructions defined within a Dockerfile. Each layer corresponds to a step in the image creation process, built upon the previous one. An image encapsulates the application and its environment, including binaries, libraries, and configurations.

Key Characteristics of Docker Images:

  • Immutable: Once built, Docker images cannot be changed. This immutability ensures that environments remain consistent across deployments.
  • Portable: Images can be easily shared and distributed, making them a reliable means of ensuring that applications run the same way in different environments.
  • Versioned: When updates are made to an image, a new layer is created. Image layers are cached, allowing for version control and efficient image building.

Here's an example Dockerfile:

dockerfile
1FROM ubuntu:20.04
2RUN apt-get update && apt-get install -y python3
3COPY . /app
4WORKDIR /app
5CMD ["python3", "app.py"]

In this example, each instruction (FROM, RUN, COPY, and CMD) adds a layer to the image.

Docker Container: The Run-time Instance

A Docker container is a runnable instance of a Docker image. If an image is the blueprint, then a container is the actual building constructed from that blueprint. Containers are designed to run processes in isolated environments, leveraging the underlying operating system's capabilities for resource restriction and process isolation.

Key Characteristics of Docker Containers:

  • Mutable: Containers can be started, stopped, and terminated, and you can add changes to them. However, such changes are not saved back to the image but can be captured in a new image.
  • Ephemeral: Containers are generally designed to be transient and disposable. Ideally, all data should either be written to data volumes or saved externally.
  • Isolated and Lightweight: Containers share the OS kernel but operate in isolated spaces, making them more resource-efficient than virtual machines.

With the earlier image, running the following Docker CLI command will create a container:

bash
docker run -d -p 8080:80 --name my_container my_image

In this command, -d runs the container in detached mode, -p maps the host port to the container port, and --name specifies the container's name.


Key Differences Between Images and Containers

AspectDocker ImageDocker Container
DefinitionBlueprint for the application and its dependencies.Running instance of a Docker image.
StateImmutableMutable
LifecycleBuilt, Generally PermanentCreated, Started, Stopped, and Removed
StorageStored in layers, only initial read access.Writable layer on top of read-only layers.
UsageUsed to create containers.Used to run applications.
IsolationN/AIsolated environment sharing host kernel.
PortabilityHighly portable, sharing facilitated by repositories.Limited to the host unless networked.
DeploymentPulled from Docker registries.Deployed in various environments as executable entities.

Subtopics for Further Exploration

Image Layering and Caching

Docker employs a layered filesystem where image layers are cached. This speeds up image rebuilds because unchanged layers do not need to be rebuilt, making it efficient and reducing build times.

Docker Registries

Images are stored in Docker registries such as Docker Hub. These registries act as repositories for images, allowing broad sharing and distribution. Understanding registries and image tagging can enhance management practices for images.

Networking and Volume Management

While images dictate what application and environment are executed, containers essentially determine their runtime configurations, including networking and storage. Familiarity with Docker networking and volumes is crucial for flexible deployments.


Understanding these fundamentals allows software developers and DevOps professionals to effectively utilize Docker. Knowing when to create new images or how to manage containers leads to more efficient deployment workflows and ensures consistency in application environments. Each serves a specific role in application development and deployment, contributing to Docker's flexibility and robustness as a containerization platform.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.