How to update a Docker image with new changes?

Why we need to update an Image?

How to update a Docker image with the new changes that we made in the container? Yeah, we all know that, the Docker image is the core part of your Docker container. The container works based on this Image.

Docker image can be built using many ways. This is explained in this topic An introduction note to Docker containers – basics, part 1

We can build many containers from a single image. An image is a combination of a file system and parameters. A Docker image is built up from a series of layers. Each layer represents an instruction in the image’s Dockerfile. Each layer except the very last one is read-only.

Once the container is launched using an image, you can make changes on that container. Like, you can create new files, you can install new modules, packages etc.. These changes will persist on the container as long as it exists.

** This is not recommended to update the Docker image using this commit method. Always try to use the container as stateless. This should increase the performance and your App will be more fit to the infra as stateless.

But, still, sometime we need to do this. Read more if you need to commit your container as your next docker Image **

Consider the situation, your Docker container exited/stopped and you are unable to start the containers using docker start command. In this case you need to restart or rebuild the container from the Docker image. Here, all changes which you have made on your container will lost and the new container from the base image should contains the initial things.

If your data / changes are in a different location and the container is mounting to that dir, you can avoid the above problem. This is the recommended way, mounting external volumes to containers. In this case your container is always in stateless form. You can start/stop containers without any dependency with its data, as it’s mounted externally.

However, in default situation [here, case 1 – without any mount] all changes on your container will be lost.

To avoid this we can commit the changes/update on a container to original image or we can create a new image with your updated container. The container from the new or update image should have all the changes.

So, How to update a Docker image?

"<yoastmark

How to commit changes to existing Docker images?

Syntax

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Example:

docker commit 782ce39021a0 wordpress:new2

Here 782ce39021a0 is the existing container ID where you made the configuration changes and wordpress:new2 image name with tag. You can also save the changes to the existing Docker image. Here we creating a new image with different tag value.

See the example pasted below:

I have one container which started using WordPress default image.

root@c2s-Lap1:~# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
782ce39021a0        wordpress:new       "docker-entrypoint..."   5 days ago          Up 2 seconds        0.0.0.0:1883->80/tcp   wp3

Entering to that container..

root@c2s-Lap1:~# docker exec -it 782ce39021a0 bash

I need file editor nano..

Checking whether it’s available on the current container environment.

root@782ce39021a0:/var/www/html# which nano
root@782ce39021a0:/var/www/html#

No result..

Installing nano..

root@782ce39021a0:/var/www/html# apt-get install nano

Checking details..

root@782ce39021a0:/var/www/html# which nano
/bin/nano

Now we need to commit the changes in this container to base image.

Exiting from docker container.. Ctrl + p + q

Committing..

root@c2s-Lap1:/var/www/html# docker commit 782ce39021a0  wordpress:new.with.nano
sha256:c3afbec42bfcb883189d967a504076a993172db99bfdae2725d3fe8ac7d1efec

Checking Docker images..

docker images
root@c2s-Lap1:/var/www/html# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
wordpress                  new.with.nano       c3afbec42bfc        50 seconds ago      799MB
wordpress                  new2                204ca49e7475        About an hour ago   798MB

Now starting a new container with updated image.

root@c2s-Lap1:~# docker run -it --name wp-new -d -p 1882:80 wordpress:new.with.nano
8ba28a0f25dd750d0e6d3179f7587525f991a58bf6b2c233cc0c2dc3e05d4360

Entering to new container..

root@c2s-Lap1:~# docker exec -it wp-new bash
root@8ba28a0f25dd:/var/www/html#

Checking the package

root@8ba28a0f25dd:/var/www/html# which nano
/bin/nano

Hence proved 😛

Suggested post..

Docker tricks and tips

In this article I would like to add some commonly using Docker commands, and some tips and tricks. Will start with some basics and add more commands/tricks going forth.

I shared some basics of Linux containerisation technologies and Docker containerisation in our previous discussions. You can check all previous discussions from this link. READ MORE…

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

12 thoughts on “How to update a Docker image with new changes?

  1. “Exiting from docker container.. Ctrl + p + q”
    If only for that I am glad I got here 😀 !!
    Thank you very much for the post.

  2. Thanks for this! One question: where does “66a9db890230” come from? Should it actually be “782ce39021a0”, which is the initial container ID shown?

  3. Thank you for sharing, I would like to know more about your great article. Keep sharing amazing content of yours.

  4. Thank you for sharing, I would like to know more about your amazing blog of yours. It really helped me with my project I am doing now.

Leave a Reply

Your email address will not be published. Required fields are marked *