EasyConcept

What is the difference between a Docker image and a container?

Technology:
Docker
Experience:
Intern,
Junior
Topics:
DevOps

Quick answer

An image is a read-only template made of layers that contains the application and its dependencies; a container is a running (or stopped) instance of an image with its own writable layer on top.

Detailed explanation

An image is built from a Dockerfile. Each instruction creates a layer, and layers are cached and shared between images, which makes builds and pulls faster. Images are versioned with tags and stored in registries such as Docker Hub or a private registry.

Running docker run creates a container from an image: Docker adds a thin writable layer, sets up networking and starts the process. You can run many containers from the same image, just as you create many objects from one class.

Changes made inside a container live only in its writable layer and disappear when the container is removed. Persistent data belongs in volumes, and changes to the application belong in a new image.

Example

bash
docker build -t my-api:1.2.0 .        # create an image
docker run -d --name api1 my-api:1.2.0  # start a container from it
docker run -d --name api2 my-api:1.2.0  # a second container, same image
docker ps                               # running containers
docker images                           # local images

Key points

  • Image: read-only, layered template
  • Container: running instance with a writable layer
  • Many containers can share one image
  • Container changes are lost on removal; use volumes for data

Follow-up questions

  • DockerEasy

    What is the difference between a Docker container and a virtual machine?

    A virtual machine runs a full guest operating system on virtualised hardware, while a container shares the host鈥檚 kernel and isolates only the process, its filesystem and resources, which makes containers much lighter and faster to start.

    Intern 路 Junior 路 DevOps
  • DockerEasy

    What are Docker volumes and why do you need them?

    Volumes are storage managed by Docker that lives outside a container鈥檚 writable layer, so data such as database files survives when the container is removed or replaced.

    Junior 路 Mid-level 路 DevOps
  • DockerMedium

    How do you write a good Dockerfile? What are multi-stage builds?

    Use a small, pinned base image, order instructions so dependencies are cached before source code, use multi-stage builds to leave build tools out of the final image, run as a non-root user and keep secrets out of the image.

    Junior 路 Mid-level 路 DevOps
  • DockerMedium

    What is Docker Compose used for?

    Docker Compose defines a multi-container application (services, networks and volumes) in one YAML file so you can start, stop and rebuild the whole stack with a single command, typically for local development and testing.

    Mid-level 路 Senior 路 DevOps
  • Node.jsEasy

    What is the difference between package.json and package-lock.json?

    package.json lists your project's dependencies with allowed version ranges, while package-lock.json records the exact versions that were installed so every machine gets an identical dependency tree.

    Intern 路 Junior 路 Programming Fundamentals
  • GitEasy

    What is Git and how is it different from GitHub?

    Git is a distributed version control system that tracks changes to files on your machine; GitHub is a hosting service for Git repositories that adds collaboration features such as pull requests, code review and CI.

    Intern 路 Junior 路 Git