Fast Track IT Solution

Commit changes to Docker images

For understanding of Docker changes in running image file, we will pull Nginx image from the Docker hub repository.

Kindly note that before doing below mention process please create first account in Docker Hub, this account will be help to pull and push any images from the public repository.

 

Pulling the image and running the container

The first step is to pull the latest NGINX image, this will done with below command.

sudo docker pull nginx

 

Sometime you may get Docker Hub login failed error while pulling any image from the Docker HUB repository, in such case you can login first then need to be pull any desire image.

Following command can be execute for login in Docker HUB.

Docker Login

Note- you put the same credential which you created earlier on Docker hub website.

Below are the command can run for stop running containers in host machine.

Docker stop $(docker ps  –a  –q)

Docker rm $(Docker ps –a –q)

 

Once it download image, we can run from the terminal window, below command need be fire for run NGINX image.

Docker run –p 80:80  -d nginx

Before start NGINX, we should check is there any service is running on same port, below is command will use for showing how many containers are running in guest machine.

Docker ps or Docker ps-a

Note- Docker ps-a will use for show any running background container service.

You can open in any client browser through guest machine IP ( http://192.168.X:X)

192.168. X: X is guest machine’s IP.

Accessing and modifying the container

Next we will access container. When we run Docker command, it will have presented container ID number. You will need that number to access the image.

You can get container ID while run below command on terminal.

Sudo docker ps (you can see all running containers along with container id.)

We will run following command for modify container.

Sudo docker exec –it CONTAINER_ID bash

 

Once you entered above command you will be entered in particular container and you will get their bash terminal, now let us add some necessary software and modify index.html file for the verification of changes.

apt-get install vim

 

Once we downloaded VIM package, we will edit index.html file which is located at (/usr/local/nginx/index.html)

Move running index.html file in other location and create new file with the same name.

$ Cd /usr/share/nginx/html

$ Mv index.html index.html.bkp

$ touch index.html

$ vim index.html ( mentioned anything, I have mentioned my name there Kalpesh Patel)

 

 

Exit the container and commit the changes

 

Now we have modified the container we have to commit the changes, first we will exit the container with the command exit. To commit the changes and create a new image based on said changes.

Issue the command.

Sudo docker commit CONTAINER_ID nginxtest

Note- I will keep Nginxtest name for the new created image so that we can easily identify

Now we have new image (nginxtest) that have all the modification already in place.

 

First we will stop running old container ( Nginx) before start new container (Nginx2) or run new container with other port.

$ Docker stop CONTAINER_ID

$ docker run –p 80:80 –d nginxtest

When we access the new running container you will see all of the modification are in place and ready to be used.