Understanding Docker Restart Behavior: Does It Use the Original or Latest Image?


5 views

When working with Docker containers, a common question arises: does docker restart use the original image version or pull the latest available image? The answer is straightforward but has important implications for deployment workflows.

Docker will always use the original image version when restarting a container. The restart command only stops and starts the existing container without modifying its underlying image reference.

# Create container with initial image
docker run --name webapp -d nginx:1.21

# Later, pull updated image
docker pull nginx:latest

# Restart maintains original image version
docker restart webapp

You can verify this behavior through container inspection:

docker inspect webapp --format '{{.Image}}'
# Shows original image hash

docker inspect webapp --format '{{.Config.Image}}'
# Shows the original image tag

To actually use a newer image version, you need to:

# Stop and remove the old container
docker stop webapp
docker rm webapp

# Create new container with updated image
docker run --name webapp -d nginx:latest

For automated deployments, consider:

docker pull nginx:latest
docker-compose up -d --force-recreate

Understanding this behavior is crucial for:

  • Rolling back deployments
  • Implementing CI/CD pipelines
  • Managing version consistency
  • Debugging version-related issues

When you run docker restart, Docker does not automatically switch to newer image versions. The container continues using the original image ID it was created with, even if you've pulled updates.

# Demonstration of the behavior:
docker run --name webapp -d nginx:1.23
docker pull nginx:latest
docker restart webapp

# Verify used image:
docker inspect webapp | grep -A 5 "Image"

Key fields in container inspection:

  • .Image: The concrete image SHA256 hash (immutable reference)
  • .Config.Image: The tag name used during creation (mutable reference)

Three scenarios trigger image updates:

# 1. Explicit recreation
docker stop webapp && docker rm webapp
docker run --name webapp -d nginx

# 2. Using docker-compose with --pull
docker-compose pull && docker-compose up -d

# 3. Kubernetes pod rotation (different ecosystem)
kubectl rollout restart deployment/webapp

For reliable updates in CI/CD pipelines:

# Atomic deployment pattern
NEW_CONTAINER="webapp-$(date +%s)"
docker run --name $NEW_CONTAINER -d nginx
docker stop webapp
docker rename webapp webapp-old
docker rename $NEW_CONTAINER webapp
docker rm webapp-old

Command to see exact image versions across your environment:

docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"
docker images --filter "reference=nginx"