Backup and Restore docker images in EC2 instance

Backup and Restore docker images in EC2 instance

September 10, 2021 / Eternal Team

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:

  • telnet
  • Php
  • apache
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

For backup image in EC2 Instance

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

Restoring image from EC2 Instance

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
AWS-Consulting-Partner

Talk to AWS Certified Consultant

    Want to start a project?

    It’s simple.

    Contact us