How to Upgrade a Docker Container After Its Image Has Changed

When working with Docker, you may face a common situation:
👉 You’ve updated the Docker image (e.g., with a new version of your app), and now you want your running container to reflect that change.

But how do you upgrade a Docker container after its image has changed?


✅ The Right Approach: Containers Are Ephemeral

Remember:

A Docker container is meant to be temporary and immutable.
It’s designed to run a specific version of an image, and when the image changes, the container itself doesn’t automatically update.

So the correct way to upgrade is to:

  1. Pull the new image
  2. Stop and remove the old container
  3. Recreate the container from the new image

1️⃣ Step-by-Step Process

🔹 Step 1: Pull the New Image

If you’re using a public image or your private registry:

docker pull myapp:latest

If you built a new image locally:

docker build -t myapp:latest .

🔹 Step 2: Stop and Remove the Old Container

List running containers:

docker ps

Stop the container:

docker stop myapp_container

Remove the container:

docker rm myapp_container

🔹 Step 3: Run a New Container from the Updated Image

docker run -d --name myapp_container -p 8080:80 myapp:latest

This creates a new container with the updated image.


2️⃣ Example Automation with Docker Compose

If you’re using Docker Compose, upgrading is easier:

docker-compose pull
docker-compose up -d

This pulls updated images and recreates containers as needed.


3️⃣ Bonus Tip: Preserve Data with Volumes

If your container stores data in volumes, those persist across container upgrades:

docker run -d -v my_data:/app/data myapp:latest

Data in my_data remains even after the container is removed and re-created.


📌 Summary

When a Docker image changes:

  1. ✅ Pull the latest image
  2. ✅ Stop and remove the old container
  3. ✅ Start a new container with the updated image

Or, if using Docker Compose:

docker-compose pull
docker-compose up -d

⚠️ Remember: Containers are ephemeral. Always treat them as replaceable units.

This ensures your containers always run the latest and most stable image.

Sharing Is Caring:

Leave a Comment