Normally we dont want to save the state : we do something like this, suppose we want to run a ubuntu container, we will install all the dependencies, we do something like this. For example i will install heroku in this ubuntu :
$ docker run -i -t ubuntu /bin/bash
After bash is open:
$ apt-get update $ apt-get install curl
To install heroku cli
$ curl https://cli-assets.heroku.com/install.sh | sh heroku -v
Heroku will be installed !
But once you close/exit the container. All your changes will be lost. What if we want to save state ? We dont want to reinstall this everytime. What if i want to reuse whatever i did earlier.. and make things easier !
How to save state ?
Cool to demonstrate, lets create a new container named “boring_shaw_ubuntu” from ubuntu image
$ docker container run -i -t --name boring_shaw_ubuntu ubuntu /bin/bash
Do all the changes in this container:
I installed heroku cli. like the previous steps in this container too.
Lets now save the state , Open a new terminal and type :
$ docker container commit boring_shaw_ubuntu ubuntu-heroku
This will create a new image with the changes with name ubuntu-heroku: You can see in the image below,
Lets now test our new image that we created
If you run this image this will have heroku already installed.
Saving the container as Image
What if we want to make this image portable. We can save it as compressed file :
$ docker image save -o ubuntu-heroku.tar ubuntu-heroku
So this will create a ubuntu-heroku.tar , which is portable
$ docker image load -i ubuntu-heroku.tar
and then you can run the container using :
$ docker run -i -t ubuntu-heroku /bin/bash