CodeOath
← All posts
Docker65 min total · 19 parts

Docker Fundamentals: Images, Containers, and Writing a Good Dockerfile

Contents — Part 3 of 19: Image vs. Container
Part 3 of 19 · ~1 min

Image vs. Container

Diagram of building a Dockerfile into an image, then running containers from it

  • An image is a read-only template — your application code plus its runtime, libraries, and OS packages, built once from a Dockerfile.
  • A container is a running instance of an image — the image itself, plus a thin writable layer on top for anything the process changes at runtime.

One image, many containers: you can start ten containers from the same image, each an independent, isolated process, without rebuilding anything. Stopping a container doesn't delete it — its writable layer (and any data written into it) is still there until you explicitly remove it with docker rm. This is a common point of confusion: docker stop just sends a termination signal to the process and leaves everything else in place; the container still exists, still shows up in docker ps -a, and can be restarted with docker start.

docker run --name web1 -d myapp        # container 1, from the myapp image
docker run --name web2 -d myapp        # container 2, same image, fully independent
docker stop web1                       # web1's process stops; its filesystem still exists
docker start web1                      # picks right back up — same writable layer as before
docker rm web1                         # NOW web1 and its writable layer are actually gone