Open In App

How To Update Existing Docker Image ?

Docker, a powerful containerization platform, allows developers to package applications and their dependencies into lightweight, portable containers. When it comes to managing Docker images, rebuilding them efficiently, and updating the containers are crucial for maintaining consistency and ensuring smooth deployments.

In this article, we’ll explore the process of rebuilding Docker images, understand essential terminologies, and demonstrate practical steps. Whether you’re a beginner or an experienced developer, this guide will help you learn the image rebuilding, and updating the image with ease.

Overview Of Docker Images

Here the so-called package mentioned above is a Docker Image, which is the base for running the containerized applications, imagine them as blueprints that define the instructions for creating containers. Each image encapsulates everything your application needs to run including application code, dependencies, and runtime environment.

Understanding Of Primary Terminologies

Before diving into the steps, let’s clarify some key terms:

Why Updating Docker Images?

Regularly updating Docker images is essential for various reasons, such as keeping our application more secure over time, providing new features, or optimizing resource utilization. We can update the docker image using the CLI.

Update Existing Docker Image: A Step-By-Step Guide

# Use an official Python runtime as a base image
FROM python:3.10

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Expose port 8080 for the application
EXPOSE 8080

# Define environment variable
ENV NAME World

# Default command to be run when the container starts
CMD ["python", "app.py"]
Building Docker image
# app.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, GeeksForGeeks!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)
docker images
docker-images

Step 1: Update Image

Based on the requirement make changes in the Dockerfile or files we used in the image, either it can be code changes, security updates to existing modules etc, please make a note that updating the Dockerfile won't effect existing application without rebuilding image, and we should update running containers based on this updated image.


For instance, we made changes to app.py as below

# app.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Welcome to GeeksForGeeks DevScripter Challenge!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)

Step 2: Rebuild The Docker Image

docker build -t my-python-app:v2 .
docker-build-image with version 2

Step 3: Check The Updated Docker Images

docker images
docker-images-uipdated

Step 4: Clean Up Previous Image

docker system prune
Cleaning up in Previous Image

Step 5: Identify Target Containers

docker ps
Listing the running processes

Step 6: Shutdown The Existing Server (container)

docker stop <container-id>
Stop existing Container

Step 7: Recreate Or Update Containers

docker run -dit --name mypython_container2 --volumes-from <previous container name or id>  -p 8080:80 my-python-app:v2


docker-image-update

Conclusion

In this article we've seen how to rebuild a docker image, release a new version and how to check the updated versions, along with the purpose of doing it, updated the existing running application as well.

Docker Image Update - FAQ's

Does Updating Affect Running Containers?

No, updating the image itself doesn't affect existing containers. To use the updated image, you'll need to stop and restart the containers

Why Is Updating Necessary?

Rebuilding the image and updating containers ensures your applications are running on the latest and most secure dependencies, reducing vulnerabilities.

How To Minimize Downtime During Rebuilding?

Utilize strategies like blue-green deployments or rolling updates to minimize downtime and ensure continuous service availability.

What Are Best Practices For Optimizing Image Size?

Remove unnecessary dependencies, leverage multi-stage builds, and minimize layer count to create smaller and more efficient images.

Can I Automate The Rebuilding Process?

Yes, you can use Continuous Integration/Continuous Deployment (CI/CD) pipelines to automate the rebuilding and deployment of Docker images.

Article Tags :