How to Create a New Image from Docker Container ? 3 Steps Only

Docker is gaining popularity in the programming world. You can run a Linux system on a small container and each container is separate from the other container. All source code, configurations, ports, networking are isolated from each other. You can think docker container as a container in a ship that can be shipped from one place to another place and you can share this container with everyone. In this entire tutorial, you will know how to create a new image from the docker container in very simple steps.

Steps by Step to Create a New Image from Docker Container

Step 1: First pull the image from the Docker hub if you have not any images.

The first step is to convert the existing or new image into the container. To do so you have to first type the docker run command. Please make sure you must have installed the docker.

docker run -ti ubuntu bash

This will run the container and take you to the bash command of the ubuntu. Please note that if the images are not on your localhost then docker will pull it from the docker hub server.

docker run command

Step 2: Create or changes something inside the Container

Let’s create a new file inside the docker container using the bash command. You can do anything inside the container. But for the demonstration purpose, I am creating only a new file “MY_FILE“.  If you exist the container then after running the same container you will see there is no file with the name  MY_FILE. Thus you have to commit the docker for making changes inside the container.

no file inside the container after exit

You can also see the value after the root@ has also changed. This clearly shows that this is the new container from the same image. Let’s create a new file using touch MY_FILE command and type ls to see it.

create new file inside the docker container

Step 3:  Commit the Docker file

Now after changing the contents inside the docker container. Exist from the container. Now to write the changes permanently use the docker commit command. To do so, First of all, find the last container id you want to commit using the docker ps command.

docker ps -l

last runned container name

docker commit existing_container_name new_container_name

After committing use the docker images command to get the images list.

docker images

custom docker images with the file inside

At last, you have to run the container to see the file inside my-image container. Type the below command to see the MY_FILE.

docker rub -ti my-image
ls

docker container with the file

Hurray, you have successfully create a New Image from Docker Container.
Hope you have liked this tutorial if you have any query then please message us on our official Data Science Learner Facebook Page.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner