Open In App

How To Update Existing Docker Image ?

Last Updated : 08 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
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.

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

  • 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
  • For your we are providing the app.py source code:
Python3
# 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 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

Python3
# 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

  • 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 3: 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 4: 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 5: 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 6: 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 7: 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

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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads