DEV Community

Cover image for How to create an image from an existing container
Ravina Deogadkar
Ravina Deogadkar

Posted on • Edited on

How to create an image from an existing container

Hi everyone! Earlier we have created docker container using nginx:alpine image and now we will learn to create snapshots of docker container. To the readers who have not read my previous article you can find it here. Don't forget to like it and share itπŸ˜„

Docker run

Let's create a container from nginx:alpine image and name it as "myweb" that will run on 80 port using Docker run command.

docker run -p 80:80 --name myweb -d nginx:alpine

docker create container

We can now check our newly created container
docker ps -a

docker container list

Docker exec -it

Docker exec command is used to execute a command in the running container.

docker exec -it myweb /bin/bash

Docker exec error

It gives us an error, but why so??
Its because bash is not installed on our container so we are going to use /bin/sh.

Docker exec resolve

Now let's add Tutorial.txt file in the root directory of our container.

Echo file

press 'Ctrl + D' to exit

Docker commit

Now we have our container ready we can create snapshot of our container by using docker commit comand along with the container name/Id and new container name.

docker commit myweb mynginx

Docker commit

Let's check our newly created docker images.

docker images

Docker images

And let's check if our file is there in image by creating a container from that image.

Docker new container

Our new container is created with name myweb_v2, now we will check for our Tutorial.txt file in a new container.

Docker exec new conatiner

That's it for today, Happy Coding!.....

Top comments (0)