Rebuilding an Image Does Not Update the Container Already Running It

In progress Docker

An image is a template; a container is a process started from one specific image. docker compose build produces a new image under the same tag, but the running container keeps the image it was created from. The new code reaches production only when the container is recreated from the new image. Forgetting that step is why a "deployed" fix sometimes isn't live.

Build, Then Recreate

docker compose build web            # new image for the web service
docker compose up -d --no-deps web  # recreate only that container

up -d compares each service's configuration and image with its running container and recreates the ones that changed. --no-deps stops Compose from also starting or recreating the services web depends on (the database, a cache), so they keep running untouched.

Command Effect on a running container
docker compose build none: only the image changes
docker compose restart web restarts the same container, old image included
docker compose up -d web recreates web if its image or config changed
docker compose up -d --force-recreate web recreates it even if nothing changed

Verify What Is Running

docker compose images web                  # image the container uses
docker inspect --format '{{.Image}}' <container>

Compare the image ID with the one just built. Recreating a container also discards anything written to its own layer: data must live in volumes.

References

Review cards

Flashcard Why does `docker compose restart` not update a running container?

Because docker compose restart reuses the existing container, keeping the old image.

Flashcard What command recreates a container even if its image and configuration have not changed?

The docker compose up -d --force-recreate command.

Quiz Which command would you use to verify that a container is using the latest image built?
  • docker inspect --format '{{.Image}}' <container>
  • docker ps
  • docker images
  • docker logs <container>

The docker inspect --format '{{.Image}}' <container> command shows the image ID of the running container, allowing you to compare it with the image ID of the latest build.