Home  Docker   Difference ...

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

  1. 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.
  2. 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.
  3. 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.
  4. Example:

    • Pulling an image: docker pull nginx
    • Building an image: docker build -t myapp .
    • Viewing local images: docker images

Docker Container

  1. 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.
  2. 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).
  3. 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.
  4. Example:

    • Running a container: docker run -d --name mynginx nginx
    • Viewing running containers: docker ps
    • Inspecting container details: docker inspect mynginx

Key Differences

Published on: Jul 01, 2024, 08:16 AM  
 

Comments

Add your comment