Open In App

How To Update Existing Docker Image ?

Last Updated : 18 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  • Dockerfile: A text file that contains instructions for building a Docker image. It defines the base image, environment variables, dependencies, and other configuration details for the image.
  • Docker Build: The command used to build/create an image from a Dockerfile. It compiles the instructions and generates a new image layer by layer, it uses cache layers if already exists, we can also force it to not use cached layers.
  • Docker System Prune: A command that removes Dangling images (unused images), containers, volumes, and networks. It helps free up disk space and keeps your system clean.

What Docker Updates do?

The following are the some of the insights on what docker updates do:

  • Updating Container Images: Docker update fetches the latest version of container images from the specified repository, ensuring that the container is running the most recent software and security patches.
  • Applying Configuration Changes: Updates may include changes to the container's configuration, such as environment variables, network settings, or volume mounts, to adapt to evolving requirements.
  • Improving Performance: Updates often optimize resource usage, improve performance metrics, and fix bugs that may affect container stability or efficiency.
  • Enhancing Security: Docker updates frequently address security vulnerabilities by applying patches and updates to underlying components and dependencies, thereby reducing potential risks and enhancing overall container security.

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. The following are some of the reasons of updating the docker images:

  • Security Patching: Through regular updates we can ensure the vulnerabilities in underlying software with patching, reducing the risk of exploitation.
  • Feature Enhancement: Updating the docker images helps to introduces the new features or improvements. It helps in enhancing the functionality and performance.
  • Optimized Resources: Through new resources we can optimize the resource usage bringing the efficiency in the docker images.
  • Compatibility: Through updating the software in the docker image, we can maintain the compatibility with dependencies and libraries with smooth operation across the environment.

How to Update Existing Docker Image? A Step-By-Step Guide

The following are the steps that guides you on how to update the docker images:

Step 1: Update Docker Image

  • Let's assume our application is running with the below base image:
  • The following is the current image version's Dockerfile
# 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"]
  • The following screenshot illustrates the building of docker image using above specified Dockerfile.
Building Docker image

Step 2: For your we are providing the app.py source code:

Python
# 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)
  • We can list the existing images using the below command:
docker images
docker-images

Step 3: 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
Python
# 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 4: Rebuild The Docker Image

  • Run the below command to add updates to the image, and make the updated image available to run containers, the below command will build the docker image based on the Dockerfile, if the cache exists for the image, the image building can be done in less time comparatively.
  • The Option -t is used for tag info, tag name and version, here string after : is version, it is optional if you don't specify the default value is latest.
docker build -t my-python-app:v2 .
docker-build-image with version 2
  • Here we mentioned the image name as my-python-app, and tag as v2, we can use this tag while running the container to choose the which version to use.

Step 5: Check The Updated Docker Images

  • The below command in viewing all the docker images in that we can confirm whether our updated docker image with name my-python-app with v2 is available.
docker images
docker-images-uipdated

Step 6: Clean Up Previous Image

  • You can use the docker system prune command to remove any dangling images (untagged images) that might be lingering from previous builds. This helps with storage management.
docker system prune
Cleaning up in Previous Image
  • We've created an updated version of image, but we didn't make any changes to already running containers, to update them we need to stop them and rerun the container using the latest version of image.

Step 7: Identify Target Containers

  • You can list active containers using docker ps and identify the containers to be updated, note the container id, we use this id to stop container.
docker ps
Listing the running processes

Step 8: Shutdown The Existing Server (container)

  • Here, we are shutdowning the exit running container with previous image version with container id using the following command:
docker stop <container-id>
Stop existing Container

Step 9: Recreate Or Update Containers

  • Run the server by using the latest version. For not loosing data use the option --volumes-from option with specifying the previous container name helps in updating image with having data.
  • The following is command for that. Here use the command with specifying your previous container name.
docker run -dit --name mypython_container2 --volumes-from <previous container name or id>  -p 8080:80 my-python-app:v2

docker-image-update

Best Practices of Updating Existing Docker Image

The following is the best practices of updating existing the docker image:

  • Versioning: On using the semantic versioning we can clearly identify the updates. On tagging the image appropriately ( eg: v1, v2 ) in distinguishing between different versions and ensuring consistency in deployments.
  • Automated Builds: Implement Continuous Integration/Continuous Deployment (CI/CD) pipelines to automate the building, testing, and deployment of Docker images. This ensures that updates are seamlessly integrated into the development and deployment workflows.
  • Immutable Infrastructure: Treat Docker images as immutable artifacts. Avoid making changes directly to running containers; instead, update the Dockerfile to reflect changes and rebuild the image. This approach enhances reproducibility and reliability.
  • Layer Caching: Utilize Docker's layer caching mechanism effectively during builds. Structure your Dockerfile to maximize caching benefits by placing frequently changing instructions towards the end and leveraging multi-stage builds where applicable.

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. We have specified the best practices and automating the updates helping the developers in the process and minimize the downtime.


Next Article
Article Tags :

Similar Reads