How to upgrade docker container after its image changed
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A running Docker container does not auto-refresh when its image is updated. To apply a new image, you replace the container while preserving required runtime settings and persistent data. A good upgrade runbook includes pull, replace, health verification, and rollback.
Core Sections
1. Treat upgrades as container replacement
Containers are immutable runtime instances created from images. Once started, they keep using the original image layers even if a newer tag exists remotely. That means an upgrade is a recreate operation, not an in-place mutation.
Typical upgrade sequence:
- Pull target image tag.
- Stop and remove old container.
- Start new container with equivalent options.
- Validate service health.
2. Pull exact image version
Use explicit version tags, not mutable tags, for predictable rollouts.
If your team supports digest pinning, store and deploy by digest to guarantee byte-identical rollouts across environments.
3. Preserve runtime configuration before replacement
Most failures come from forgetting flags used by the old container. Capture current settings before removing it.
Important settings to preserve:
- published ports
- environment variables
- mounted volumes
- network aliases
- restart policy
If configuration is already defined in Compose or orchestration manifests, prefer those sources over manual reconstruction.
4. Recreate the container with the new image
Volume-backed data remains available after container replacement, which is why volume mapping must be preserved exactly.
5. Use Docker Compose for repeatable upgrades
Compose reduces manual flag drift and is usually safer than direct docker run for services with several settings.
--no-deps upgrades one service without restarting unrelated dependencies. Use full stack restart only when dependency contracts changed.
6. Validate health before traffic cutover
A container in running state is not proof of application readiness. Validate endpoints and logs:
If app initialization takes time, use health checks and wait conditions in deployment workflow before switching traffic.
7. Prepare and test rollback path
Keep previous stable image tag available until the new version proves stable. Rollback should be a documented command set, not an improvised response.
For zero-downtime services, use blue-green style rollout and switch load balancer targets only after new container passes checks.
8. Clean up safely after stabilization window
After observing stable behavior for a defined window, remove unused artifacts:
Do cleanup after rollback window, not immediately after deployment.
Common Pitfalls
- Pulling a new image but forgetting to recreate the running container.
- Recreating container without original env vars, ports, or volumes.
- Deploying mutable tags such as
latestin production environments. - Declaring rollout complete before health and log checks pass.
- Deleting old images too early and slowing incident rollback.
Summary
- Docker upgrades after image changes are replacement workflows.
- Use explicit version tags and preserve runtime configuration.
- Prefer Compose or declarative manifests for repeatability.
- Validate readiness before routing real traffic.
- Keep rollback commands and previous stable images ready.

