September 10, 2021 / Nirav Shah
Usually when we create a docker container it will be terminated after use.
Thus, we have to create a new container and install usual tools for daily tasks everytime. So the problem is “How to backup your docker images”?
To solve this problem, We can create an image from a container and store this image in Ec2 Instance.
If we have some tools that are used daily in one container and we don’t want to install them manually every time.
In that Case, We can customize an image and reuse it when needed and set it up in a few seconds.
First, install docker and start docker in your Ec2 linux instances,
For Example
For installing ubuntu os in docker
docker pull ubuntu
To run ubuntu container…
docker run -it ubuntu bash
Now, you are in container. You want to install these 3 tools:
apt-get install telnet apt-get install php apt-get install apache2
All 3 tools are installed. You can check by this command
find / -name telnet find / -name php find / -name apache2
Now, Exit from container by ctrl + pq command
Check your container with docker ps command
Now create image from container by this command
docker commit container id imagename ======= container id mention in result of docker ps command
Example
docker commit 44c854fce209 backup
Check your image by this command
docker images
docker save --output imagename.lastest.tar imagename
Example
docker save --output backup.lastest.tar backup
You can check by ls command and file location by pwd command
Now you can delete this container and image by this command
docker rm -f containerid docker rmi -f imageid
Now check by
docker ps docker images
it doesn’t exist.
Check by ls command your backup in current directory
Use this command
docker load --input imagename.lastest.tar
Example
docker load --input backup.lastest.tar
Now check with Docker images. You will find your image.
Now create container from this image by using this command
docker run -it imagename bash
Example
docker run -it backup bash
NOW Check running container and it has 3 tools are pre-installed using following commands.
find / -name telnet find / -name php find / -name apache2
As a Director of Eternal Web Private Ltd an AWS consulting partner company, Nirav is responsible for its operations. AWS, cloud-computing and digital transformation are some of his favorite topics to talk about. His key focus is to help enterprises adopt technology, to solve their business problem with the right cloud solutions.
Have queries about your project idea or concept? Please drop in your project details to discuss with our AWS Global Cloud Infrastructure service specialists and consultants.