Sobes.tech
Middle

What happens after making changes to the image?

sobes.tech AI

Answer from AI

After making changes to the image, the image itself becomes a new, separate version. When a container is subsequently run based on this image, the container will use the modified version reflecting the applied changes. Old containers, started from previous versions of the image, will retain their state and operate with the old version until they are stopped and restarted based on the new image. In other words, changes in the image do not affect already running containers.

Let's consider the lifecycle using Docker as an example:

  1. Dockerfile Build: You make changes to the Dockerfile.

    # Updated Dockerfile
    FROM ubuntu:latest
    RUN apt-get update && apt-get install -y nginx
    COPY index.html /var/www/html/
    EXPOSE 80
    
  2. Image Build: You execute the build command.

    # Building a new version of the image
    docker build -t my-webserver:v2 .
    

    Docker creates a new layer with the changes on top of the previous layers. my-webserver:v2 now points to the new image.

  3. Running New Container: You run a new container.

    # Running a container based on the new image version
    docker run -d -p 8080:80 --name webserver_v2 my-webserver:v2
    

    This container will use the new version of the image.

  4. Existing Containers: Containers started from, for example, my-webserver:v1, will continue to operate with old data and configuration.

Action Result Impact on old containers
Changing Dockerfile The image definition code changes. No
Building the image (docker build) A new image with a unique ID is created. No
Tagging the image (docker tag) An alias is assigned to the new image. No
Running a new container The new container uses the new image. No
Restarting an old container Running the old container on the old image. No (by default)
Deleting the old image The image is deleted, but containers based on it remain. No (containers are preserved)

To update running containers to the new image version, you need to stop and remove the old containers, then start new ones based on the updated image. In production environments, orchestrators like Kubernetes, Docker Swarm, or utilities like Docker Compose are used to manage such updates, implementing strategies like rolling updates.