How to update a Docker image with new changes?

Why we need to update an Image?

How to update a Docker image? Yeah, we all know, 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.

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.

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.

However, in default situation 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.

See the example pasted below:

I have one container which started using WordPress default image.

[email protected]:~# 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..

[email protected]:~# docker exec -it 782ce39021a0 bash

I need file editor nano..

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

[email protected]:/var/www/html# which nano
[email protected]:/var/www/html#

No result..

Installing nano..

[email protected]:/var/www/html# apt-get install nano

Checking details..

[email protected]:/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..

[email protected]:/var/www/html# docker commit 66a9db890230  wordpress:new.with.nano
sha256:c3afbec42bfcb883189d967a504076a993172db99bfdae2725d3fe8ac7d1efec

Checking Docker images..

docker images
[email protected]:/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.

[email protected]:~# docker run -it --name wp-new -d -p 1882:80 wordpress:new.with.nano
8ba28a0f25dd750d0e6d3179f7587525f991a58bf6b2c233cc0c2dc3e05d4360

Entering to new container..

[email protected]:~# docker exec -it wp-new bash
[email protected]:/var/www/html#

Checking the package

[email protected]:/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 Ashok

Linux Systems Architect at Endurance International Group. I know her (Linux) since many years. Linux lover. Like to play on Linux console. I started this blog to share and discuss Linux thoughts.

Always happy for an open discussion! Write to arun (@) crybit (dot) com. Check about me for more details. About this blog and our strong members, check The team CryBit.com

Leave a Reply

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