Why we need to update an Image?
** Updated post **
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. **
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?
How to commit changes to existing Docker images?
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
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.
[email protected]-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..
[email protected]:~# docker exec -it 782ce39021a0 bash
I need file editor nano..
Checking whether it’s available on the current container environment.
[email protected]782ce39021a0:/var/www/html# which nano [email protected]782ce39021a0:/var/www/html#
[email protected]782ce39021a0:/var/www/html# apt-get install nano
[email protected]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
[email protected]:/var/www/html# docker commit 782ce39021a0 wordpress:new.with.nano sha256:c3afbec42bfcb883189d967a504076a993172db99bfdae2725d3fe8ac7d1efec
Checking 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 😛
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…